Blog

Sending email via Office 365 using PowerShell

January 22, 2020

This post is really more about the potential problems one might encounter using Send-MailMessage cmdlet in PowerShell to connect and send email via Office 365.

First a quick example:

[SecureString]$o365Password = ConvertTo-SecureString "Your Office 365 Account Password" -AsPlainText -Force
[PSCredential]$o365Credentials = New-Object System.Management.Automation.PSCredential("Your Office 365 Account Username",$o365Password)
Send-MailMessage `
  -Subject "Your Subject" `
  -Body "Your email message" `
  -To "toSomeone@somewhere.com" `
  -From "fromSomeone@somewhereelse.com" `
  -SmtpServer "outlook.office365.com" `
  -Port 587 `
  -Credential $o365Credentials `
  -UseSsl

If the above settings are set correctly the email should be sent.

Some errors you may encounter:

Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send 
anonymous mail during MAIL FROM [XXXXXXXXXXXXXXX.XXXXXXXX.prod.outlook.com]
  • Possibly expired or deactivated Office 365 account
  • Confirm username and password by logging into Outlook web access
  • Confirm connecting on port 587
  • Confirm -UseSSL parameter provided
Send-MailMessage : Unable to connect to the remote server
  • SmtpServer parameter is set to “outlook.office365.com”. Lots of other solutions suggest alternatives that may have worked in the past, but did not work for me.

0 Comments

Leave Your Comment

Your email address will not be published. Required fields are marked *


about me

An information technology professional with twenty year's experience in systems administration, computer programming, requirements gathering, customer service, and technical support.