After starting project in Release mode with Visual Studio my SmtpClient works and it sends an email, but after publishing to the server i see an error.
This is my C# code:
using (MailMessage mail = new MailMessage())
using (SmtpClient smtpClient = new SmtpClient())
{
smtpClient.Host = "smtp-mail.outlook.com";
smtpClient.Port = 587;
smtpClient.Credentials = new NetworkCredential("mail", "password");
smtpClient.EnableSsl = true;
mail.Body = $"Imie: {Name}, Mail: {Email}, Subcject: {Subject}, Comment: {Comment}";
mail.IsBodyHtml = false;
mail.From = new MailAddress("mail", "Nowe zgłoszenie ze strony");
mail.To.Add("receiver");
mail.Subject = $"Nowe zgłoszenie ze strony";
smtpClient.Send(mail);
}
This is my server: OS: Ubuntu Hosting: Amazon Firewall: None
This is an error:
fail: Microsoft.AspNetCore.Server.Kestrel[13] Connection id "0HMKJSO3E4066", Request id "0HMKJSO3E4066:00000002": An unhandled exception was thrown by the application. 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.3 Authentication unsuccessful [BL1PR13CA0303.namprd13.prod.outlook.com]
As i said credentials are correct.
####UPDATE
I changed smtp service provider from Outlook to another and it works. But now the thing is why outlook not working?
CodePudding user response:
Compared to this article, the following two lines are missing:
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.UseDefaultCredentials = false;
The cited article has a note:
Unless you have enabled two-stage authorization on your Outlook.Com account. If you have, you will need to create an application specific password, or the code above will throw an exception when your credentials are refused by the Outlook.Com SMTP server.