The short answer: yes, you can read an Outlook mailbox from Python, and the easiest reliable route is Microsoft Graph with the official Microsoft Authentication Library. Register an app in Microsoft Entra ID, grant it permission to read mail, sign in once, then call the Graph endpoint for messages. That works for Microsoft 365, Outlook.com and most hosted Exchange mailboxes. For on-site Exchange servers or mailboxes you only reach over a LAN, IMAP or Exchange Web Services may be the only options — and Microsoft has been retiring Basic auth on those, so check with the administrator before you build on them.

A key distinction up front: "reading" a mailbox is not the same as "reading it silently in the background forever." Graph permissions come in two flavours — delegated (acts as the signed-in user) and application (acts as the app, with no user present). Application permissions need an admin to grant consent and should only be used where you genuinely need unattended access. Start delegated unless you have a reason not to.

What You'll Need

  • A Microsoft account with access to the mailbox — Microsoft 365 work/school, or a personal Outlook.com account.
  • Python installed (3.9 or newer is a safe target), plus the requests library, and the msal package from Microsoft.
  • An app registration in the Microsoft Entra admin center (formerly Azure AD). A personal account can register an app too, through the Microsoft developer portal.
  • Permission to read mail: for delegated access, the Mail.Read scope; for unattended access, the Mail.Read application permission with admin consent.
  • For IMAP: the mailbox address, a password or app password, and confirmation that IMAP is enabled on the account.

Step-by-Step

  1. Decide which API fits. Use Microsoft Graph for anything cloud-hosted — it is the current, supported interface. Use IMAP only if Graph is unavailable and your admin confirms it is enabled. Exchange Web Services is legacy; treat it as a last resort and ask your admin rather than assuming.
  2. Register an application. Sign in to the Microsoft Entra admin center, open the app registrations area and create a new registration. Give it a name, choose the supported account types that match your users (single tenant for one organisation, or personal Microsoft accounts if you are using Outlook.com), and register it. Note the Application (client) ID and the Directory (tenant) ID.
  3. Add a redirect URI. For a local script, choose the "mobile and desktop applications" platform and add http://localhost as a redirect URI. This is what lets interactive sign-in hand the token back to your script. Menus and exact labels in the portal change between versions, so if a label differs slightly, look for the closest match.
  4. Add API permissions. In the app's API permissions page, add a Microsoft Graph permission, choose delegated permissions and select Mail.Read. If your organisation requires admin consent, a tenant admin has to approve it — a personal Outlook.com account does not need that step.
  5. Install the libraries. In a terminal, run pip install msal requests. MSAL handles the sign-in flow and token caching; requests performs the HTTP calls.
  6. Sign in and get a token. Create an MSAL public client application with your client ID and authority (the authority includes your tenant, or uses the "common" endpoint for multi-tenant and personal accounts). Call the interactive flow with the Mail.Read scope. The first run opens a browser window; sign in with the mailbox account and consent. MSAL returns an access token and caches it, so later runs can acquire a token silently.
  7. Make the Graph call. Send a GET request to https://graph.microsoft.com/v1.0/me/messages with an Authorization: Bearer <token> header. The response is JSON with a value array of messages. Each message includes subject, sender, received date and a body preview.
  8. Read message fields safely. Use .get() on each dictionary rather than direct key access, because fields such as bodyPreview or from may be missing on some items. For a full body, request the message by ID, or add ?$select=subject,from,receivedDateTime,body to control which fields come back and keep the response small.
  9. Page through large mailboxes. Graph returns a limited page of results. If the JSON contains an @odata.nextLink, request that URL to get the next page, and repeat until it disappears. Do not assume one call returns everything.
  10. Filter server-side. Add query parameters such as $filter=isRead eq false or $search="invoice" and $top=50 so you pull only what you need. Filtering on the server is far faster than downloading thousands of messages and filtering in Python. Note that $search and $filter cannot always be combined in one request.
  11. Run unattended only if you must. For a scheduled job with no user present, your admin grants the Mail.Read application permission with admin consent, and you acquire a token with the client credentials flow — client ID plus a client secret stored in an environment variable, never in code. Then call /users/{mailbox-address}/messages instead of /me/messages.

Common Mistakes

  • Hard-coding the access token. Tokens expire, typically within about an hour. Always ask MSAL for a token at runtime and let it refresh silently.
  • Putting the client secret in source code. Store it in an environment variable or a secrets manager and keep it out of version control.
  • Calling /me/messages in an unattended job. There is no signed-in user in that flow, so /me fails. Use the specific mailbox path under /users/.
  • Forgetting pagination. A script that reads only the first page quietly misses older mail.
  • Using a work password for IMAP when the tenant blocks it. Modern tenants often disable Basic auth; the connection is refused even though the password is correct.
  • Requesting too much data. Pulling full message bodies for an entire inbox makes responses enormous and slow. Select only the fields you need.
  • Marking mail as read accidentally. Some IMAP fetches and client-side flags can change message state. Reading should not alter the mailbox unless you intend it to.

Tips

  • Test with your own mailbox before pointing a script at anyone else's. Shared or delegated mailboxes should only be accessed with the owner's and administrator's consent.
  • Cache your token and reuse the MSAL token cache object rather than signing in on every run.
  • Log the HTTP status code and the error body when a call fails; Graph error messages name the exact problem, such as insufficient privileges or a malformed filter.
  • Use $top and $select from the start so your script stays fast as the mailbox grows.
  • Check whether the mailbox you are targeting is a shared mailbox, an archive or a resource; the path differs and some permissions do not cover archives.

Troubleshooting

Sign-in says the app needs admin approval. The tenant requires consent for the scope. Ask an administrator to grant consent in the portal, or use a personal account for testing.

You get a 401 Unauthorized. The token is missing, expired or issued for the wrong resource. Reacquire it through MSAL and confirm the audience is Microsoft Graph.

You get a 403 Forbidden. Authentication worked but permission did not. Check that Mail.Read is granted and consented, and that you are reading a mailbox your account is allowed to see.

An empty value array. The mailbox may genuinely be empty, or your filter is too narrow. Remove filters and test with no query string.

IMAP login fails with the correct password. IMAP is likely disabled or basic authentication is blocked. Ask the administrator, and prefer Graph rather than searching for workarounds.

If none of that helped, check the service health status in the Microsoft 365 admin center, sign out and re-consent by clearing the MSAL token cache file, and reinstall the packages into a clean virtual environment. If the mail is in a corporate tenant, the fastest resolution is almost always a message to your IT administrator.

Frequently Asked Questions

Can I read an Outlook.com mailbox, not just Microsoft 365?

Yes. Register the app with support for personal Microsoft accounts and use the common authority so sign-in works for both work and personal accounts.

Do I need admin consent?

For delegated Mail.Read on a personal account, no. In managed organisations, consent policy is set per tenant, so you may need an administrator to approve it — especially for unattended application permissions.

Is IMAP still an option?

Sometimes. It depends on the tenant: many organisations block it or disable Basic auth. Microsoft Graph is the supported path for cloud mailboxes.

Will reading messages mark them as read?

A plain Graph GET does not change read state. Only send a PATCH request if you deliberately want to mark a message as read or unread.

How do I read attachments?

Request the attachment collection for a specific message ID rather than pulling every message's full payload. Check file sizes before downloading.

Can I access someone else's mailbox?

Only with explicit permission — delegated access granted by the owner, or application permission granted by an administrator. Reading another person's mail without consent is prohibited.