'Nodemailer with Gmail | There was an error: Error: Invalid login: 535-5.7.8 Username and Password not accepted
I am trying to set up email verification in my app using nodemailer with a Gmail account.
My problem is that it reports an error, stating that my Username and Password have not been accepted:
There was an error: Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials a3sm2007362wri.89 - gsmtp
const transporter = nodemailer.createTransport({
host: "smtp.gmail.com",
service: "Gmail",
port: 465,
secure: true,
auth: {
user: process.env.USER,
pass: process.env.PASS,
},
});
transporter.sendMail(
{
from: process.env.USER,
to: email,
subject: subject,
text: text,
},
(error) => {
if (error) {
return console.log("There was an error: " + error);
}
console.log("Email sent successfully");
}
);
I can confirm that process.env.USER and process.env.PASS are correct, and that I have also allowed less secure apps.
Is there something I am missing/not understanding?
Solution 1:[1]
Username and Password not accepted. normally means that you need to use an apps password instead of the users true password
Using OAuth 2.0 Mechanism should work as well but may be a little harder to implement.
Check if the user has 2fa enabled this is normally what causes it.
Solution 2:[2]
Use OAuth2 and nodemailer https://www.woolha.com/tutorials/node-js-send-email-using-gmail-with-nodemailer-oauth-2 and https://javascript.plainenglish.io/sending-emails-with-nodemailer-with-gmail-and-oauth2-e0b609587b7a this article should help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DaImTo |
| Solution 2 | Jaba |

