Upgrading PowerShell Scripts that Use Exchange Online to Send Email

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.

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.

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 you can replace them with Send-MailMessage.


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.

Advertisements

2 Replies to “Upgrading PowerShell Scripts that Use Exchange Online to Send Email”

  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.

Leave a Reply

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