0
votes

I have an application the send e-mail by the users using the outlook smtp server. This application works fine on the localhost, with the same credetions. But when I publish my ASP.NET Core application on the Azure, I receive the following exception:

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 [SN6PR04CA0015.namprd04.prod.outlook.com]

To send e-mails I am using the code:

MailMessage mail = new MailMessage(
EmailServiceConfiguration.SenderMail(),
   addresses.Aggregate((seed, value) => seed += "," + value)
);

SmtpClient client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new System.Net.NetworkCredential("[email protected]", "mypassword");
client.Host = "smtp-mail.outlook.com";
mail.Subject = "My subject";
mail.Body = "My text";
mail.IsBodyHtml = true;
client.Send(mail);

Remembering that this exception only happens when the application is running on azure, local host is work and normal.

Thanks.

2
This is a built-in protection that Azure has to reduce SPAM. That is why this will work on your local machine, not in Azure. See my answer below for more details.Ken W MSFT

2 Answers

1
votes

Azure is committed to stopping SPAM and reducing the customer impact caused by negative IP reputation. Starting on November 15th, 2017, sending outbound email directly to external domains (such as outlook.com, gmail.com) from a Virtual machine (VM) will be made available only to certain subscription types. Outbound SMTP connections using TCP port 25 (primarily used for unauthenticated e-mail delivery) will be blocked for most new subscriptions (more details here).