Send Emails using Microsoft 365 in ASP.NET Core
- Setup
- Authenticated SMTP enable for user
- Setup Modern authentication
- Turn off MFA
- Turn off security defaults on Azure Active Directory
- Microsoft 365 SMTP settings
- Implement SMTP AUTH submission using C#
- Issue resolutions
- Client was not authenticated to send anonymous mail during MAIL FROM
- References
Microsoft 365 and Office 365 supports using licensed user account to send emails in client applications. SMTP protocol can be used to send emails in ASP.NET Core applications.
This article shows how to use SMTP AUTH to send emails in ASP.NET Core 3.1 applications.
Setup
There are a few configurations we need to do to be able to send emails using SMTP.
Authenticated SMTP enable for user
1) Logon to https://admin.microsoft.com and then go to Users -> Active users -> select the user that will be used for sending emails.
2) Click Mail tab.
3) Click Manage email apps link.
4) Ensure Authenticated SMTP is enabled.
5) Click Save changes button to save changes.
Setup Modern authentication
1) In admin center, click Settings -> Org settings.
2) Click 'Modern authentication'
3) Ensure 'Authenticated SMTP' is enabled:
Turn off MFA
MFA needs to be turned off for the email account.
1) In admin center, click 'Users' -> 'Active users'> -> select the user account that will be used to send emails.
2) Click 'Manage Multifactor authentication'.
3) Disable MFA for that account.
Turn off security defaults on Azure Active Directory
1) Go to Azure portal or click 'Azure Active Directory' link in admin center.
2) Navigate to your tenant Azure Active Directory properties window.
3) Click link 'Manage security defaults' link.
4) Turn off security defaults.
Microsoft 365 SMTP settings
The following are the details for SMTP AUTH:
- Host: smtp.office365.com
- Port: Port 587 (recommended) or port 25
- TLS/StartTLS: Enabled
- Username and password: the sign in credentials of the hosted mailbox being used (the user account setup in the above steps).
Implement SMTP AUTH submission using C#
The following is the sample code to send emails in .NET Core 3.1 applications using SmtpClient class.
using System.Net.Mail; ...... var message = new MailMessage { From = ... }; using (var client = new SmtpClient()) { client.EnableSsl = true; client.Host = 'smtp.office365.com'; client.Port = 587; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Credentials = new NetworkCredential('example@example-domain.com', 'password'); await client.SendMailAsync(message); }
Issue resolutions
Client was not authenticated to send anonymous mail during MAIL FROM
One of the commonly encountered error is like the following:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [******.PROD.OUTLOOK.COM] at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.EndSend(IAsyncResult result) at System.Net.Mail.SendMailAsyncResult.SendMailFromCompleted(IAsyncResult result) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source) at System.Net.Mail.SendMailAsyncResult.End(IAsyncResult result) at System.Net.Mail.SmtpClient.SendMailCallback(IAsyncResult result) --- End of stack trace from previous location where exception was thrown --- at
This issue is usually caused by incorrect setups. Please ensure the above mentioned configurations are setup correctly (in section Setup). And also ensure your user name and password are correct.
Sometimes you may also need to try the following steps:
- Turn off a setting
- Turn on that setting
References
How to set up a multifunction device or application to send email using Microsoft 365 or Office 365