title: Send-MailMessage : The SMTP server requires a secure connection... The server response was: 5.5.1 Authentication Required.
body: <p>Alright so as the title says, I get this error when trying to send email via PowerShell: </p>
  
  <p><em>Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.</em> </p>
  
  <p>I have looked at numerous questions related to the same issue. But I can't seem to make my script work:</p>
  
  <pre><code>    #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
  ##############################################################################
  </code></pre>
  
  <p>I have logged into the Gmail account from the machine that will be running the script. I have also enabled <em>Access for less secure apps</em> from the Google account manager. I do get this to work just fine if I prompt for the credentials using the <code>-Credential (Get-Credential)</code> instead of calling for the <code>$cred</code> variable.</p>
  
  <p>Is there something I am missing?</p>
  
  <p>Thanks,
  Dan</p>