Send Emails using Microsoft 365 in ASP.NET Core

access_time 3 months ago visibility701 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 3 months 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

Want to publish your article on Kontext?

Learn more

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

local_offer .NET local_offer C#

visibility 60
thumb_up 0
access_time 3 months ago

In many solutions, .NET standard has been used to share code between .NET Framework and .NET Core projects. Since the release of .NET 5, you probably will think what is the .NET standard version for C# 9 language features. Well, the answer is simple - there is no need to have another version of ...

local_offer C# local_offer .NET

visibility 261
thumb_up 0
access_time 2 years ago

JSON is commonly used in modern applications for data storage and transfers. Pretty much all programming languages provide APIs to parse JSON. 

Entity Framework Core Code-First - Generate Covering Index with Columns Included

local_offer entity-framework local_offer asp.net core local_offer Azure local_offer C#

visibility 257
thumb_up 1
access_time 6 months ago

In SQL Server or some other relational databases, it is a very common requirement to create covering index with columns included in index pages beside the index key columns. With Entity Framework Core, you can also easily generate covering indexes using purely C# code. For Kontext website, backend ...

About column

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

rss_feed Subscribe RSS