Categories
JavaScript Answers

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

Spread the love

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: "your.gmail.account@gmail.com",
    pass: "your.password",
  },
});

const mailOptions = {
  from: "your.gmail.account@gmail.com",
  to: "receivers.email@domain.com",
  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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *