Powershell to send mail using gmail smtp settings

Powershell to send mail using gmail smtp settings

you can send mail from powershell using gmail smtp and email id. There are mail options to automate send mail from powershell but below i am going to show you how to use gmail smtp in  Net.Mail.MailMessage .

Please check – Gmail SMTP  For detail SMTP and POP3 port and authentication method  at the end of the post.

Below is powershell code which will send test mail from $email and using gmail SMTP.gmail.com. you can modify Subject and  body to add your email content and also make sure you change email and password  with your gmail and password.

You can download the powershell script from Microsoft technet


$email = “myemail3@gmail.com”

$pass = “MyPass@123”

$smtpServer = “smtp.gmail.com

$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$smtp.EnableSsl = $true
$msg.From = “$email”
$msg.BodyEncoding = [system.Text.Encoding]::Unicode
$msg.SubjectEncoding = [system.Text.Encoding]::Unicode
$msg.IsBodyHTML = $true
$msg.Subject = “Test mail from PS”
$msg.Body = “<h2> Test mail from PS </h2>
Hi there

$SMTP.Credentials = New-Object System.Net.NetworkCredential(“$email”, “$pass”);


(Visited 675 times, 1 visits today)

Add a Comment