title: Using MailKit to send through Outlook.com
body: <p>I'm trying to send an email through my Outlook.com email address using MailKit.</p>
  
  <p>I've followed all the examples I've seen online and this is what I have:</p>
  
  <pre><code>public async Task SendEmailAsync(string email, string subject, string htmlmessage)
  {
      var message = new MimeMessage();
      message.From.Add(new MailboxAddress("Service Account", "me@outlook.com"));
      message.To.Add(new MailboxAddress("First Last", email));
      message.Subject = subject;
      message.Body = new TextPart(TextFormat.Html)
      {
          Text = htmlMessage
      };
  
      using (var client = new SmtpClient())
      {
          //Have tried both false and true    
          client.Connect("smtp-mail.outlook.com", 587, false);
          client.AuthenticationMechanisms.Remove("XOAUTH2");
          client.Authenticate("me@outlook.com", "mypassword");
          await client.SendAsync(message);
          client.Disconnect(true);
      }
  
      return;
  }
  </code></pre>
  
  <p>If I set the <code>useSSL</code> parameter to <code>true</code> on <code>client.Connect()</code>, I get this error:</p>
  
  <pre><code>An error occurred while attempting to establish an SSL or TLS connection
  </code></pre>
  
  <p>If I set the <code>useSSL</code> parameter to <code>false</code>, I get this error:</p>
  
  <pre><code>AuthenticationException: AuthenticationInvalidCredentials: 5.7.3 Authentication unsuccessful
  </code></pre>
  
  <p>What am I doing wrong?</p>
  
  <p><strong>Update</strong></p>
  
  <p>Added <code>ProtocolLogger</code> per the suggestion of @jstedfast and here was the result:</p>
  
  <pre><code>Connected to smtp://smtp-mail.outlook.com:587/?starttls=when-available
  S: 220 BN6PR11CA0009.outlook.office365.com Microsoft ESMTP MAIL Service ready at Sun, 10 Feb 2019 03:26:30 +0000
  C: EHLO [192.168.1.12]
  S: 250-BN6PR11CA0009.outlook.office365.com Hello [73.175.143.94]
  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.12]
  S: 250-BN6PR11CA0009.outlook.office365.com Hello [73.175.143.94]
  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 ************
  C: ****************
  S: 334 ************
  C: ****************
  S: 535 5.7.3 Authentication unsuccessful [BN6PR11CA0009.namprd11.prod.outlook.com]
  </code></pre>
  
  <p>BTW, I commented out some stuff with <code>*</code>.  I wasn't sure what that was and if it was sensitive or not.</p>