I am trying to send an email from my system when creating a document, but I get the following error: 'The SMTP server requires a secure connection or the client was not authenticated. The response from the server was: 5.7.57 ' I have tried different methods that I have found, but they all give me the same error.
The code I am using is the following
public static bool SendMail()
{
var resultado = false;
string error = "";
string claveCorreo = System.Configuration.ConfigurationManager.AppSettings["AdminPassword"];
try
{
MailMessage mail = new MailMessage();
mail.To.Add("[email protected]");
mail.From = new MailAddress("[email protected]", "Portal Empleados");
mail.Subject = "Correo de Prueba";
mail.Body = "Este es una prueba";
mail.IsBodyHtml = true;
var smtp = new SmtpClient()
{
Credentials = new System.Net.NetworkCredential("[email protected]", claveCorreo),
Host = "smtp.office365.com",
Port = 587,
EnableSsl = true,
};
smtp.Send(mail);
resultado = true;
}
catch (Exception e)
{
error = e.Message.ToString();
resultado = false;
}
return resultado;
}
various points
TOPIC 1:
First recommendation , you have to use MailKit.Net.Smtp.SmtpClient instead of System.Net.Mail.SmtpClient You can check the official documentation where we can read
TOPIC 2: Send mail with MimeKit and setting StartTls Here you have to use the MailKit nuget component to use the SmtpClient class and send, but first you must connect... and send in said "connection" the "options" parameter configured with MailKit .Security.SecureSocketOptions.StartTls Example
Code
TOPIC 3: Error when sending via Outlook.com, it requires an exclusive app password If you connect with the code above and get the following error
It's because you have to enable an "app password". Use of application passwords with applications that do not support two-step verification That is, you carry out the configuration and outlook.com will provide you with a password to be used to connect (which is not the password of your mailbox but exclusive for the application that you are developing) this is useful when you want to connect with shipping client app
Links that can help you
I hope it helps or guides you.