Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Send-MailMessage script runs perfect on PSv3, but cannot authenticate user with PS v2

$
0
0

This drives me nuts: the code below runs perfectly in PowerShell Version 3. I can authenticate and send mail via live.com and googlemail.com. The password is "hardcoded" in the script, to avoid typos.

If I run it on the same PC (or any other) in PowerSehll v2, I get the following errors

(a) Live.com: Mailbox unavailable. The server response was: 5.7.3 Requested action aborted; user not authenticated (b) Gmail.com: Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.

The script:

#requires -version 2.0"PSHost v" + (Get-Host).Version

$pwlive   = ConvertTo-SecureString 'PASSWORD' -as -f 
$userlive = 'johnsmith@live.com'
$smtplive = 'smtp.live.com'

$pwgmail   = ConvertTo-SecureString 'PASSWORD' -as -f 
$usergmail = 'johnsmith@googlemail.com'
$smtpgmail = 'smtp.googlemail.com'

$rcpt = 'annaexample@yahoo.com'
$text = "Testmail"

$credlive = new-object System.Management.Automation.PSCredential $userlive,$pwlive
$credgmail = new-object System.Management.Automation.PSCredential $usergmail,$pwgmail

# 1st try
"Send-MailMessage: Trying Live ..."
Send-MailMessage -SmtpServer $smtplive -UseSsl -to $rcpt -From $userlive -Subject "1st try: Powershellmail via Live.com, $(Get-Date -format hh.mm) Uhr" -Credential $credlive -Body $text -encoding ([System.Text.Encoding]::UTF8) 

# 2nd try
"Send-MailMessage: Trying Gmail .."
Send-MailMessage -SmtpServer $smtpgmail -UseSsl -to $rcpt -From $usergmail -Subject "2nd try: Powershellmail via Gmail.com, $(Get-Date -format hh.mm) Uhr" -Credential $credgmail -Body $text -encoding ([System.Text.Encoding]::UTF8) 

# 3rd try
"Net.Mail.SmtpClient: Trying Live ..."
$SMTPClient = New-Object Net.Mail.SmtpClient($Smtplive, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = $credlive
$SMTPClient.Send($userlive, $rcpt, "3rd try: [Net.Mail.SmtpClient]-Mail via Live.com, $(Get-Date -format hh.mm) Uhr", $text)
# 4th try"Net.Mail.SmtpClient: Trying Gmail ..."
$SMTPClient = New-Object Net.Mail.SmtpClient($Smtpgmail, 587) 
$SMTPClient.EnableSsl = $true 
$SMTPClient.Credentials = $credgmail
$SMTPClient.Send($usergmail, $rcpt, "4th try: [Net.Mail.SmtpClient]-Mail via Live.com, $(Get-Date -format hh.mm) Uhr", $text)


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>