I'm trying to send email from my ASP.NET core 2.2 web application, using my company Office 365 email account (we have Office 365 Business Basic package).
I found this code on StackOverflow:
MailMessage msg = new MailMessage();
msg.To.Add(new MailAddress("someone@somedomain.com", "SomeOne"));
msg.From = new MailAddress("you@yourdomain.com", "You");
msg.Subject = "This is a Test Mail";
msg.Body = "This is a test message using Exchange OnLine";
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential("your user name", "your password");
client.Port = 587; // You can use Port 25 if 587 is blocked (mine is!)
client.Host = "smtp.office365.com";
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.EnableSsl = true;
try
{
client.Send(msg);
lblText.Text = "Message Sent Succesfully";
}
catch (Exception ex)
{
lblText.Text = ex.ToString();
}
However I got this 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, SmtpClientAuthentication is disabled for the Tenant.
Visit https://aka.ms/smtp_auth_disabled for more information. [HK2PR03CA0048.apcprd03.prod.outlook.com]
And on the link on the error above they stated:
I followed the link and enabled the Authenticated SMTP, like the pic below:
However, then I got this 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. [HK2PR02CA0162.apcprd02.prod.outlook.com]
Then i searched for that error but couldn't find any information.
Now I have no clues to continue, please help me to fix this, thank you so much.