How to send Mail using azure Entra Id User tenant mail in .NET core programmatically ?

Kuldeep Y 41 Reputation points
2024-07-23T14:41:16.6833333+00:00

Hi,
I have an azure Entra ID Subscription where I have created user and the email with my domain, For example (ABC@abcemi.com) and I tried to send mail programmatically.

Here are the steps that I have followed to Send Mail via .NET core code: -

First, I have set MFA authentication to Enforced, so I can create app password then I have created an app password.
second, I have grant admin consent to SMTP, Mail permission via azure portal to my account.
Third, I have granted the SMTP Authenticated app to use mailbox through Microsoft365 admin center.
Fourth, below is the code that I am using: -

User's image

in the App settings I am using the this

"EmailDetails": {

"smtpServer": "smtp.office365.com",

"EnableSsl": "true",

"smtpPort": "587",

"smtpUser": "My Azure Entra Tenant Mail",

"smtpPass": "App password generated earlier",

"DefaultCred": "false"

}

but whenever I try to send mail it gives an error that is :

"System.Net.Mail.SmtpException: 'The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client not authenticated to send mail. Error: 535 5.7.139 Authentication unsuccessful, user is locked by your organization's security defaults policy. Contact your administrator. [SJ0PR03CA0089.namprd03.prod.outlook.com"

Any help would be appreciated, Thanks.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,647 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,400 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,645 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,731 Reputation points
    2024-07-23T18:43:14.2066667+00:00

    SmtpClient does not support oauth tokens only basic authentication:.

    https://github.com/dotnet/runtime/issues/76253

    but basic authentication has been retired for office 365.

    https://www.microsoft.com/en-us/microsoft-365/blog/2022/09/01/microsoft-retires-basic-authentication-in-exchange-online/

    if your account is grandfathered you may be able to enable basic. otherwise you will need to pick a email library that supports oauth. you will also need to add support in your app to get the bearer token.

    https://learn.microsoft.com/en-us/graph/auth-v2-service?tabs=http