Connect Microsoft 365 mail with OAuth2
Create the OAuth provider and token that let IMAP and SMTP messaging accounts sign in to a Microsoft 365 mailbox.
Use OAuth2 when an IMAP or SMTP messaging account connects to a Microsoft 365 mailbox, because Microsoft 365 does not accept a password. You create an OAuth provider, then an outbound token, then the messaging accounts that use the token.
Before you begin
- You need the
administratorrole in Servicely. - Switch to the Administration workspace with the Workspace selector at the foot of the menu.
- You need permission to create app registrations in Microsoft Entra ID.
- You can sign in to Microsoft as the mailbox user. That user holds a mailbox licence.
Steps
-
In the Microsoft Entra admin centre, register an application for Servicely. Copy its application (client) ID and its directory (tenant) ID. Microsoft describes the screen in How to register an app in Microsoft Entra ID.
-
In the Microsoft Entra admin centre, add a redirect URI for the Web platform. Enter your instance address followed by
/sso/oauth_callback, for examplehttps://<instance>.servicely.ai/sso/oauth_callback. See How to add a redirect URI to your application. -
In the Microsoft Entra admin centre, open the application’s API permissions. Add the delegated Microsoft Graph permissions
email,IMAP.AccessAsUser.All,offline_access,openid,SMTP.SendandUser.Read. See Web API app registration and API permissions. -
In the Microsoft Entra admin centre, open Certificates & secrets and create a client secret. Copy its value. See Add and manage app credentials in Microsoft Entra ID.
-
In Servicely, go to Integrations → API access → System OAuth providers.

The System OAuth providers item in the API access menu of Integrations. -
Select New, open the Templates menu at the top right and select Microsoft. The template fills in the URLs and sets Grant type to Authorization Code.

A new System OAuth Provider filled from the Microsoft template, with the [tenant-id] placeholder in the Authorization URL and Token URL. -
Fill in the provider, then select Create.
Field What to enter Example Name A name for the provider. Microsoft 365 mail Client ID The application (client) ID you copied. Client Secret The client secret value you copied. Authorization URL, Token URL Replace [tenant-id]with the directory (tenant) ID you copied.Redirect URL Leave the value. It is the redirect URI you entered in Microsoft Entra ID. -
Go to Integrations → API access → System API outbound tokens and select New.
-
Fill in the token, then select Create.
Field What to enter Example Name A name for the token. Key fills in from it. Microsoft 365 mailbox Type OAuth2. OAuth Provider The provider you created. Microsoft 365 mail Scopes The mail scopes, separated by spaces. offline_access https://outlook.office.com/IMAP.AccessAsUser.All https://outlook.office.com/SMTP.Send openid emailAllowed Hosts Leave it empty, or enter the IMAP and SMTP server names the accounts use. A token is refused for a server that is not listed. 
A new System API Outbound Token of type OAuth2 with the mail scopes entered and State Draft. -
Select Get authorization token and sign in to Microsoft as the mailbox user, not as an administrator.
-
In the Microsoft sign-in window, select Accept on the “Permissions requested” page. Microsoft returns you to the token, and State changes from Draft to Ready.
-
Create the messaging accounts. Use the Microsoft IMAP inbound and SMTP outbound templates in the Templates menu, and select the token in Outbound API token on each. See Create a messaging account.

An IMAP (Inbound Email) messaging account with Authentication type OAuth 2 and the required Outbound API token field.
Result
The token’s State is Ready and Expires holds a date. Messaging accounts that name the token sign in with it, and Servicely renews it before it expires. An account whose token is missing or inactive stops on its first run. See Troubleshoot messaging accounts.
Variations
Consent for the whole organisation
A Microsoft Entra ID administrator can consent once for the whole organisation, so the mailbox user is not asked to.
- On the token, leave OAuth Prompt as consent and select Get authorization token.
- Sign in to Microsoft as a Microsoft Entra ID administrator and accept on behalf of the organisation.
- Change OAuth Prompt to select_account and save the token.
- Select Get authorization token again and sign in as the mailbox user.
Outbound credentials lists the OAuth Prompt values.