title: Why does authenticating with Office365 SMTP claim that the correct credentials are invalid?
body: <p>I have been attempting to utilise Office 365' SMTP functionality, but have run into difficulties when trying to authenticate with it.</p>
  
  <p>This is the code I'm testing with:</p>
  
  <pre><code>string host = "smtp.office365.com";
  int port = 587;
  string username = "&lt;redacted&gt;";
  string password = "&lt;redacted&gt;";
  
  var client = new SmtpClient(new ProtocolLogger("C:\\temp\\mailerLog.log"));
  
  client.Connect(host, port, MailKit.Security.SecureSocketOptions.Auto);
  {
      client.Authenticate(new NetworkCredential(username, password));
  }
  </code></pre>
  
  <p>Now, although I have obviously removed the credentials from this example, those credentials are absolutely the ones that I can copy and paste to log into <code>outlook.office365.com</code> in a web browser with no issues.</p>
  
  <p>When running the above however, a <code>MailKit.Security.AuthenticationException</code> is thrown with the following message (I've redacted the parts that may be identifying information):</p>
  
  <pre><code>535: 5.7.3 Authentication unsuccessful [&lt;redacted&gt;.&lt;redacted&gt;.PROD.OUTLOOK.COM]
  </code></pre>
  
  <p>The <code>InnerException</code> is of type <code>MailKit.Net.Smtp.SmtpCommandException</code> and has a <code>StatusCode</code> of <code>AuthenticationInvalidCredentials</code>, which suggests the credentials must be wrong somehow...but why?  What could/should the credentials be for office 365 other than the account itself?  What am I doing wrong?</p>
  
  <p>I feel like there must be a step I'm missing, such as something that needs configuring in Office365, but after trawling the Admin panel, I'm not sure what it could be.</p>
  
  <p>I've also tried having a look at what gets logged, but it doesn't seem to reveal that much to me.  I've included it below (with identifying information redacted) in case someone more familiar with SMTP notices something which reveals what's wrong:</p>
  
  <pre><code>Connected to smtp://smtp.office365.com:587/?starttls=when-available
  S: 220 &lt;redacted&gt;.outlook.office365.com Microsoft ESMTP MAIL Service ready at Fri, 27 Sep 2019 20:52:52 +0000
  C: EHLO [192.168.1.17]
  S: 250-&lt;redacted&gt;.outlook.office365.com Hello [&lt;redacted&gt;]
  S: 250-SIZE 157286400
  S: 250-PIPELINING
  S: 250-DSN
  S: 250-ENHANCEDSTATUSCODES
  S: 250-STARTTLS
  S: 250-8BITMIME
  S: 250-BINARYMIME
  S: 250-CHUNKING
  S: 250 SMTPUTF8
  C: STARTTLS
  S: 220 2.0.0 SMTP server ready
  C: EHLO [192.168.1.17]
  S: 250-&lt;redacted&gt;.outlook.office365.com Hello [&lt;redacted&gt;]
  S: 250-SIZE 157286400
  S: 250-PIPELINING
  S: 250-DSN
  S: 250-ENHANCEDSTATUSCODES
  S: 250-AUTH LOGIN XOAUTH2
  S: 250-8BITMIME
  S: 250-BINARYMIME
  S: 250-CHUNKING
  S: 250 SMTPUTF8
  C: AUTH LOGIN
  S: 334 VXNlcm5hbWU6
  C: &lt;redacted&gt;
  S: 334 UGFzc3dvcmQ6
  C: &lt;redacted&gt;
  S: 535 5.7.3 Authentication unsuccessful [&lt;redacted&gt;.&lt;redacted&gt;.PROD.OUTLOOK.COM]
  </code></pre>
  
  <p>Also, I do not believe this question is a duplicate, as whilst there are other similar questions on Stackoverflow, they either use Microsoft's deprecated <code>System.Net.Mail.SmtpClient</code> or involve full-fledged Exchange servers where server-side configuration is readily available (unless I've missed something somewhere).</p>