Setting up OAuth2 SMTP authentication for Microsoft 365
Log in to add to favouritesPage last updated 05 June 2026
Microsoft is retiring Basic Authentication for SMTP AUTH, with full deactivation on April 30, 2026. After this date, SMTP connections using username/password will stop working. OAuth2 is now required for sending email via Microsoft 365.
This guide walks you through configuring OAuth2 SMTP authentication using the Service Principal method (SMTP.SendAsApp), which is Microsoft's documented approach for application-based SMTP access.
Overview
To enable OAuth2 SMTP authentication, you need to:
- Create an App Registration in Microsoft Entra ID
- Register a Service Principal in Exchange Online and grant mailbox permissions
- Configure Contensis with the OAuth2 credentials
- Wait for permissions to propagate (typically 15-30 minutes)
Time Required: 20-30 minutes (plus propagation time)
Part 1: App Registration (Microsoft Entra Admin Center)
Step 1: Create the App Registration
- Sign in to the Microsoft Entra admin center
- Navigate to App registrations
- Click New registration
- Configure:
- Name:
Contensis SMTP OAuth2(or a name of your choosing) - Supported account types: "Single tenant"
- Redirect URI: Leave blank
- Name:
- Click Register
Step 2: Note the Application Details
After registration, copy these values (you'll need them later):
- Application (client) ID:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx - Directory (tenant) ID:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Step 3: Create a Client Secret
- In your app registration, go to Certificates & secrets
- Click New client secret
- Configure:
- Description: "SMTP OAuth2 Client Secret"
- Expires: Choose appropriate duration (e.g., 24 months)
- Click Add
- IMPORTANT: Copy the Value immediately (it won't be shown again)
Step 4: Grant API Permissions
- Go to API permissions
- Click Add a permission
- Select APIs my organization uses
- Search for and select: Office 365 Exchange Online
- Choose Application permissions
- Search for and add: SMTP.SendAsApp
- Click Add permissions
- CRITICAL: Click Grant admin consent for [Your Organization]
- You must be a Global Administrator or Application Administrator
- The status should show a green checkmark for all permissions
Part 2: Exchange Online Configuration (PowerShell)
This step registers a Service Principal in Exchange Online and grants it permission to send email from your mailbox. It cannot be done through any web portal — PowerShell is required.
Recommended: Use the Automated Script
Download and run Configure-ServicePrincipal.ps1 - it automates all of the steps below, including module installation, connection, service principal creation, and mailbox permission assignment.
# Right-click the script and select "Run with PowerShell"
# Or from a PowerShell prompt:
.\Configure-ServicePrincipal.ps1
The script will prompt you for:
- Application (Client) ID — from Part 1, Step 2
- Enterprise Application Object ID — follow the steps below to find the object ID
- In the Microsoft Entra admin center, go to Identity > Applications > App registrations
- Select your app, then click Overview
- Click the link under Managed application in local directory
- Copy the Object ID from the Enterprise Application page (this is different from the App Registration Object ID)
- Sender email address — the mailbox that will send emails
Alternative to the script: Manual steps
Prerequisites
- PowerShell 5.1 or PowerShell 7+
- Exchange Online Administrator role
- A mailbox with an Exchange Online licence that will be used as the sender
Step 1: Install Exchange Online Management Module
Open PowerShell as Administrator and run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Install-Module -Name ExchangeOnlineManagement -Scope CurrentUser -Force
Step 2: Connect to Exchange Online
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline
Sign in with an account that has Exchange Administrator permissions.
Step 3: Register the Service Principal
$AppId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" # Application (Client) ID
$ObjectId = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" # Enterprise Application Object ID
New-ServicePrincipal -AppId $AppId -ObjectId $ObjectId
# Verify
Get-ServicePrincipal | Where-Object {$_.AppId -eq $AppId}
Step 4: Grant Mailbox Permissions
$mailbox = "sender@yourdomain.com"
$ObjectId = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" # Enterprise Application Object ID
Add-MailboxPermission -Identity $mailbox -User $ObjectId -AccessRights FullAccess
# Verify
Get-MailboxPermission -Identity $mailbox | Where-Object {$_.User -like "*$ObjectId*"}
Step 5: Disconnect
Disconnect-ExchangeOnline -Confirm:$false
Part 3: Configure Contensis
Once the Service Principal is set up in Exchange Online, configure Contensis with the OAuth2 credentials.
- Navigate to Management Console > Global Settings
- Set the following values:
| Contensis Setting | Value |
|---|---|
EmailSMTPHost | smtp.office365.com |
EmailSmtpPort | 587 |
EmailSmtpUseSSL | true |
EmailSmtpUseOAuth2 | true |
EmailSmtpOAuth2AzureTenantId | Directory (Tenant) ID from Part 1, Step 2 |
EmailSmtpOAuth2ClientId | Application (Client) ID from Part 1, Step 2 |
EmailSmtpOAuth2ClientSecret | Client Secret Value from Part 1, Step 3 |
The sender email address (the "from" address) is configured per-notification or in your email settings — it must match a mailbox that was granted permissions in Part 2.
Part 4: Wait for Propagation
Service Principal permissions typically propagate within 15-30 minutes, but can occasionally take longer.
During this time, authentication attempts may fail with errors like:
- "535 5.7.3 Authentication unsuccessful"
- "Authentication failed"
What to do:
- Wait at least 15-30 minutes after granting mailbox permissions
- Send a test email from Contensis
- If still failing after 30 minutes, review the troubleshooting section below
Troubleshooting
If you need to diagnose issues after setup, download and run Diagnose-OAuth2Config.ps1 — it checks the service principal registration, mailbox permissions, and organisation-wide SMTP settings.
Common issues
Error: "535 5.7.3 Authentication unsuccessful"
Possible causes:
- Service Principal doesn't exist in Exchange — run Configure-ServicePrincipal.ps1
- Mailbox permissions not granted — run Configure-ServicePrincipal.ps1
- Permissions haven't propagated yet — wait 15-30 minutes
- Wrong API permission (using
Mail.Sendinstead ofSMTP.SendAsApp) — fix in the Entra admin center - Missing admin consent for
SMTP.SendAsApp— grant it (Part 1, Step 4) - Incorrect Object ID used (App Registration Object ID instead of Enterprise Application Object ID)
How to verify:
Connect-ExchangeOnline
# Check if service principal exists
Get-ServicePrincipal | Where-Object {$_.AppId -eq "your-app-id"}
# Check mailbox permissions
Get-MailboxPermission -Identity "sender@yourdomain.com" | Where-Object {$_.User -like "*your-object-id*"}
Or run Diagnose-OAuth2Config.ps1 to check everything automatically.
Error: "Mailbox not found"
Causes:
- Mailbox doesn't have an Exchange Online licence
- Email address is incorrect (check for typos)
- User account is disabled
Solution:
- Verify the mailbox in Microsoft 365 Admin Center
- Assign an Exchange Online licence
- Use the exact email address
Service Principal Not Found
Causes:
- Service principal wasn't created in Exchange
- Wrong Object ID used (must be Enterprise Application Object ID)
- Service principal creation failed silently
Solution:
# List all service principals to verify
Get-ServicePrincipal | Format-List AppId, ObjectId, DisplayName
# If not found, recreate it
New-ServicePrincipal -AppId "your-app-id" -ObjectId "your-enterprise-object-id"
SMTP AUTH Disabled Organisation-Wide
Check organisation settings:
Get-TransportConfig | Select-Object SmtpClientAuthenticationDisabled
If True, SMTP AUTH is blocked organisation-wide. Contact your Exchange administrator to enable it.
Granting Access to Multiple Mailboxes
If you need multiple mailboxes to send mail through the application, grant permissions to each mailbox:
$ObjectId = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" # Enterprise Application Object ID
Add-MailboxPermission -Identity "user1@yourdomain.com" -User $ObjectId -AccessRights FullAccess
Add-MailboxPermission -Identity "user2@yourdomain.com" -User $ObjectId -AccessRights FullAccess
Add-MailboxPermission -Identity "user3@yourdomain.com" -User $ObjectId -AccessRights FullAccess
Now any of these mailboxes can be used as the sender email address in Contensis.
Summary Checklist
- App Registration created in Microsoft Entra ID
- Application (Client) ID, Directory (Tenant) ID, and Enterprise Application Object ID noted
- Client Secret created and saved securely
SMTP.SendAsApppermission granted with admin consent- Service Principal created in Exchange Online (via script or manually)
- Mailbox permissions granted to Service Principal (
FullAccess) - Waited 15-30 minutes for propagation
- Contensis configured with OAuth2 settings (Part 3)
- Test email sent successfully
Support
If you encounter issues not covered in this guide:
- Run Diagnose-OAuth2Config.ps1 to check your configuration
- Verify all steps were completed exactly as written
- Wait 30 minutes for propagation if you haven't already
- Check the Microsoft 365 Service Health Dashboard for known issues
- Review Exchange Online logs and message trace
- Raise a helpdesk ticket