Office 365 and IMAP or POP3 with OAuth 2.0 authentication in unattended (app-only) mode: How to make it work

General News

Summary

This means that classic username/password authentication will no longer work with Exchange Online, and application will have to be upgraded to use OAuth 2.0. The guide below describes what needs to be done to enable POP3 or IMAP access for unattended apps (app-only mode). // application (client) ID obtained from Azure const string ClientId = "4f5a9f88-1111-1111-1111-bdca9a88c089"; // change to your AppId // applications client secret value (application password) const string ClientSecretValue = "ThisIsSomeVerySecretValue"; // change to your secret value // your organizations directory (tenant) ID const string TenantId = "fb561382-2222-2222-2222-06ab992d36b7"; // change to your TenantId // mailbox to access (not an alias) const string SmtpAddress = "someone@example.org"; // change this // default scope of permissions to request static readonly string[] Scopes = new[] { "https://outlook.office365.com/.default", // for accessing Exchange Online with app-only auth }; • None Before your application connects to Office 365s IMAP or POP3 service, it has to request an OAuth 2.0 access token using the Microsofts authentication API. using Microsoft.Identity.Client; ... // get an instance of confidential client application API var cca = ConfidentialClientApplicationBuilder .Create(ClientId) .WithClientSecret(ClientSecretValue) .WithTenantId(TenantId) .Build(); // acquire OAuth 2.0 access token from Microsoft 365 AuthenticationResult result = await cca.AcquireTokenForClient(Scopes).ExecuteAsync(); string accessToken = result.AccessToken; • None Then, connect to using Rebex IMAP or Rebex POP3 package and use the acquired token to start accessing your organizations mailboxes. using Rebex.Net; ... // connect to Office 365 and authenticate using access token var client = new Imap(); // or new Pop3() client.Connect("outlook.office365.com", SslMode.Implicit); client.Login(SmtpAddress, accessToken, ImapAuthentication.OAuth20) // start working with the mailbox ... To give this a try before adding the code to your application, try our ImapOAuthAppOnlyConsole or Pop3OAuthAppOnlyConsole samples.

Classifications

industries
No industries detected
applications
Accounting and Taxes

AskAI Classifications

Labels
Software Development SaaS Developer Tools

Linked Companies

Rebex ČR s.r.o.
$1M to $5M
GitHub, Inc.
$1M to $5M
Microsoft
$1B+
n/a