Connect Microsoft 365 mail with OAuth2
Authenticate IMAP and SMTP messaging accounts to Microsoft 365 with OAuth2, from the Microsoft Entra ID app to the Servicely token.
Use OAuth2 when an IMAP or SMTP messaging account connects to a Microsoft 365 mailbox, because Microsoft no longer accepts password authentication. It is for administrators who manage both the Microsoft Entra ID app registration and the Servicely messaging accounts.
Before you begin
- You need the
administratorrole in Servicely. - You need permission to create app registrations in Microsoft Entra ID.
- You can sign in to Microsoft as the mailbox user. That user must hold a mailbox licence.
- The IMAP or SMTP messaging account exists. See Create a messaging account.
Servicely uses the same OAuth providers and API outbound tokens as its other integrations. Outbound credentials describes every field.
Steps
-
In the Azure portal, open App registrations and register a new application for Servicely. Note its application (client) ID and its tenant ID.
-
In Servicely, create a System OAuth Provider record from the Microsoft template. Enter the Client ID. Make sure the tenant ID appears in the Authorization URL and Token URL. Set Grant type to Authorization Code.

A System OAuth Provider form with the template menu open on Microsoft and the Redirect URL ending in /sso/oauth_callback. -
Copy the provider’s Redirect URL. In the app registration’s overview, open Redirect URIs, select Add a platform, choose Web and paste the URL. Save. The overview now shows “1 web” next to Redirect URIs.
-
Under API permissions, add these delegated Microsoft Graph permissions: email, IMAP.AccessAsUser.All, offline_access, openid, SMTP.Send and User.Read.

The API permissions page listing the six delegated Microsoft Graph permissions, each granted. -
Under Certificates & secrets, add a client secret. Copy its value into the provider’s Client Secret field and save the provider.
-
Create a System API Outbound Token. Set Type to OAuth2, select the provider in OAuth Provider, and enter the Scopes below.
offline_access https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/SMTP.Send openid email
A new System API Outbound Token of type OAuth2 with the email scopes entered and State New. -
Save the token and select Get authorization token. Sign in to Microsoft as the mailbox user, not as an administrator.
-
Select Accept on the Microsoft permissions page. Microsoft returns you to Servicely.

The Microsoft “Permissions requested” page asking to read and write mail and send email from the mailbox. -
Check that the token’s State is Ready and Expires holds a date.

The System API Outbound Token after authorisation, with State Ready and an Expires date. -
Open the messaging account. Set Authentication type to OAuth 2 and select the token in Outbound API token. Save, then select Test Connection.

An IMAP (Inbound Email) messaging account using OAuth 2 and the outbound API token.
| OAuth Prompt | Use |
|---|---|
| login | Microsoft asks for credentials even if a session exists |
| select_account | Microsoft asks which account to use |
| consent | Microsoft shows the consent page |
| none | Microsoft shows no prompt |
Result
The messaging account authenticates with the token instead of a password, and its connection test passes. Servicely renews the token with the offline_access scope. If the test fails or mail does not arrive, see Troubleshoot messaging accounts.