title: Outlook SMTP Oauth Send - Authentication Unsuccessful
body: <p>I have been trying to implement email functionality in a legacy app using the <a href="https://docs.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth" rel="nofollow noreferrer">Legacy Mail API</a> to add OAuth support after getting a token through the <a href="https://docs.microsoft.com/en-us/azure/active-directory/develop/msal-authentication-flows#device-code" rel="nofollow noreferrer">Device Code Flow</a> starting with the <a href="https://github.com/azure-samples/active-directory-dotnetcore-devicecodeflow-v2" rel="nofollow noreferrer">Microsoft Sample Project</a>.</p>
  
  <p>Along that route I have added <code>SMTP.Send</code> and many other API permissions to find the missing piece. (Including <code>{ "User.Read", "User.ReadBasic.All", "SMTP.Send", "offline_access", "Mail.Send" }</code> in fear of missing one) </p>
  
  <p>I have been testing with the <a href="https://github.com/jstedfast/MailKit" rel="nofollow noreferrer">MailKit library</a> to build a proof of concept.</p>
  
  <p>So far I have the following Code Snippet that fails after trying to authenticate.</p>
  
  <pre><code>public void SendSmtpMessageAsync(string id, string accessToken)
  {
      var message = new MimeMessage();
      message.From.Add(new MailboxAddress("From Name", "From Address@Example.com"));
      message.To.Add(new MailboxAddress("To Name", "To Address@Example.com"));
      message.Subject = "How you doin'?";
  
      message.Body = new TextPart("plain")
      {
          Text = @"Test Email Content"
      };
  
      using (var client = new SmtpClient(new ProtocolLogger(Console.OpenStandardOutput())))
      {
          try
          {
              client.Connect("smtp.office365.com", 587, SecureSocketOptions.StartTls);
  
              var oauth2 = new SaslMechanismOAuth2(id, accessToken);
  
              var temp = client.AuthenticationMechanisms;
              client.Authenticate(oauth2);
  
              client.Send(message);
              client.Disconnect(true);
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex);
          }
      }
  }
  </code></pre>
  
  <p><br /></p>
  
  <h1>MailKit Log</h1>
  
  <p>I have enabled logging and gotten a log that shows the client connecting than sending the token to authenticate but the authentication failing.</p>
  
  <pre><code>Connected to smtp://smtp.office365.com:587/?starttls=always
  S: 220 MW3PR05CA0003.outlook.office365.com Microsoft ESMTP MAIL Service ready at Mon, 25 May 2020 21:31:07 +0000
  C: EHLO [192.168.0.7]
  S: 250-MW3PR05CA0003.outlook.office365.com Hello [&lt;&lt;My IP&gt;&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.0.7]
  S: 250-MW3PR05CA0003.outlook.office365.com Hello [&lt;&lt;My IP&gt;&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 XOAUTH2 &lt;&lt;Token omitted but I have confirmed that it is Base64 encoded and 
  in the format of base64("user=" + userName + "^Aauth=Bearer " + accessToken + "^A^A")&gt;&gt;
  S: 535 5.7.3 Authentication unsuccessful [MW3PR05CA0003.namprd05.prod.outlook.com]
  MailKit.Security.AuthenticationException: 535: 5.7.3 Authentication unsuccessful 
  [MW3PR05CA0003.namprd05.prod.outlook.com]
  </code></pre>
  
  <p>Any direction or resources would be appreciated since most existing posts are from pre-2020 when Legacy SMTP support was added. Additionally, if you see any misunderstanding let me know so I can do some additional reading.</p>