0

Part 2 of an earlier question here - I know have a variable which contains a password in plaintext, and a username in plaintext which happens to be a email address. I need to use these in order to send an email via SMTP in powershell, Send-MailMessage for example.

So I need to pass it:

personx@persony.com and $password

In order to authenticate against a smart host the send the email. Any way of doing this, something clever with Get-Credential or some switch on Send-MailMessage, or is there another totally different option?

Cheers

Community
  • 1
  • 1
PnP
  • 3,133
  • 17
  • 62
  • 95
  • quick search gives this: http://stackoverflow.com/questions/6239647/using-powershell-credentials-without-being-prompted-for-a-password . check out the line starting With `$cred` and supply it to send-mailmessage -credential – Frode F. Dec 21 '12 at 17:06

1 Answers1

2

You can use ConvertTo-SecureString to convert your plain text password.

Assuming your username is in $username variable and plain text password in $password, you can do something like this:

$securepass = ConvertTo-SecureString -AsPlainText -String $password -Force
$creds = New-Object System.Management.Automation.PSCredential -argumentlist $username,$securepass

Send-MailMessage ... -Credentials $creds
Nasir
  • 10,935
  • 8
  • 31
  • 39