title: SMTP 5.7.57 error when trying to send email via Office 365
body: <p>I'm trying to set up some code to send email via <a href="https://technet.microsoft.com/en-us/library/dn554323.aspx#HowtoconfigSMTPCS" rel="noreferrer">Office 365's authenticated SMTP service</a>:</p>
  
  <pre><code>var _mailServer = new SmtpClient();
  _mailServer.UseDefaultCredentials = false;
  _mailServer.Credentials = new NetworkCredential("test.user@mydomain.com", "password");
  _mailServer.Host = "smtp.office365.com";
  _mailServer.TargetName = "STARTTLS/smtp.office365.com"; // same behaviour if this lien is removed
  _mailServer.Port = 587;
  _mailServer.EnableSsl = true;
  
  var eml = new MailMessage();
  eml.Sender = new MailAddress("test.user@mydomain.com");
  eml.From = eml.Sender;
  eml.to = new MailAddress("test.recipient@anotherdomain.com");
  eml.Subject = "Test message";
  eml.Body = "Test message body";
  
  _mailServer.Send(eml);
  </code></pre>
  
  <p>This doesn't appear to be working, and I'm seeing an exception:</p>
  
  <blockquote>
    <p>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<br>
       at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from)<br>
      at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException&amp; exception)<br>
      at System.Net.Mail.SmtpClient.Send(MailMessage message)</p>
  </blockquote>
  
  <p>I've tried <a href="https://msdn.microsoft.com/en-us/library/ty48b824%28v=vs.110%29.aspx" rel="noreferrer">enabling network tracing</a> and it appears that secure communications are established (for example, I see a line in the log for the "STARTTLS" command, and later there's a line in the log "Remote certificate was verified as valid by the user.", and the following <code>Send()</code> and <code>Receive()</code> data is not readable as plain text, and doesn't appear to contain any TLS/SSH panics)</p>
  
  <p>I can use the very same email address and password to log on to <a href="http://portal.office.com/" rel="noreferrer">http://portal.office.com/</a> and use the Outlook email web mail to send and read email, so what might be causing the authentication to fail when sending email programmatically?</p>
  
  <p>Is there any way to additionally debug the encrypted stream?</p>