# Connect Microsoft 365 and Outlook.com

> Connect Outlook.com and Microsoft 365 mailboxes through Microsoft Graph, with your own Microsoft Entra app, browser sign-in or a sign-in code.

Source: https://docs.posternmail.app/providers/microsoft/

This page covers Outlook.com, Hotmail, Live and MSN addresses, and work or school accounts on
Microsoft 365. The Hub talks to them through **Microsoft Graph**, signed in with your Microsoft
account. Passwords and app passwords do not work: Microsoft turned off password sign-in for
mail on these accounts.

Before anyone can add a Microsoft mailbox, the Hub's admin registers a free app in Microsoft
Entra once and gives its client ID to the Hub. Postern ships no shared Microsoft app.

## How sign-in works

There are two ways, and the dialog offers whichever your Hub can do:

| | Sign in with Microsoft (browser) | Sign in with a code |
|---|---|---|
| Button | **Sign in with Microsoft** | **Sign in with Microsoft using a code** (or **Sign in with Microsoft** when it is the only way) |
| What happens | Microsoft's sign-in page opens and sends you back to the Hub. | Postern shows a short code; you enter it at `microsoft.com/devicelogin` on any device. |
| Needs | The Hub opened over `https://` or on `localhost`, and a redirect URI in the app registration | **Allow public client flows** in the app registration |
| Blocked by | Nothing special | Organizations with Entra **security defaults** or policies that block the device code flow |

When the Hub has an `https://` address, the browser sign-in comes first and the code is the
fallback. On a plain `http://` LAN address only the code is offered. The phone apps always use
the code.

Either way, the tokens go to your Hub only. The device where you type the code never sees them.

## What you need

- A Microsoft account that can create app registrations in Microsoft Entra. Any Microsoft
  account can (personal accounts get a default directory). For a company, an Entra admin may
  have to do it or approve the app.
- Admin rights on the Hub.
- For the browser sign-in: the Hub on `https://` (see [HTTPS](https://docs.posternmail.app/reach/https/)) and
  `POSTERN_PUBLIC_URL` set to that address (see [Configuration](https://docs.posternmail.app/configuration/)).

## 1. Register the app in Microsoft Entra

Microsoft's guide: <https://learn.microsoft.com/en-us/entra/identity-platform/quickstart-register-app>.

1. Open <https://entra.microsoft.com/> and sign in.
2. Go to **Entra ID → App registrations** (older menus: **Identity → Applications → App
   registrations**) and click **New registration**.
3. **Name:** `Postern Hub`.
4. **Supported account types:** pick the one that fits the mailboxes you will connect. It
   decides the tenant value in step 4.

   | Supported account types | Mailboxes | Tenant value |
   |---|---|---|
   | Accounts in any organizational directory and personal Microsoft accounts | Outlook.com and work or school accounts | `common` |
   | Personal Microsoft accounts only | Outlook.com, Hotmail, Live only | `consumers` |
   | Accounts in any organizational directory | Work or school accounts of any company | `organizations` |
   | Accounts in this organizational directory only | Your company only | your tenant ID or domain, for example `example.com` |

5. Leave **Redirect URI** empty for now and click **Register**.

> **Check:** the app's **Overview** page shows an **Application (client) ID** (a GUID such as
> `00000000-0000-0000-0000-000000000000`).

> **If it did not work:** if **New registration** is greyed out, your organization does not
> let users register apps. Ask an Entra admin.

## 2. Set up authentication

Open the app's **Authentication** page.

1. **For the code sign-in:** under **Advanced settings**, set **Allow public client flows** to
   **Yes** and click **Save**.
2. **For the browser sign-in** (only if your Hub is on `https://` or `localhost`): click **Add a
   platform → Mobile and desktop applications**, enter a custom redirect URI and click
   **Configure**:

   ```text
   https://mail.example.com/api/v1/oauth/callback/microsoft
   ```

   It is your Hub's address followed by `/api/v1/oauth/callback/microsoft`, exactly, without a
   trailing slash. Use **Mobile and desktop applications**, not **Web**: the Hub signs in as a
   public client without a secret, and Microsoft rejects that for a **Web** redirect.

You do not need a client secret or a certificate.

> **Check:** **Authentication** shows **Allow public client flows: Yes** and, if you added it,
> the redirect URI under **Mobile and desktop applications**.

> **If it did not work:** if you skip the redirect URI, the browser sign-in fails with
> `AADSTS50011` and you can still use the code.

## 3. Add the permissions

Open **API permissions → Add a permission → Microsoft Graph → Delegated permissions** and add:

| Permission | Why |
|---|---|
| `Mail.ReadWrite` | Read mail, flag, move, delete, manage folders and categories |
| `Mail.Send` | Send mail |
| `User.Read` | Read the mailbox address of the account that signed in |
| `offline_access` | Get a refresh token, so the Hub keeps syncing without asking you again |
| `IMAP.AccessAsUser.All` | The push connection: the Hub keeps one IMAP connection open to learn about new mail at once |

The Hub asks for exactly these at sign-in. Adding them here is what lets an admin approve them
for the whole organization in advance. In a company where users may not consent to apps
themselves, an admin clicks **Grant admin consent for (your organization)** on the same page.
Microsoft's guide: <https://learn.microsoft.com/en-us/entra/identity/enterprise-apps/grant-admin-consent>.

> **Check:** the list shows the five permissions; with admin consent, each says **Granted for
> (your organization)**.

> **If it did not work:** without `IMAP.AccessAsUser.All` everything still works, but new mail
> is only picked up by the regular checks (every 45 seconds).

## 4. Give the client ID to your Hub

Either use the admin panel or the Hub's configuration. A value in the configuration wins: the
admin panel then shows **Set in the Hub's configuration** and its fields are locked.

**In the admin panel:** **Settings → Admin → Sign-in with Google and Microsoft → Microsoft
(Outlook, Microsoft 365)**: paste the **Application (client) ID**, enter the tenant value from
step 1 (empty means `common`) and click **Save**.

**In the configuration:**

**Docker Compose:**
Add the variables to `~/postern/compose.override.yaml` (create the file if it does not exist):

```yaml
services:
  hub:
    environment:
      POSTERN_MICROSOFT_CLIENT_ID: "00000000-0000-0000-0000-000000000000"
      POSTERN_MICROSOFT_TENANT: "common"
```

Apply it:

```sh
cd ~/postern && docker compose up -d
```
**Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
Add the variables to `/etc/postern/hub.env` (they are already there, commented out):

```sh
sudo nano /etc/postern/hub.env
```

```ini
POSTERN_MICROSOFT_CLIENT_ID=00000000-0000-0000-0000-000000000000
POSTERN_MICROSOFT_TENANT=common
```

Apply it:

```sh
sudo systemctl restart postern-hub
```
**Desktop app (built-in Hub):**
The desktop app manages its Hub's configuration, so use the admin panel. The built-in Hub runs
at `http://localhost:<port>`. For the browser sign-in, add
`http://localhost:<port>/api/v1/oauth/callback/microsoft` with that port as the redirect URI;
the code sign-in works without it.

`POSTERN_MICROSOFT_TENANT` takes `common`, `organizations`, `consumers`, a tenant ID or a
domain. The admin panel rejects values with `/`, `?`, `#`, `:` or spaces ("Invalid tenant").

> **Check:** the admin panel shows **Microsoft (Outlook, Microsoft 365)** as **Set up** or
> **Set in the Hub's configuration**.

> **If it did not work:** **Microsoft sign-in wasn't saved** followed by a reason; fix the value
> it names.

## 5. Add the mailbox

1. **Settings → Mailboxes → Add mailbox**, pick **Microsoft**, type your address and click
   **Sign in with Microsoft**.
2. **Browser sign-in:** Microsoft's page opens. Pick or enter your account, sign in and accept
   the permissions. You come back to Postern: **Mailbox connected. Your mail is downloading
   now.** On the desktop app the page opens in your normal browser, which must be signed in to
   your Hub's web app with the same Postern account.
3. **Code sign-in:** Postern shows **Open Microsoft's sign-in page and enter this code:** and a
   code such as `F4K9QX2M`. Click **Copy code**, then **Open microsoft.com/devicelogin** (any
   device works), paste the code, sign in and accept. Postern shows **Waiting for you to finish
   on Microsoft's site…** and then **you@outlook.com is connected**. On a phone, tap **Copy code
   and open sign-in**.

The code is valid for about 15 minutes.

> **Check:** **Settings → Mailboxes** shows the address with **Outlook · Downloading mail, …%
> done**, then **Up to date**.

> **If it did not work:** see [Troubleshooting](https://docs.posternmail.app/providers/microsoft/#troubleshooting).

## Server settings

There is nothing to enter: the Hub uses Microsoft Graph at `https://graph.microsoft.com/v1.0`
and, for push, IMAP at `outlook.office365.com`, port 993, TLS. For reference, the IMAP and SMTP
servers Microsoft publishes are:

| | Server | Port | Security |
|---|---|---|---|
| IMAP | `outlook.office365.com` | 993 | TLS |
| SMTP (Outlook.com) | `smtp-mail.outlook.com` | 587 | STARTTLS |
| SMTP (Microsoft 365) | `smtp.office365.com` | 587 | STARTTLS |

Postern does not use them with a password.

## What the Hub does with Microsoft

- **Recognizing the account.** Outlook.com, Hotmail, Live, Windows Live, MSN and Passport
  addresses are known. Your own domain is recognized as Microsoft 365 when its MX records point
  to `*.protection.outlook.com`; the dialog then says "This domain uses Microsoft 365. Sign in
  with your work or school account (OAuth). Some organizations require an administrator to
  approve the app first."
- **Folders.** All mail folders and subfolders are synced. Inbox, Sent Items, Drafts, Archive,
  Deleted Items and Junk Email are recognized. Outbox, Conversation History and Sync Issues are
  left out. Every message is in exactly one folder.
- **Tags** are saved as Outlook categories.
- **Threads** come from Outlook's conversation IDs.
- **Answered state** is not available through Graph, so Postern cannot show or set it for
  Microsoft mailboxes.
- **New mail.** Graph's own push needs a Hub reachable from the internet, so the Hub keeps one
  IMAP connection to `outlook.office365.com` open and waits for news in the inbox. When it rings,
  or every 45 seconds, it asks Graph for changes. If IMAP is turned off for the mailbox, the
  45-second checks continue on their own.
- **Sending** goes through Graph. Exchange saves the copy in Sent Items. Messages larger than
  4 MB are sent in parts (draft, then attachments through upload sessions), up to 150 MB.
- **Devices.** The desktop and mobile apps sync Microsoft mailboxes directly; the Hub hands them
  the refresh token and the client ID.

Mailboxes connected before the Hub asked for `IMAP.AccessAsUser.All` have no push connection.
Use **Update sign-in** once to give the consent; until then, the 45-second checks apply.

## Limits

- Microsoft throttles heavy use of Graph. The Hub waits as Microsoft asks and continues. You
  only see **Outlook is limiting how fast this mailbox can sync** when it lasts 15 minutes or
  more.
- Sending is limited to 150 MB per message, plus your organization's own message size limits.
- A full Microsoft mailbox shows **This mailbox is full at the provider. Free up space there and
  syncing continues.**

## Troubleshooting

### Messages in Postern

| Message | Cause | Fix |
|---|---|---|
| **This way of signing in isn't set up on your Hub.** | No Microsoft client ID on the Hub ("Microsoft sign-in isn't set up on this Hub. An administrator can add a Microsoft client ID in Settings → Providers."), or the browser sign-in was started on a Hub without `https://`. | Step 4. The setting is in **Settings → Admin**. |
| The dialog asks for a password for an Outlook address | The Hub has no Microsoft client ID, so it falls back to IMAP with a password, which Microsoft refuses. | Set up the client ID (steps 1 to 4) and start again. |
| **Microsoft didn't start the sign-in. Check the client ID and tenant.** | Microsoft rejected the code request. | Check the client ID, the tenant value and **Allow public client flows**. |
| **The code expired. Start again to get a new one.** | You did not finish within the code's lifetime (about 15 minutes). | Start again. |
| **Sign-in was declined.** | You or an admin declined the permissions. | Start again and accept, or ask for admin consent. |
| **The Hub restarted during sign-in. Start again.** | The Hub restarted while it waited for the code. | Start again. |
| **Couldn't read the mailbox address of this Microsoft account.** | The account has no Exchange mailbox, or `User.Read` was not granted. | Use an account with a mailbox; check the permissions. |
| **Couldn't connect the mailbox. Microsoft sign-in was cancelled.** | You cancelled on Microsoft's page. | Start again. |
| **Couldn't connect the mailbox. Microsoft sign-in failed: …** | Microsoft returned an error; the `AADSTS` code is in the text. | See the table below. |
| **Couldn't connect the mailbox. Microsoft rejected the sign-in. Check the app registration and try again.** | The code exchange failed, usually because the redirect is registered as **Web** instead of **Mobile and desktop applications**. | Fix step 2. |
| **Couldn't connect the mailbox. Sign in to the Hub with the account that started adding the mailbox, then try again.** | The browser that came back from Microsoft is not signed in to the Hub as you. | Sign in to the Hub's web app in that browser, then start again. |
| **The sign-in no longer works. Reconnect to keep getting mail.** | Password change, revoked consent, or a policy that ended the session. | **Reconnect** and sign in again. |

### Microsoft error codes

| Code | Meaning | Fix |
|---|---|---|
| `AADSTS7000218` | The request needs a client secret: **Allow public client flows** is off. | Step 2, item 1. |
| `AADSTS50011` | The redirect URI does not match the registration. | Add the exact URI under **Mobile and desktop applications** (step 2). |
| `AADSTS700016` | The app is not found in the directory: wrong client ID, or the tenant does not fit the account types. | Check the client ID and the tenant value (step 1 table). |
| `AADSTS50020` | The account is not part of the tenant the app allows. | Use `common` (and matching account types), or the right tenant. |
| `AADSTS65001` | Consent is missing and users may not consent themselves. | An admin grants consent (step 3). |
| `AADSTS530035` or "blocked by security defaults" | The organization blocks the code sign-in. | Use the browser sign-in (needs `https://`), or ask an admin to allow it. |
| `AADSTS90002` | The tenant does not exist. | Fix `POSTERN_MICROSOFT_TENANT` or the tenant in the admin panel. |

Microsoft's lookup for any code: <https://login.microsoftonline.com/error>.

### Other symptoms

| Symptom | Cause | Fix |
|---|---|---|
| "Need admin approval" on Microsoft's page | Your organization does not let users consent to apps. | An admin grants consent (step 3). |
| New mail shows up only after up to a minute | No push connection: IMAP is off for the mailbox, or the mailbox was connected before the IMAP permission was asked for. | Turn IMAP on in the Exchange admin center (mailbox → **Manage email apps**), then **Update sign-in**. |
| Sending from an alias fails | The mailbox has no send-as right for that address. | Give the right in Exchange, or send from the main address. |

To disconnect, remove the mailbox in Postern. To revoke the Hub's access at Microsoft: personal
accounts at <https://account.live.com/consent/Manage>, work accounts at
<https://myapps.microsoft.com>.
