how do i use MailMessage with MFA or send emails with MFA enabled?

312 Views Asked by At

I am making an app with ASP.Net and I am using Sytem.Net.Mail.MailMessage function to send emails when a new entry is added to the database, but my business is using MFA on its accounts. I was wondering, whether there is any way to circumvent MFA; without the need to disable it in the first place.

Example code

MailAddress to = new MailAddress("[email protected]");
MailAddress from = new MailAddress("[email protected]");

MailMessage message = new MailMessage(from, to);
message.Subject = "New item added";
message.Body = "A new item has been added to the databse and is waiting approval.";

SmtpClient client = new SmtpClient("outlook.office365.com", 587)
   {
      Credentials = new NetworkCredential("[email protected]", "Password"),
      EnableSsl = true
   };
// code in brackets above needed if authentication required

   try
    {
      client.Send(message);
    }
   catch (SmtpException ex)
    {
      Console.WriteLine(ex.ToString());
    }
0

There are 0 best solutions below