I am trying to send mail using nodemailer
node module and configured like this.
... config.js ...
smtp: {
host: "smtp.gmail.com",
port: 465,
secure: true,
auth: {
user: "***@gmail.com",
pass: "****"
}
}
... mail.js ...
const config = require("../config");
const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport(config.smtp);
const sendEmail = async (data, res) => {
// if(!data.to || !data.subject)
// return false;
data = { ...data, from: config.smtp.auth.user };
console.log(data)
transporter.sendMail(data, (error, info) => {
console.log(error);
res.send(error);
});
res.send(true)
}
module.exports = sendEmail;
... controller ...
router.get('/testEmail', async (req, res) => {
let emailData = {
to: "***@gmail.com",
subject: "Welcome to Musical World",
content: "test test"
};
let result = sendEmail(emailData, res);
console.log(result)
// res.send(result);
})
When I try to send email, it returns
{"code":"EAUTH","response":"535-5.7.8 Username and Password not accepted. Learn more at\n535 5.7.8 https://support.google.com/mail/?p=BadCredentials p25-20020a056638217900b0033c14d2386bsm898629jak.75 - gsmtp","responseCode":535,"command":"AUTH PLAIN"}
So I did googling and found that's why I didn't turn on less secure app in my google account. But when I went there, google said
I tried to turn on less secure apps
, but G less secure apps
feature isn't available.
What do I have to do , then?
I finally resolved by myself!.
Google no longer supports less secure app
feature from 03/20/2022. It will help other developers who get stuck with this issue.
So we can't access to gmail using only emal and password
. Instead we have to use OAuth2
to get access to gmail.
... config.js ...
smtp: {
service: "gmail",
auth: {
type: "OAuth2",
user: "*****@gmail.com",
pass: "******",
clientId: "********",
clientSecret: "***********",
refreshToken: "************"
}
}
You have to have OAuth2 client in your google cloud console for this. Here's full details you can refer to.
https://www.freecodecamp.org/news/use-nodemailer-to-send-emails-from-your-node-js-server/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.