wakatashi
asked on
Powershell 2.0 - send e-mail via Office 365
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:
...but it fails with the following error:
How can I make this work in Powershell v2.0, please?
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 = "user@domain.com"
$To = "recipient@anotherdomain.com"
$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"
$Username = "user@domain.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
ion
+ FullyQualifiedErrorId : SmtpException,Microsoft.PowerShell.Commands.SendMailMessage
How can I make this work in Powershell v2.0, please?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try changing line $SMTPClient.Credentials = $mycreds
to
to
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
ASKER
I'm afraid not - I get the same error as last time. That's a neater way of encoding the plain text credentials though!
Does the credentials work when you send mail from outlook using the smtp server?
ASKER
I've been trying on on and off for about 3 months to get this to work, and the credentials were fine when I started on this! But since then it seems that BT (who provide the Office 365 mailbox) have disabled the mailbox, and so the credentials were being rejected. I've re-enabled the mailbox, and now the script, with your modifications, works fine.
Thank you very much for your help, Subsun! Working script is:
Thank you very much for your help, Subsun! Working script is:
$From = "user@domain.com"
$To = "recipient@anotherdomain.com"
$SMTPServer = "smtp.office365.com"
$SMTPPort = "587"
$Username = "user@domain.com"
$Password = "plaintextpassword"
$subject = "This is the subject line"
$body = "This is the message body"
$SMTPClient = New-Object Net.Mail.SmtpClient($SMTPServer,$SMTPPort)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$SMTPClient.Send($From, $To, $subject, $Body)
ASKER
When I replace the send-mailmessage line with the code above, I get the following:
Open in new window