title: How can I use encrypted secure password in smtp credentials
body: <p>In my .ps1 file I want to execute a job and send the result through email. I want the password to not be readable, so I encrypt it by doing this on beforehand.</p>
  
  <pre><code>$password = read-host -prompt "Enter your Password"
  write-host "$password is password"
  $secure = ConvertTo-SecureString $password -force -asPlainText
  $bytes = ConvertFrom-SecureString $secure
  $bytes | out-file .\securepassword.txt
  </code></pre>
  
  <p>Works just fine so far.</p>
  
  <p>Then I want to use the secured password inside the .ps1, which is where I'm having difficulties. I've tried a bunch of variations, but I think this is my most solid attempt so far:</p>
  
  <pre><code> $usercred = "myemail@gmail.com"
   $encryptedpw = Get-Content .\securepassword.txt
   $pwcred = $ecryptedpw | ConvertTo-SecureString
  </code></pre>
  
  <p>When I try to use this in my <code>$smtp.credentials</code> like this:</p>
  
  <pre><code>$smtp.Credentials = New-Object System.Net.NetworkCredential($usercred, $pwcred);
  </code></pre>
  
  <p>it outputs the following error <code>The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.</code> at the <code>$smtp.send($msg)</code></p>
  
  <p>And I know the password is the issue (at least I'm pretty sure), because if I manually type it in - it works just fine. Any ideas?</p>
  
  <p><strong>_ edit 1 _</strong></p>
  
  <p>Below is the whole send-mail part if anyone wonder:</p>
  
  <pre><code># Send Notification if alert $i is greater then 0 
  if ($i -gt 0) 
  { 
      foreach ($user in $users) 
  { 
  
    $smtpServer = "smtp.gmail.com" 
    $port = "587"
    $smtp.EnableSSL = $true
    $smtp = New-Object Net.Mail.SmtpClient($smtpServer, $port) 
    $smtp.EnableSsl = $true 
    $smtp.Credentials = New-Object System.Net.NetworkCredential($usercred, $pwcred);
    $msg = New-Object Net.Mail.MailMessage 
    $msg.To.Add($user) 
          $msg.From = "myemail@gmail.com" 
    $msg.Subject = "Environment DiskSpace Report for $titledate" 
          $msg.IsBodyHTML = $true 
          $msg.Body = get-content $blablabla
    $smtp.Send($msg) 
          $body = "" 
      } 
    } 
  </code></pre>