6

I'm trying to send email via nodemailer. And sender email is of microsoft azure. But I'm getting error-response: '535 5.7.139 Authentication unsuccessful, the request did not meet the criteria to be authenticated successfully. Contact your administrator. MFA is enabled. Also I have generated app password. Can anyone please help me with this issue. Code snippet to send mail-


var userTransporter = nodemailer.createTransport({
  host: 'smtp.office365.com',
  port: 587,     // secure SMTP
  secure: false,
  auth: {
    user:SENDER_EMAIL,
    pass: SENDER_APP_PASSWORD,
  }
});

    var mailOptions = {
        from: SENDER_EMAIL,
        to: email,
        template: path.join(__dirname, "../public/views/email-verification"),
        context: {
          token: otp,
          email: email,
          url: EMAIL_VERIFICATION_LINK,
          name: user_name
        },
      };
      let mail = await transporter.sendMail(mailOptions);
Delliganesh Sevanesan
  • 4,146
  • 1
  • 5
  • 15
Muskan Bansal
  • 61
  • 1
  • 1
  • 3

2 Answers2

2

If you are using 2-step authentication, you will need to create an app password.

Login to your Microsoft account.

  1. Security
  2. Advanced security options
  3. App passwords
  4. Create a new app password
transp: {
    host: smtp.office365.com,
    port: 587,
    secure: false,
    auth: {
        user: 'user@email.com',
        pass: 'AppPass'
    }
}
Daniel
  • 21
  • 3
0

I can see in your code that you are using secure: false field.
Instead of secure: false field you can change into secureConnection: false.

var userTransporter = nodemailer.createTransport({
  host: 'smtp.office365.com',
  port: 587,     // secure SMTP
  secureConnection: false,
  auth: {
    user:SENDER_EMAIL,
    pass: SENDER_APP_PASSWORD,
  }
});

Make sure to check in your package.json file the "nodemailer" : "*" dependency is added, if not make sure to add it.