The server response was: 5.7.57 Client not authenticated to send mail Error in ASP.Net, C#

Raja Rashid Hussain 1 Reputation point
2022-06-22T05:50:30.463+00:00

Hi,

I am getting error while sending email from one of my ASP.Net, C# application.

string smtpServer = "smtp.office365.com";  
int port = 587;  
using (MailMessage mail = new MailMessage())  
{  
	using (SmtpClient SmtpServer = new SmtpClient(smtpServer))  
	{  
		mail.From = new MailAddress(_emailIDFrom);  
		mail.To.Add(_emailIDTo);  
		mail.Subject = _subject;  
		mail.IsBodyHtml = true;  
		mail.Body = _emailBody;  
		SmtpServer.Port = port;  
		SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;  
		SmtpServer.UseDefaultCredentials = false;  
		SmtpServer.Credentials = new System.Net.NetworkCredential(_emailIDFrom, _emailIDFromPassword);  
		SmtpServer.EnableSsl = true;  
		try  
		{  
			SmtpServer.Send(mail);  
			return true;  
		}  
		catch (Exception ex)  
		{  
			lblMessage.Text = ex.Message;  
			return false;  
		}  
	}  
}  

ERROR
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,
the request did not meet the criteria to be authenticated successfully.
Contact your administrator. [DX1P273CA0032.AREP273.PROD.OUTLOOK.COM]

My Global.asax

if (ServicePointManager.SecurityProtocol.HasFlag(SecurityProtocolType.Tls12) == false)  
{  
	//ServicePointManager.SecurityProtocol = ServicePointManager.SecurityProtocol | SecurityProtocolType.Tls12;  
	ServicePointManager.SecurityProtocol =   
		SecurityProtocolType.Tls |  
		SecurityProtocolType.Tls11 |  
		SecurityProtocolType.Tls12;  
	//ServicePointManager.SecurityProtocol = (SecurityProtocolType)48 | (SecurityProtocolType)192 | (SecurityProtocolType)768 | (SecurityProtocolType)3072;  
}  

How to resolve this error. Please help. Thanks

Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
516 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Glen Scales 4,431 Reputation points
    2022-06-24T00:28:22.597+00:00

    You can't use Basic Authentication to send SMTP email with Office365 anymore see https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/deprecation-of-basic-authentication-exchange-online

    That means the .Net SmtpClient can't be used anymore so you need to look at somthing like MailKit https://github.com/jstedfast/MailKit that supports oAuth see https://github.com/jstedfast/MailKit/blob/master/ExchangeOAuth2.md and https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth

    The other option is to stop using SMTP and switch to using the Graph API https://learn.microsoft.com/en-us/graph/api/user-sendmail?view=graph-rest-1.0&tabs=http which is a more secure solution as you can take advantage of the client credentials flow etc in your code and also limit the scope of the target mailboxes you app could send as.

    2 people found this answer helpful.
    0 comments No comments