How To: Test Exchange Online SMTP on Port 587 with PowerShell

How-To PowerShell Jan 24, 2024

Prerequisites

The following PowerShell steps are to test SMTP email. This example is for Exchange Online, but you can use any other SMTP server to test. You will need PowerShell 3.0 or higher.

First, we need to save the credentials into a variable. Use the command below to save credentials to "$creds" variable.

$creds = get-credential

A window will pop-up and you need to enter your Office 365 username and password.

Once the credentials are stored in the variable, use the snippet below to build your query in NotePad or preferred text editor and replace the "FROM" field with your Office 365 account / email alias that you have specified in your credentials, then specify a valid "TO" email address. This can be your gmail account or yahoo account.

Send-MailMessage -From <O365 email address> -To <Recipient email address> -Subject "Test Email" -Body "Test SMTP Service from Powershell on Port 587" -SmtpServer smtp.office365.com -Credential $creds -UseSsl -Port 587

If you get the error message below, then it means you have the Tenant Security Defaults enabled.

Send-MailMessage : The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail. Error: 535 5.7.139 Authentication unsuccessful, user is locked by your organization's security defaults policy. Contact your administrator.
[LO4P265CA0035.GBRP265.PROD.OUTLOOK.COM 2024-01-24T19:00:16.642Z 08DC1C82E4AED9C6]
At line:1 char:1

Exchange Online SMTP Requirements

There are 2 requirements to be able to send emails with Exchange Online:

1) Have SMTP AUTH enabled for the tenant

This is by default disabled by Microsoft.

Check here to enable SMTP AUTH for the whole organization.

Enable or disable SMTP AUTH in Exchange Online
Summary: An overview of SMTP AUTH in Exchange Online.

2) Have Tenant Security Defaults disabled for the tenant.

By default, Microsoft enables Security Defaults for the tenant.

If you already setup Conditional Access and other security bits enabled, then you can disable the Security Defaults by checking the link below.

Providing a default level of security in Microsoft Entra ID - Microsoft Entra
Get protected from common identity threats using Microsoft Entra security defaults

More information about the details of how to send email using Microsoft Exchange Online can be found here:

How to set up a multifunction device or application to send emails using Microsoft 365 or Office 365
Learn how to set up printers, scanners, LOB applications to send emails using Microsoft 365 or Office 365.

Tags