Connect to Exchange Online SMTP with the client credentials flow
Updated:
Warning
This sample is preserved as a historical record. It accepts all certificates and prints authentication data containing an access token to the console. Do not run it as written. Validate certificates, avoid logging authentication data, and manage secrets securely in your implementation.
Note
Azure Active Directory is now called Microsoft Entra ID. The app registration steps are based on the portal interface at the time of writing. .NET 7, used to test the sample, is out of support.
The client credentials flow is now supported for SMTP. Developer information is available on the following page:
This article introduces app registration, Exchange Online configuration, and a basic C# implementation. The code itself is not explained, so please read it alongside the developer documentation above. It implements only the minimum functionality; a full implementation requires knowledge of the Microsoft identity platform and SMTP. The sample is a C# console application that uses MSAL for authentication.
If you do not have a clear requirement to connect to Exchange Online using SMTP, consider migrating to Microsoft Graph instead.
Select Manage > App registrations > New registration.
Configure the following settings and select Register:
Name: Enter any application name (for example, SmtpTest02).
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.
Under Overview > Essentials, record the Application (client) ID.
Select Manage > API permissions.
Select Add a permission.
Under APIs my organization uses, select Office 365 Exchange Online.
Select Application permissions.
Select SMTP > SMTP.SendAsApp, and then select Add permissions.
Under Configured permissions, select the ellipsis next to the default Microsoft Graph > User.Read permission, and then select Remove permission.
Select Yes, remove.
Select Grant admin consent for <tenant name>.
Select Yes.
Select Manage > Certificates & secrets.
Under Client secrets, select New client secret.
Enter a description and select Add.
Record the value of the new client secret.
Return to the top of the Azure Active Directory blade.
Select Manage > Enterprise applications.
Find the app you registered in the list and record its Object ID.
Configure Exchange Online
Connect to Exchange Online from PowerShell with Exchange Online administrator permissions.
Run the following command:
1
New-ServicePrincipal-AppId <application (client) ID recorded above> -ObjectId <object ID recorded above> -DisplayName <registered app name> | fl
using Microsoft.Identity.Client; using System.Net.Security; using System.Net.Sockets; using System.Net; using System.Security.Cryptography.X509Certificates; using System.Text;
namespaceSmtpOAuthCcfDemo { internalclassProgram { privateconststring ClientId = "<APPLICATION (CLIENT) ID RECORDED ABOVE>"; privateconststring Secret = "<CLIENT SECRET RECORDED ABOVE>"; privateconststring TenantId = "<TENANT NAME (e.g. contoso.onmicrosoft.com)>"; privatestaticreadonlystring[] Scopes = newstring[] { "https://outlook.office365.com/.default" };
privateconststring SenderSmtpAddress = "<SENDER SMTP ADDRESS (MAILBOX THE APP HAS FULL ACCESS TO)>"; privateconststring RecipientSmtpAddress = "<RECIPIENT SMTP ADDRESS>";