I am using C# to send an email from my local computer, but I'm running into an error while executing the code below (for testing purposes).
C# code:
protected void SendEmail(string emailAddres)
{
SmtpClient smtp = new SmtpClient();
MailMessage mail = new MailMessage()
{
To = { new MailAddress(emailAddres) },
From = new MailAddress("[email protected]", "fromEmail"),
Subject = "Subject of a test email!",
IsBodyHtml = true,
Body = "Test body email.",
BodyEncoding = System.Text.Encoding.UTF8,
SubjectEncoding = System.Text.Encoding.UTF8
};
try
{
smtp.Send(mail);
lblResultEmail.Text = "Message Sent Succesfully";
lblResultEmail.ForeColor = System.Drawing.Color.Green;
}
catch (Exception ex)
{
lblResultEmail.Text = ex.ToString();
lblResultEmail.ForeColor = System.Drawing.Color.Red;
}
}
In the Web.config I added the below:
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.office365.com" port="587" defaultCredentials="false" enableSsl="true"
userName="[email protected]" password="ThisPwd" targetName="STARTTLS/smtp.office365.com"
/>
</smtp>
</mailSettings>
</system.net>
Error message:
System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM [AM0PR05CA0077.eurprd05.prod.outlook.com] at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at...
Edit:
This could be a duplicate of SMTP 5.7.57 error when trying to send email via Office 365 But I couldn't find a solution there, so I'd like to reopen the discussion.