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.