Send Emails using Microsoft 365 in ASP.NET Core

event 2020-09-11 visibility 20,698 comment 0 insights
more_vert
insights Stats

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.

warning This guide is provided for reference only. There are certain risks when turn off some of the security configurations in Microsoft 365 or Azure. Please take these into consideration before making decisions. Please refer to official documentations if you are not sure about some concepts.

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.

20200911144605-image.png

3) Click Manage email apps link.

4) Ensure Authenticated SMTP is enabled.

20200911144703-image.png

5) Click Save changes button to save changes. 

Setup Modern authentication

1) In admin center, click Settings -> Org settings.

2) Click 'Modern authentication'

20200911145036-image.png

3) Ensure 'Authenticated SMTP' is enabled:

20200911145131-image.png

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.

20200911152011-image.png

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.20200911150407-image.png

3) Click link 'Manage security defaults' link.

4) Turn off security defaults.

20200911150528-image.png

warning Please keep in mind the risk of turning off security defaults. For more details about Security defaults, please refer to this page.

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

Fix issues with printers, scanners, and LOB applications that send email using Microsoft 365 or Office 365

More from Kontext
comment Comments
No comments yet.

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts