Categories
JavaScript Answers

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

Spread the love

To fix username and password not accepted when using Nodemailer, we set the auth property when we create the transporter object.

For instance, we write

const nodemailer = require("nodemailer");

const transporter = nodemailer.createTransport({
  service: "gmail",
  auth: {
    user: "YOUR-USERNAME",
    pass: "THE-GENERATED-APP-PASSWORD",
  },
});

const send = async () => {
  const result = await transporter.sendMail({
    from: "YOUR-USERNAME",
    to: "RECEIVERS",
    subject: "Hello World",
    text: "Hello World",
  });

  console.log(JSON.stringify(result, null, 4));
};
send();

to call createTransport with an object with the auth property set to an object with the username and password.

Then we call sendMail to send the message with the from, to, subject and text fields for the email.

It returns a promise with the result of the transmission.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.