title: Using encrypted password to send mail is failing
body: <p>I'm trying to use a stored encrypted password in a script that sends an email but I keed getting the error:</p>
  
  <blockquote>
    <p>send-mailmessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM 
    [DM6PR66666019.namprd456.prod.outlook.com]</p>
  </blockquote>
  
  <p>I used the following code to create the text file:</p>
  
  <pre><code>$passwordtostore = 'NotTheRealPassword$9gv8z6VHnPfDd8zc'
  $secureStringPWD = $passwordtostore | ConvertTo-SecureString -AsPlainText -Force
  $secureStringText = $secureStringPWD | ConvertFrom-SecureString
  Set-Content "c:\temp\scriptsencrypted_password1.txt" $secureStringText
  </code></pre>
  
  <p>I use the following to import the password:</p>
  
  <pre><code>$passwordFile = "c:\temp\scriptsencrypted_password1.txt"
  $password = Get-Content $passwordFile | ConvertTo-SecureString
  </code></pre>
  
  <p>Here's the sendmail function I am using:</p>
  
  <pre><code>function SendMail($ToEmails,$FromEmail,$Subj,$Body,$UserName,$Password){
      $cred = New-Object System.Management.Automation.PSCredential $UserName,$Password
  
      $MailParams=@{"from"=$FromEmail; "to"=$ToEmails;"body"=$Body;"subject"=$Subj;"smtpserver"="smtp.office365.com"}
  
      send-mailmessage @MailParams -Credential $cred -UseSsl $true -port 587
  }
  </code></pre>
  
  <p>Here's the code which calls the function:</p>
  
  <pre><code>$alertEmail = "me.stillme@mydomain.com"
  $username="psemail@mydomain.com"
  $passwordFile = "c:\temp\scriptsencrypted_password1.txt"
  $password = Get-Content $passwordFile | ConvertTo-SecureString
  $credential = New-Object System.Management.Automation.PsCredential($username, $password)
  
  Import-Module -Name "..\SendMail.psm1"
  
  ... Doing some stuff
  
  SendMail $alertEmail $username "This is the subject" "this is the body" $credential.UserName $credential.Password
  </code></pre>