How to fix username and password not accepted when using Nodemailer?

Sometimes, we want to fix username and password not accepted when using Nodemailer.

In this article, we’ll look at how to fix username and password not accepted when using Nodemailer.

How to fix username and password not accepted when using Nodemailer?

To fix username and password not accepted when using Nodemailer, we should try to allow less secure apps to access account.

We can allow less secure apps to access a Gmail account by going to https://myaccount.google.com/lesssecureapps and then toggle the option on.

Then we can log in and and send an email message with

const nodemailer = require("nodemailer");

const transporter = nodemailer.createTransport({
  host: "smtp.gmail.com",
  port: 587,
  secure: false,
  requireTLS: true,
  auth: {
    user: "[email protected]",
    pass: "your.password",
  },
});

const mailOptions = {
  from: "[email protected]",
  to: "[email protected]",
  subject: "Test",
  text: "Hello World!",
};

transporter.sendMail(mailOptions, (error, info) => {
  if (error) {
    return console.log(error.message);
  }
  console.log("success");
});

We create a nodemailer.createTransport instance with the credentials and SMTP server host address.

And then we call sendMail with the mailOptions object that has the values for the email message to send the email.

Conclusion

To fix username and password not accepted when using Nodemailer, we should try to allow less secure apps to access account.