Basic authentication for IMAP in Exchange Online has been disabled. See Deprecation of Basic authentication in Exchange Online for details. Azure Active Directory is now called Microsoft Entra ID. The app registration steps below are based on the portal interface at the time of writing.
As Exchange Online moves away from basic authentication, OAuth is now supported for IMAP. Developer information is available on the following page:
Here is a basic C# sample. I won’t explain the code itself, so please read it alongside the developer documentation above. It implements only the minimum functionality, so a full implementation requires knowledge of the Microsoft identity platform and IMAP. This is a C# console application that uses MSAL for authentication. It uses the device authorization grant flow because there is little benefit in creating a new IMAP client that uses the authorization code flow. I’ll also show how to register the application in Azure AD for this flow.
There is little benefit in connecting to Exchange Online using IMAP, so you should consider migrating to Microsoft Graph instead.
using Microsoft.Identity.Client; using System; using System.IO; using System.Linq; using System.Net.Security; using System.Net.Sockets; using System.Text; using System.Threading.Tasks;
namespaceImapClientDemo { classProgram { privateconststring ClientId = "<CLIENT ID OF YOUR APP>"; privateconststring Authority = "https://login.microsoftonline.com/organizations"; privatestaticreadonlystring[] Scopes = newstring[] { "https://outlook.office.com/IMAP.AccessAsUser.All" };
staticvoidMain(string[] args) { var tokenResult = GetToken().Result; string token = tokenResult.AccessToken; string user = tokenResult.Account.Username; // For a shared mailbox, replace user with the email address of the shared mailbox. string XOAUTH2 = Base64Encode($"user={user}\u0001auth=Bearer {token}\u0001\u0001");
using (ExoImapClient client = new ExoImapClient()) { // Receive the greeting. var response = client.Receive();
if (!response.StartsWith("* OK")) { thrownew Exception("Unexpected response received."); }
Go to the Azure portal and sign in to the Azure Active Directory admin center.
Go to the Azure Active Directory blade.
Select Manage > App registrations > New registration.
Configure the following settings and select Register:
Name: Enter any application name (for example, ImapTest01).
Supported account types: Accounts in any organizational directory (any Azure AD directory - multitenant).
Redirect URI: Select Public client/native (mobile & desktop) from the drop-down list and enter https://login.microsoftonline.com/common/oauth2/nativeclient.
Select Manage > Authentication.
Set Enable the following mobile and desktop flows to Yes.