title: Azure Web App cannot send email from smtp.office365.com
body: <p>I'm trying to send email from Azure Web App using Office365. Later, we'll move it to Azure Web Job or Azure Functions. </p>
  
  <p>The following sample code works in <strong><em>local development machine</em></strong>, as well as in Azure using <strong><em>on-premise SMTP server at same port 587</em></strong>. However, the same credentials as local machine throws the exception from <strong><em>smtp.office365.com</em></strong> in Azure. </p>
  
  <p><strong><em>It seems like the problem lies between Azure and Office 365.</em></strong> Any help would be greatly appreciated!</p>
  
  <p><a href="https://blogs.msdn.microsoft.com/benjaminperkins/2017/01/11/sending-email-from-an-azure-web-app-using-an-o365-smtp-server/" rel="nofollow noreferrer">Sending email from an Azure App Service using an O365 SMTP server</a></p>
  
  <pre><code>MailMessage msg = new MailMessage();
  msg.To.Add(new MailAddress("To Email Address", "Benjamin"));
  msg.From = new MailAddress("From Email Address", "You");
  msg.Subject = "Azure Web App Email using smtp.office365.com";
  msg.Body = "Test message using smtp.office365.com on Azure from a Web App";
  msg.IsBodyHtml = true;
  SmtpClient client = new SmtpClient();
  client.UseDefaultCredentials = false;
  client.Credentials = new System.Net.NetworkCredential("O365 UID", "O365 PASS");
  client.Port = 587;
  client.Host = "smtp.office365.com";
  client.DeliveryMethod = SmtpDeliveryMethod.Network;
  client.EnableSsl = true;
  client.Send(msg); &lt;-- Line 34
  </code></pre>
  
  <h2>Exception</h2>
  
  <blockquote>
    <p>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 [XXXXX.namprd02.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&amp; exception) at
    System.Net.Mail.SmtpClient.Send(MailMessage message) at line 34</p>
  </blockquote>