title: How to send emails by using VB.NET or C# code from an office 365 account that multi factor authentication enabled on it?
body: <p>I have the following VB.NET code for sending emails from my office 365 account, it was worked fine until Microsoft forced to enable MFA on Office 365 accounts:</p>
  
  <pre><code>Imports System.Net.Mail
  
  Sub main()
  
          Dim message As MailMessage = New MailMessage()
          message.From = New MailAddress("MyEmail@MyOffice365Account.com")
          message.To.Add(New MailAddress("Receiver@AnyEmail.com"))       
          message.Subject = "My subject"
          message.Body = "My body message"
          message.IsBodyHtml = True
          message.BodyEncoding = System.Text.Encoding.UTF8
          message.SubjectEncoding = System.Text.Encoding.UTF8
          Dim smtp As New SmtpClient("smtp.office365.com", 587)
          smtp.EnableSsl = True
          smtp.Credentials = New Net.NetworkCredential("MyEmail@MyOffice365Email.com", "my password")
          smtp.Send(message)
  
  End sub
  </code></pre>
  
  <p>When I run the above code I get the following error:</p>
  
  <blockquote>
    <p>An unhandled exception of type 'System.Net.Mail.SmtpException'
    occurred in System.dll</p>
    
    <p>Additional information: 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 [DB6PR0501CA0038.eurprd05.prod.outlook.com]</p>
  </blockquote>
  
  <p>I'm sure the error is happened after I Enabled multi factor authentication on my account, and I cant disable this feature because Microsoft is forced Office 365 users to use this feature.</p>
  
  <p>Is there is any way to solve this issue? </p>