---
title: "Connect Microsoft 365 mail with OAuth2"
site: admin
version: 2027.1
versions: "all"
url: https://docs-new.servicely.ai/admin/2027.1/messaging/connect-microsoft-365-oauth2/
type: task
application: system_email
last_updated: 2026-12-03
commit: 97a56b9c7df7
---


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 `administrator` role 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

<Steps>
  <Step>
    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](https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-register-app).
  </Step>
  <Step>
    In the Microsoft Entra admin centre, add a redirect URI for the **Web** platform. Enter your instance address followed by `/sso/oauth_callback`, for example `https://<instance>.servicely.ai/sso/oauth_callback`. See [How to add a redirect URI to your application](https://learn.microsoft.com/en-us/entra/identity-platform/how-to-add-redirect-uri).
  </Step>
  <Step>
    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.Send` and `User.Read`. See [Web API app registration and API permissions](https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-configure-app-access-web-apis).
  </Step>
  <Step>
    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](https://learn.microsoft.com/en-us/entra/identity-platform/how-to-add-credentials).
  </Step>
  <Step>
    In Servicely, go to **Integrations → API access → System OAuth providers**.

    <Image src="asset:screenshots/admin/integrations-connect-an-oauth2-provider/nav-api-access.png">The System OAuth providers item in the API access menu of Integrations.</Image>
  </Step>
  <Step>
    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.

    <Image src="asset:screenshots/admin/messaging-connect-microsoft-365-oauth2/oauth-provider.png">A new System OAuth Provider filled from the Microsoft template, with the [tenant-id] placeholder in the Authorization URL and Token URL.</Image>
  </Step>
  <Step>
    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. | |
  </Step>
  <Step>
    Go to **Integrations → API access → System API outbound tokens** and select **New**.
  </Step>
  <Step>
    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 email` |
    | **Allowed 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. | |

    <Image src="asset:screenshots/admin/messaging-connect-microsoft-365-oauth2/outbound-token.png">A new System API Outbound Token of type OAuth2 with the mail scopes entered and State Draft.</Image>
  </Step>
  <Step>
    Select **Get authorization token** and sign in to Microsoft as the mailbox user, not as an administrator.
  </Step>
  <Step>
    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.
  </Step>
  <Step>
    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](page:messaging/create-a-messaging-account).

    <Image src="asset:screenshots/admin/messaging-connect-microsoft-365-oauth2/account-oauth.png">An IMAP (Inbound Email) messaging account with Authentication type OAuth 2 and the required Outbound API token field.</Image>
  </Step>
</Steps>

## 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](page:messaging/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.

1. On the token, leave **OAuth Prompt** as consent and select **Get authorization token**.
2. Sign in to Microsoft as a Microsoft Entra ID administrator and accept on behalf of the organisation.
3. Change **OAuth Prompt** to select_account and save the token.
4. Select **Get authorization token** again and sign in as the mailbox user.

[Outbound credentials](page:integrations/outbound-credentials) lists the **OAuth Prompt** values.

## Related

- [Create a messaging account](page:messaging/create-a-messaging-account)
- [Connect Microsoft 365 mail with Graph API](page:messaging/connect-microsoft-365-graph-api)
- [Outbound credentials](page:integrations/outbound-credentials)
- [Connect to an OAuth2 provider](page:integrations/connect-an-oauth2-provider)
