I was expecting this to be easy, but it seems that there are many ways to skin this particular cat, and some of them require Powershell version 3 or 4.
I'm running Powershell 2 under Windows Server 2008 R2. I'd like to use Powershell to send an e-mail message via Office 365's SMTP server. This requires SSL on port 587, and authenticated SMTP. I know that the SMTP server, port, and credentials I'm using are correct.
This is what I have so far:
$From = "email@example.com"
$To = "firstname.lastname@example.org"
$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"
$Username = "email@example.com"
$Password = "plaintextpassword"
$subject = "This is the subject line"
$body = "This is the message body"
$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$mycreds = New-Object System.Management.Automation.PSCredential ($Username, $secpasswd)
send-mailmessage -from $From -to $To -subject $subject -body $body -smtpserver $SMTPServer -credential $mycreds -UseSSL
...but it fails with the following error:
Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
At C:\test\mailtest.ps1:15 char:17
+ send-mailmessage <<<< -from $From -to $To -subject $subject -body $body -smtpserver $SMTPServer -credential $mycreds -UseSSL
+ CategoryInfo : InvalidOperation: (System.Net.Mail.SmtpClient:SmtpClient) [Send-MailMessage], SmtpExcept
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
How can I make this work in Powershell v2.0, please?