Send Emails using Microsoft 365 in ASP.NET Core

access_time 10 days ago visibility16 comment 0

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.

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.

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

info Last modified by Raymond at 10 days ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Kontext Column

Created for everyone to publish data, programming and cloud related articles.
Follow three steps to create your columns.


Learn more arrow_forward

More from Kontext

Machine Learning with .NET in Jupyter Notebooks

local_offer plot local_offer machine-learning local_offer jupyter-notebook local_offer C# local_offer dotnet core

visibility 737
thumb_up 0
access_time 9 months ago

In this article, I'm going to show you how to install Jupyter in Windows and then install .NET kernel for Jupyter notebooks. It also shows a machine learning example using ML.NET. The target audience are .NET developers who want to expand their skills in data engineering and science domain...

local_offer teradata local_offer C# local_offer .NET

visibility 7745
thumb_up 0
access_time 6 years ago

In this post, I will demonstrate how to connect to Teradata database via .NET Data Provider for Teradata using C#. Prerequisites Install the .NET Data Provider for Teradata from the following link: ...

About column

ASP.NET Core

Articles about ASP.NET Core 1.x, 2.x and 3.x.

rss_feed Subscribe RSS