title: Unable to send email from Office 365 using c# SmtpClient
body: <p>Trying to send email from my winforms app in c# using SmtpClient. Going by this MS <a href="https://docs.microsoft.com/en-au/Exchange/mail-flow-best-practices/how-to-set-up-a-multifunction-device-or-application-to-send-email-using-office-3?redirectSourcePath=%252fen-us%252farticle%252fHow-to-set-up-a-multifunction-device-or-application-to-send-email-using-Office-365-69f58e99-c550-4274-ad18-c805d654b4c4#option2" rel="nofollow noreferrer">article</a> it should work. I have searched many forums etc and have not found any solutions to this. The error message seems to suggest that client is not authenticated. The password is correct I have logged in as this user withthe password and its fine. 2FA is on but htis should not get in the way should it?</p>
  
  <p>Things I have checked are in place</p>
  
  <ol>
  <li>Enable SSl</li>
  <li>From email and user email is the same</li>
  <li>Port 587</li>
  <li>I have this account in outlook and it sends just fine</li>
  <li>Firewall is not blocking this port. I have other code using this exact same code with a non O365 host and it works just fine</li>
  </ol>
  
  <p><strong>Code</strong></p>
  
  <pre><code>            var userName = "user@domain.onmicrosoft.com";
              var password = "password";
              var msg = new MailMessage();
              msg.To.Add(new MailAddress("user@gmail.com"));
              msg.From = new MailAddress(userName);
              msg.Subject = "Test Office 365 Account";
              msg.Body = "Testing email using Office 365 account.";
              msg.IsBodyHtml = true;
  
              var client = new SmtpClient{
                  Host = "smtp.office365.com",
                  Credentials = new System.Net.NetworkCredential(userName, password),
                  Port = 587,
                  DeliveryMethod = SmtpDeliveryMethod.Network,
                  EnableSsl = true
              };
  
              client.Send(msg);
  </code></pre>
  
  <p><strong>Exception</strong></p>
  
  <ul>
  <li>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</li>
  </ul>
  
  <p><strong>MAIL KIT IMPLEMENTATION</strong> (suggestion of vasily.sib)</p>
  
  <pre><code>var message = new MimeMessage();
          message.From.Add(new MailboxAddress("Test User", "user@domain.onmicrosoft.com"));
          message.To.Add(new MailboxAddress("Gmail User", "testuser@gmail.com"));
          message.Subject = "test";
  
          message.Body = new TextPart("plain")
          {
              Text = @"test"
          };
  
          var client = new SmtpClient(new ProtocolLogger("imap.log"));
  
  
          // For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
          client.ServerCertificateValidationCallback = (s, c, h, e) =&gt; true;
  
          client.Connect("smtp.office365.com", 587, SecureSocketOptions.Auto); //this is fine and it connects
  
          var clientAuthenticationMechanisms = client.AuthenticationMechanisms;
          client.AuthenticationMechanisms.Remove("XOAUTH2");
          // Note: only needed if the SMTP server requires authentication
          client.Authenticate("user@domain.onmicrosoft.com", "password"); // this is where it fails with Authentication Failure
  
          client.Send(message);
          client.Disconnect(true);
  </code></pre>
  
  <p><strong>MAIL KIT LOG OUTPUT</strong></p>
  
  <pre><code>Connected to smtp://smtp.office365.com:587/?starttls=when-available
  S: 220 SYCP282CA0015.outlook.office365.com Microsoft ESMTP MAIL Service ready at Mon, 25 Nov 2019 04:49:36 +0000
  C: EHLO [192.168.2.50]
  S: 250-SYCP282CA0015.outlook.office365.com Hello [58.6.92.82]
  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.2.50]
  S: 250-SYCP282CA0015.outlook.office365.com Hello [58.6.92.82]
  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
  REMOVED BASE64 DATA (password, login)
  S: 535 5.7.3 Authentication unsuccessful [SYCP282CA0015.AUSP282.PROD.OUTLOOK.COM]
  </code></pre>