The Need to Upgrade PowerShell Scripts that Send Email via Exchange Online

One of my posts describes a PowerShell script which uses the Send-MailMessage cmdlet to send messages from an Exchange Online mailbox, in this case to generate a welcome message to new tenant users. Send-MailMessage uses a SMTP AUTH connection, one of the methods the Exchange Online development group wants to upgrade to use modern authentication in their drive to eliminate basic authentication.

Microsoft hasn’t said exactly when SMTP AUTH connections will be able to use modern authentication, but it will happen, and it’s good to be prepared.

Update: OAuth support is now available for SMTP AUTH.

Upgrade for Modern Authentication

If you use the Send-MailMessage cmdlet with Exchange Online, you’ll have to upgrade your scripts to make sure that they continue working when the block on basic authentication descends. It’s a good idea to take an inventory of scripts to know where work needs to be done.

But you shouldn’t only focus on Send-MailMessage. Older scripts might use a combination of the .NET SmtpClient and MailMessage classes to send email from PowerShell. These classes are more general-purpose because they’re designed to be used to connect with a range of SMTP servers from programs built with different languages. However, the same SMTP AUTH restrictions apply when scripts use these classes to send email with Exchange Online.

Using .NET Classes to send Email from PowerShell

Here’s an example of using the .NET classes to send a message. You can do everything shown here with Send-MailMessage except add an x-header.

# Example of how to send a message using the .NET SmtpClient and MailMessage classes
If (-not $O365Cred) { #Make sure we have credentials
    $O365Cred = (Get-Credential)}
$MsgFrom = "Info@office365itpros.com"
$MsgTo = "Mybestfriend@outlook.com"
$SmtpServer = "smtp.office365.com" ; $SmtpPort = "587"
# Build Message Properties
$Message = New-Object System.Net.Mail.MailMessage $MsgFrom, $MsgTo
$Message.Subject = "Example Message" 
$Message.Attachments.Add("C:\Temp\Office365TenantUsage.csv")
$Message.Headers.Add("X-O365ITPros-Header","Important Email")
$Message.Body = Get-Content c:\temp\textforemail.html
$Message.IsBodyHTML = $True
# Build the SMTP client object and send the message off
$Smtp.EnableSsl = $True
$Smtp.Credentials = $O365Cred
$Smtp = New-Object Net.Mail.SmtpClient($SmtpServer, $SmtpPort)
$Smtp.Send($Message)

SmtpClient is Deprecated

Apart from the need to update scripts using the .NET classes when Microsoft deprecates basic authentication for SMTP AUTH, you should be aware that the SmtpClient class is already deprecated and could therefore become unavailable to PowerShell in the future. For that reason, while you’re scanning scripts to prepare to upgrade them for modern authentication, make a mental note to look out for these classes so that the code is also upgraded when you update with a more modern method to send email.


Stay updated with change inside Exchange Online by subscribing to the Office 365 for IT Pros eBook. We stay ahead of the game so you don’t have to worry.

9 Replies to “The Need to Upgrade PowerShell Scripts that Send Email via Exchange Online”

  1. Hey, feel like I’m missing something here – but although you’ve said to prepare for Send-MailMessage’s deprecation, you haven’t mentioned any PowerShell alternatives?

    1. There’ll be an alternative when Microsoft announces how they will upgrade Send-MailMessage. This will either be a new cmdlet or an upgrade to the existing cmdlet to support modern authentication. For now, all we can do is to inventory the scripts that use the cmdlet in preparation for a change that is coming.

  2. Still can’t seem to find an alternative for ‘Send-MailMessage’. Without an alternative, we are unable to disable all legacy authentication methods as MS suggests.

      1. Thanks for the link Tony, I was not aware of the option to use the Graph API. I’ll try that out

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.