Unable to send the email from office365 email using SMTP. getting this error each time. I tried many ways, to allow SMTP from the admin center too. Also changed the password too.
Error: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 Client was not authenticated to send mail. Error: 535 5.7.3 Authentication unsuccessful [PN3PR01CA0040.INDPRD01.PROD.OUTLOOK.COM 2023-05-18T11:19:25.007Z 08DB572806B55A65]
var smtpClient = new SmtpClient("smtp.office365.com", 587);
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("[email protected]", "xxxxxx");
smtpClient.EnableSsl = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.TargetName = "STARTTLS/smtp.office365.com";
// Create the email message
var messageMain = new MailMessage();
messageMain.From = new MailAddress("[email protected]");
messageMain.To.Add("[email protected]");
messageMain.Subject = "Hello from ASP.NET MVC";
messageMain.Body = "This is the body of the email.";
smtpClient.Send(messageMain);