Sending email with smtp while two factor enable

Shahid Majeed 1 Reputation point
2021-10-25T06:23:08.787+00:00

HI,

I am sending email with smtp in my web application recently IT departement enable two factor authentication for the emails. After that email sending start failing with error "Failure sending Email".

So i want to know what should we need to do to send email when two factor authentications enable.
Application is in WebForms and .net 4

Here is my current code.

public string SendEmail(string ToList, string CCList, string BCCList, string Subject, string MessageBody)
{
    try
    {
        System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();

        mail.BodyEncoding = System.Text.Encoding.UTF8;
        mail.HeadersEncoding = System.Text.Encoding.UTF8;
        mail.SubjectEncoding = System.Text.Encoding.UTF8;

        // To List
        addEmailAddress(ToList, ref mail);

        //CC List
        addEmailAddress(CCList, ref mail);

        //BCC List
        addEmailAddress(BCCList, ref mail);

        mail.From = new System.Net.Mail.MailAddress(email, displayName);
        mail.Subject = Subject;

        mail.Body = MessageBody;
        mail.Priority = System.Net.Mail.MailPriority.High;
        mail.IsBodyHtml = true;

        System.Net.Mail.SmtpClient Smtp = new System.Net.Mail.SmtpClient(smtpMailServer);
        Smtp.EnableSsl = enableSsl;
        //Smtp.UseDefaultCredentials = false;

        Smtp.Credentials = new System.Net.NetworkCredential(email, password);
        Smtp.Send(mail);

        log.Debug(userName + ": CommonFunction SendEmail_1 end with OK status");
        return "OK";
    }
    catch (Exception exc)
    {
        log.Error(userName + ": Exception in CommonFunction SendEmail_1: " + exc.StackTrace.ToString());
        log.Error(userName + ": Exception in CommonFunction SendEmail_1: " + exc.Message.ToString());
        throw new Exception(exc.Message);
    }
}
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,290 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,071 Reputation points
    2021-10-26T05:27:14.86+00:00

    Hi @Shahid Majeed ,
    If you’re using Microsoft Outlook with the two-step verification (2FA) turned on, you’ll need to:

    1. Go to the Security basics page and sign in to your Microsoft account.
    2. Select more security options.
    3. Under App passwords, select Create a new app password. A new app password is generated and appears on your screen.
    4. Enter this app password where you would enter your normal password along with regular Outlook SMTP/IMAP settings: IMAP: imap-mail.outlook.com
      Port: 993
      SMTP: smtp-mail.outlook.com
      Port: 587
      With this setup you will be able to use your Microsoft Outlook account in Growbots!
      Best regards,
      Yijing Sun

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.