gpt4 book ai didi

node.js - Nodejs nodemailer 用户名和密码不被接受

转载 作者:搜寻专家 更新时间:2023-11-01 00:35:15 25 4
gpt4 key购买 nike

我正在尝试使用 nodejs 和 nodemailer 发送邮件,但它对我不起作用。我有这段代码:

const config = require('../../config');
const http = require('http');
const nodemailer = require('nodemailer');

module.exports = {

sendMail: function (params) {
return new Promise((resolve, reject) => {

var transporter = nodemailer.createTransport({
//service: 'gmail', //I tried with this too
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: params.fromMail,
pass: params.fromPass
}
});

var mailOptions = {
from: params.fromMail,
to: params.toMailList,
subject: params.subject,
html: params.mailHtmlContent,
};

transporter.sendMail(mailOptions, function(err, info){
if (err) {
reject(err);
} else {
resolve(info.response);
}
});


});
}

};

我收到这个错误:

{ 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 p6sm97493129wrx.50 - gsmtp at SMTPConnection._formatError (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:774:19) at SMTPConnection._actionAUTHComplete (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:1509:34) at SMTPConnection._responseActions.push.str (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:547:26) at SMTPConnection._processResponse (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:933:20) at SMTPConnection._onData (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:739:14) at TLSSocket._socket.on.chunk (C:\Users\Carlos\Desktop\Proyectos\nodeServer\node_modules\nodemailer\lib\smtp-connection\index.js:691:47) at TLSSocket.emit (events.js:182:13) at addChunk (_stream_readable.js:283:12) at readableAddChunk (_stream_readable.js:264:11) at TLSSocket.Readable.push (_stream_readable.js:219:10) 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 p6sm97493129wrx.50 - gsmtp', responseCode: 535, command: 'AUTH PLAIN' }

我已经更改了我的 gmail 帐户中关于“不太安全的应用程序”的相应参数。我也尝试使用另一个非 gmail 帐户。我试过这个:

service: 'gmail',

这是连接:

host: 'smtp.gmail.com',
port: 465,
secure: true,

我在 stackoverflow 中阅读了所有“类似问题”,但我不知道我是否还需要其他任何东西,或者我是否做错了什么。 (当然两个账号的用户名和密码我都查过了,没问题)。

我做错了什么?

最佳答案

var nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
service: 'gmail',//smtp.gmail.com //in place of service use host...
secure: false,//true
port: 25,//465
auth: {
user: 'example@gmail.com',
pass: 'password'
}, tls: {
rejectUnauthorized: false
}
});

transporter.sendEMail = function (mailRequest) {
return new Promise(function (resolve, reject) {
transporter.sendMail(mailRequest, (error, info) => {
if (error) {
reject(error);
} else {
resolve("The message was sent!");
}
});
});
}

module.exports = transporter;

用法:

//import transporter JS file
const mail = require('../utils/mail');
let htmlContent = `
<h1><strong>Contact Form</strong></h1>
<p>Hi,</p>
<p>${name} contacted with the following Details</p>
<br/>
<p>Email: ${email}</p>
<p>Phone: ${phone}</p>
<p>Company Name: ${companyName}</p>
<p>Message: ${message}</p>
`
let mailOptions = {
from: "Example <example@gmail.com>",
to: "me@gmail.com",
subject: "Mail Test",
text: "",
html: htmlContent
}

mail.sendMail(mailOptions)
.then(function (email) {
res.status(200).json({ success: true, msg: 'Mail sent' });
}).catch(function (exception) {
res.status(200).json({ success: false, msg: exception });
});

注意:

  1. 在使用 gmail 发送电子邮件之前,您必须允许不安全的应用程序访问 gmail,您可以通过转到 gmail 设置来执行此操作 https://myaccount.google.com/lesssecureapps?pli=1

  2. 同时启用验证码 https://accounts.google.com/DisplayUnlockCaptcha

关于node.js - Nodejs nodemailer 用户名和密码不被接受,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54219737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com