Fix Microsoft 365 IMAP LOGIN Failed in Python
Question details
The user needs to authenticate a Python IMAP connection to a Microsoft 365 mailbox.

- Product
- Microsoft 365 Exchange Online and Python imaplib
- Device & OS
- not provided
- Scenario
- A Python script uses imaplib to connect to a Microsoft 365 mailbox.
- Observed behavior
- IMAP login fails even though the password is correct and IMAP is enabled.
Confirm the mailbox address and ask your Microsoft 365 administrator whether IMAP and OAuth access are permitted for the mailbox and tenant. Use outlook.office365.com on port 993 for IMAP over SSL, not the SMTP server or port.
Authenticate to Microsoft 365 IMAP with OAuth 2.0
Use this method when password-based IMAP authentication is blocked and your tenant permits OAuth access.
This example uses delegated access and device-code sign-in for the signed-in user's mailbox. An administrator may need to approve the app and its permissions.
In the Microsoft 365 admin center, open Users > Active users, select the user, and check Mail > Manage email apps to confirm IMAP is enabled. Ask your administrator to check tenant policies if access is still blocked.
In the Microsoft Entra admin center, open App registrations > New registration. Under API permissions, add the delegated Office 365 Exchange Online permission IMAP.AccessAsUser.All, and grant consent if required. Under Authentication, enable public client flows for device-code sign-in.
Run pip install msal in the Python environment that will run the script.
Replace the tenant ID, application ID, and mailbox address below, then run the script and follow the device-code prompt: import imaplib import msal app = msal.PublicClientApplication( 'APPLICATION_ID', authority='https://login.microsoftonline.com/TENANT_ID' ) flow = app.initiate_device_flow( scopes=['https://outlook.office.com/IMAP.AccessAsUser.All'] ) if 'user_code' not in flow: raise RuntimeError(flow.get('error_description', 'Device flow failed')) print(flow['message']) result = app.acquire_token_by_device_flow(flow) if 'access_token' not in result: raise RuntimeError(result.get('error_description', 'Token request failed')) mailbox = 'user@example.com' auth = f"user={mailbox}\x01auth=Bearer {result['access_token']}\x01\x01" with imaplib.IMAP4_SSL('outlook.office365.com', 993) as client: client.authenticate('XOAUTH2', lambda _: auth.encode('utf-8')) print(client.select('INBOX'))

Read the mailbox with Microsoft Graph instead of IMAP
Use Microsoft Graph when your application can read messages through an API rather than an IMAP connection.
Work with Office documents in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar interface and support for Microsoft Office formats, making it easy to move document work between suites. It does not change Microsoft 365 IMAP authentication settings.

Frequently Asked Questions
Why does IMAP login fail when my password is correct?
Microsoft 365 may block basic authentication even when IMAP is enabled. Use OAuth 2.0 with the IMAP.AccessAsUser.All delegated permission, and check tenant policies and consent with your administrator.
Which server and port should Python use for Microsoft 365 IMAP?
Use imaplib.IMAP4_SSL with outlook.office365.com on port 993. The smtp.office365.com server on port 587 is for sending mail via SMTP, not reading mail through IMAP.
Where can an administrator check whether IMAP is enabled?
In the Microsoft 365 admin center, open Users > Active users, select the mailbox user, and look under Mail > Manage email apps for the IMAP setting.
Can I read Microsoft 365 mail without using IMAP?
Yes. Register an app with the appropriate Microsoft Graph Mail.Read permission, obtain an access token, and request messages from the /me/messages endpoint for delegated access.




