Others

How to Use XOAUTH2 with IMAP, POP, or SMTP for Mail Servers

Kushani NimanthikaKushani Nimanthika Oct 23, 2026 868 views

Question details

The user needs to configure XOAUTH2 authentication to connect an application to Microsoft or Gmail mail servers via IMAP, POP, or SMTP protocols.

How to Use XOAUTH2 with IMAP, POP, or SMTP for Mail Servers
Product
Microsoft Entra ID
Device & OS
not provided
Scenario
Setting up modern OAuth authentication for an application to securely access email services.
Observed behavior
Basic authentication with Outlook or Gmail server settings fails, as the mail servers now require XOAUTH2 for successful connections.
Before you start

Ensure you have administrative access to your Microsoft Entra ID (formerly Azure Active Directory) portal to register the application and generate the required OAuth credentials.

Solution 1Recommended

Register the Application with Microsoft Entra ID

Configure OAuth authentication by registering your application with Microsoft's identity platform to securely connect via IMAP, POP, or SMTP.

Since Microsoft and Google have deprecated basic authentication, applications must use modern authentication (OAuth 2.0). By registering your application in Microsoft Entra ID, you can obtain the necessary access tokens to authenticate via the XOAUTH2 protocol.

1
Log in to the Azure Portal

Navigate to the Microsoft Azure portal and open the Microsoft Entra ID (formerly Azure Active Directory) service.

2
Create a New App Registration

Click on 'App registrations' in the left menu, select 'New registration', and enter a name for your IMAP/POP/SMTP application.

3
Configure API Permissions

Go to 'API permissions', click 'Add a permission', and select Microsoft Graph or Office 365 Exchange Online. Add the delegated permissions required for your protocol, such as IMAP.AccessAsUser.All, POP.AccessAsUser.All, or SMTP.Send.

4
Generate Client Credentials

Navigate to 'Certificates & secrets' and create a new client secret. Save this value securely, as your application will need it to request OAuth tokens.

5
Implement the XOAUTH2 Protocol

Update your application code to use the Microsoft Authentication Library (MSAL) to retrieve an access token. Pass this token to your mail server using the standard XOAUTH2 authentication format.

Microsoft Documentation: For extensive developer guidelines and code samples, refer to the official Microsoft documentation on authenticating IMAP, POP, and SMTP applications using OAuth.
Free Microsoft Office alternative

Enhance Your Workflow with WPS Office

While managing complex backend configurations like XOAUTH2 email authentication, keep your document workflows simple and efficient. WPS Office provides a lightweight, fully functional suite for all your document, spreadsheet, and presentation needs without the heavy cost.

Fully compatible with Microsoft Office formats (.docx, .xlsx, .pptx) for seamless email attachmentsEasily integrate and share documents directly through modern email clients secured by OAuthLightweight application that uses minimal system resources while offering advanced PDF editingFamiliar user interface ensuring a zero-learning-curve migration from Microsoft Office
microsoft office alternative - wps office

Frequently Asked Questions

What is XOAUTH2 and why do I need it for IMAP or SMTP?

XOAUTH2 is an authentication protocol mechanism that uses OAuth 2.0 access tokens instead of traditional username/password credentials. You need it because major email providers like Microsoft and Google are disabling basic authentication to improve security.

Can I use XOAUTH2 with personal Gmail accounts?

Yes, Google supports XOAUTH2 for Gmail. Instead of Microsoft Entra ID, you must register your application in the Google Cloud Console, create OAuth 2.0 Client IDs, and request the appropriate Gmail API scopes.

Why does my application fail to authenticate even after generating an OAuth token?

Authentication failures often occur if the generated token lacks the correct API scopes (like SMTP.Send), if the token has expired without a refresh token mechanism in place, or if the XOAUTH2 string isn't formatted correctly (base64 encoded containing the user address and bearer token).