Alright so as the title says, I get this error when trying to send email via PowerShell:
Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.
I have looked at numerous questions related to the same issue. But I can't seem to make my script work:
#Email alerts when a user gets locked out
##############################################################################
$pass = Get-Content .\securepass.txt | ConvertTo-SecureString -AsPlainText -Force
$name = "sender@gmail.com"
$cred = New-Object System.Management.Automation.PSCredential($name,$pass)
##############################################################################
$From = "sender@gmail.com"
$To = "recipient@domain.org"
$Subject = "User Locked Out"
$Body = "A user has been locked out of his/her account."
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
Send-MailMessage -From $From -to $To -Subject $Subject `
-Body $Body -SmtpServer $SMTPServer -port $SMTPPort `
-Credential $cred -UseSsl
##############################################################################
I have logged into the Gmail account from the machine that will be running the script. I have also enabled Access for less secure apps from the Google account manager. I do get this to work just fine if I prompt for the credentials using the -Credential (Get-Credential)
instead of calling for the $cred
variable.
Is there something I am missing?
Thanks, Dan