Send mail via Gmail with PowerShell V2's Send-MailMessage Send mail via Gmail with PowerShell V2's Send-MailMessage powershell powershell

Send mail via Gmail with PowerShell V2's Send-MailMessage


Here's my PowerShell Send-MailMessage sample for Gmail...

Tested and working solution:

$EmailFrom = "notifications@somedomain.com"$EmailTo = "me@earth.com"$Subject = "Notification from XYZ"$Body = "this is a notification from XYZ Notifications.."$SMTPServer = "smtp.gmail.com"$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)$SMTPClient.EnableSsl = $true$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("username", "password");$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

Just change $EmailTo, and username/password in $SMTPClient.Credentials... Do not include @gmail.com in your username...


This should fix your problem:

$credentials = New-Object Management.Automation.PSCredential “mailserver@yourcompany.com”, (“password” | ConvertTo-SecureString -AsPlainText -Force)

Then use the credential in your call to Send-MailMessage -From $From -To $To -Body $Body $Body -SmtpServer {$smtpServer URI} -Credential $credentials -Verbose -UseSsl


I just had the same problem and ran into this post. It actually helped me to get it running with the native Send-MailMessage command-let and here is my code:

$cred = Get-CredentialSend-MailMessage ....... -SmtpServer "smtp.gmail.com" -UseSsl -Credential $cred -Port 587 

However, in order to have Gmail allowing me to use the SMTP server, I had to log in into my Gmail account and under this link https://www.google.com/settings/security set the "Access for less secure apps" to "Enabled". Then finally it did work!!