PowerShell - Send-MailMessage with AWS SES - Intermittent

Viewed 716

I've been using the below script to send email and sometimes it works, sometimes it does not. I'm not sure exactly why it is intermittent and with this error:

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

This is the script I'm using:

$AWS_ACCESS_KEY = "KEYYYYYYYYYY"
$AWS_SECRET_KEY = "SECRET_KEYYYYYYYYYYYYYY"
$SECURE_KEY = $(ConvertTo-SecureString -AsPlainText -String $AWS_SECRET_KEY -Force)
$creds = $(New-Object System.Management.Automation.PSCredential ($AWS_ACCESS_KEY, $SECURE_KEY))

$smtp = "email-smtp.us-east-1.amazonaws.com"
$from = "noreply@domain.com"
$to = "to_you@domain.com"
$bcc = "its_me@domain.com"
$subject = "Hello There"
$body = "Hey Friend!<br><br>"
$body += "Body<br><br>"
$body += "Regards,<br>"
$body += "Bye<br>"

#Sending email
Sleep 2
Send-MailMessage -From $from -To $to -Bcc $bcc -Subject $subject -Body $body -BodyAsHtml -SmtpServer $smtp -Credential $creds -UseSsl -Port 587

What could be the issue? Has anyone seen this before? Any log in AWS console that I could look at?

Thanks!

1 Answers

I also faced a similar issue managed to resolve with the below codes.

    $smtpServer = "email-smtp.us-east-1.amazonaws.com" 
$smtpPort = 587  
$username = "************"
$password = "***********************"  
$from = "alerts@test.com"
$to = "recipient@domain.com"
$subject = "Amazon SES Test Email"
$body = "Body Amazon SES Test Email"
$cc = "xyz@domain.com"
$smtp = new-object Net.Mail.SmtpClient($smtpServer, $smtpPort)
$smtp.EnableSsl = $true 
$smtp.Credentials = new-object Net.NetworkCredential($username, $password)
$msg = new-object Net.Mail.MailMessage
$msg.From = $from
$msg.To.Add($to)
$msg.CC.Add($cc)
$msg.Subject = $subject
$smtp.Send($msg)
$msg.Body = $body
Related