3

i am trying to send emails though gmail smtp server using powershell script. but i am not able to send emails .. i am using below script-

$EmailFrom = "[email protected]" $EmailTo = "[email protected]" $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("bttpm", "Password"); $SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body) 
2
  • Have you checked the connection? Commented Mar 13, 2015 at 10:08
  • yes, i can telnet the port Commented Mar 13, 2015 at 10:15

3 Answers 3

9

This worked for me:

$SMTPServer = "smtp.gmail.com" $SMTPPort = "587" $Username = "[email protected]" $Password = "" $to = "[email protected]" $cc = "[email protected]" $subject = "Email Subject" $body = "Insert body text here" $attachment = "C:\test.txt" $message = New-Object System.Net.Mail.MailMessage $message.subject = $subject $message.body = $body $message.to.add($to) $message.cc.add($cc) $message.from = $username $message.attachments.add($attachment) $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort); $smtp.EnableSSL = $true $smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password); $smtp.send($message) write-host "Mail Sent" 

However, you may get this error message:

"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. " 

This is because the default security settings of Gmail block the connection, as suggested by the auto message from Google. So just follow the instructions in the message and enable "Access for less secure apps". At your own risk. :)

More info here: http://petermorrissey.blogspot.ro/2013/01/sending-smtp-emails-with-powershell.html

Sign up to request clarification or add additional context in comments.

1 Comment

i am using powershell version 4 . After running this scrip i got below error ----- Exception calling "Send" with "1" argument(s): "The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at" At line:23 char:1 + $smtp.send($message) + ~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : SmtpException
2

"The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required."

I recently ran into this issue with attempting to automate messages through my Gmail account. I do not like the option of allowing "access for less secure apps". I want forward thinking security. So I continued my search.

My solution was to enable "2-Step" verification. This provides a slightly more secure solution as it provides an alternate password for your script to access your account.

Sign in using App Passwords:
https://support.google.com/accounts/answer/185833

Stefan's link also has this solution, but it's buried in the comments and I didn't originally find it there until after I found it on my own through searching my Gmail account. That's why I am posting it here.

Comments

1

Send email with attachment using powershell -

 $EmailTo = "[email protected]" // [email protected] $EmailFrom = "[email protected]" //[email protected] $Subject = "zx" //subject $Body = "Test Body" //body of message $SMTPServer = "smtp.gmail.com" $filenameAndPath = "G:\abc.jpg" //attachment $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body) $attachment = New-Object System.Net.Mail.Attachment($filenameAndPath) $SMTPMessage.Attachments.Add($attachment) $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587) $SMTPClient.EnableSsl = $true $SMTPClient.Credentials = New-Object System.Net.NetworkCredential("[email protected]", "xxxxxxxx"); // xxxxxx-password $SMTPClient.Send($SMTPMessage) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.