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. For the planned retirement of basic authentication for SMTP AUTH, see Deprecation of Basic authentication in Exchange Online.
As Exchange Online moves away from basic authentication, OAuth is now supported for SMTP. 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 SMTP. 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 SMTP client that uses the authorization code flow. I’ll also show how to register the application in Azure AD for this flow.
If you don’t have a clear requirement to connect to Exchange Online using SMTP, you should consider migrating to Microsoft Graph instead.
using Microsoft.Identity.Client; using System; using System.IO; using System.Linq; using System.Net; using System.Net.Security; using System.Net.Sockets; using System.Text; using System.Threading.Tasks;
namespaceSmtpOAuthDemo { classProgram { privateconststring ClientId = "<CLIENT ID OF YOUR APP>"; privateconststring Authority = "https://login.microsoftonline.com/organizations"; privatestaticreadonlystring[] Scopes = newstring[] { "https://outlook.office.com/SMTP.Send" };
staticvoidMain(string[] args) { var tokenResult = GetToken().Result; string token = tokenResult.AccessToken; string user = tokenResult.Account.Username; string XOAUTH2 = Base64Encode($"user={user}\u0001auth=Bearer {token}\u0001\u0001"); string fromAddress = user; // In case of sending as someone else, replace the value of fromAddress variable with the user's email address.
using (ExoSmtpClient client = new ExoSmtpClient()) { // Receive the greating. var response = client.Receive();
if (!response.StartsWith("220")) { thrownew Exception("Unexpected response received."); }
Select Manage > App registrations > New registration.
Configure the following settings and select Register:
Name: Enter any application name (for example, SmtpTest01).
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.