我正在尝试为我正在创建的应用设置nodemailer。我尝试运行代码时收到错误。
这是我的设置:
const nodemailer = require("nodemailer");
const xoauth2 = require("xoauth2");
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
xoauth2: xoauth2.createXOAuth2Generator({
user: '****@gmail.com',
clientId: '*******************************',
clientSecret: '*****************************',
refreshToken: '*****************************'
})
}
});
var mailOptions = {
from: 'Dave <*******@gmail.com>',
to: '*******@gmail.com',
subject: 'Nodemailer test',
text: 'Hello world'
}
transporter.sendMail(mailOptions, function(err, res) {
if (err) {
console.log(err);
} else {
console.log('Email sent');
}
})
显然,我已经仔细检查了我的client ID
,secret
和refresh token
。但是我收到以下错误:
{ 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 y22sm6749013wry.51 - gsmtp at SMTPConnection._formatError (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.j s:557:19) at SMTPConnection._actionAUTHComplete (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/ index.js:1248:34) at SMTPConnection._responseActions.push.str (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-conn ection/index.js:340:26) at SMTPConnection._processResponse (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/ind ex.js:706:20) at SMTPConnection._onData (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:5 09:14) at TLSSocket._socket.on.chunk (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp- connection/index .js:461:47) at emitOne (events.js:96:13) at TLSSocket.emit (events.js:188:7) at readableAddChunk (_stream_readable.js:176:18) at TLSSocket.Readable.push (_stream_readable.js:134:10) at TLSWrap.onread (net.js:551:20) 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=BadCrede ntials y22sm6749013wry.51 - gsmtp', responseCode: 535, command: 'AUTH PLAIN' }
这是我第一次尝试使用nodemailer,我觉得我显然错过了一个关键部分,但不确定是什么。
使用真实电子邮件和密码时出现新错误:
{ Error: Invalid login: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvZ
534-5.7.14 vX8SWYwnciMLBvsoC9zLFxi_9pKu2juDkWdPY4cJngQct2L0qjKFr3aF_SlAVCV816xj-8
534-5.7.14 eC36n8fZzITno-GnJdvwRSf6eIXfeU_ohzp07tc4S3LA0x2k9xPRwAjMlsWfNoa1Iz2GwX
534-5.7.14 AyBKjwT8nmD-wpNNK5J_bN9F3OI56XFAfw0NmjxKnUfhHXPoTs0sGCc6eRn_9hgYp2TyFe
534-5.7.14 MAd2gvxDCVp5O9V-yuGa9nrch8ey4> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/answer/78754 f135sm7148225wmd.7 - gsmtp
at SMTPConnection._formatError (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp- connection/index.js:557:19)
at SMTPConnection._actionAUTHComplete (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp- connection/index.js:1248:34)
at SMTPConnection._responseActions.push.str (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:340:26)
at SMTPConnection._processResponse (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:706:20)
at SMTPConnection._onData (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:509:14)
at TLSSocket._socket.on.chunk (/home/ubuntu/workspace/NUFC/NUFC_Blogv1.2/node_modules/nodemailer/lib/smtp-connection/index.js:461:47)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
at TLSWrap.onread (net.js:551:20)
代码:'EAUTH', 回复:'534-5.7.14请通过网络浏览器和\ n534-5.7.14登录,然后再试一次。\ n534-5.7.14了解更多信息,请访问\ n534 5.7.14 https://support.google.com/mail/answer/78754 f135sm7148225wmd.7 - gsmtp ”, responseCode:534, 命令:'AUTH PLAIN'}
答案 0 :(得分:5)
您需要使用真实Google帐户的实际email
和password
以不同方式设置转运工具。
// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'somerealemailaddress@gmail.com', //email address to send from
pass: 'somerealemailpassword' //the actual password for that account
}
});
答案 1 :(得分:1)
示例代码:Nodemailer中的OAuth2
var email_smtp = nodemailer.createTransport({
host: "smtp.gmail.com",
auth: {
type: "OAuth2",
user: "youremail@gmail.com",
clientId: "CLIENT_ID_HERE",
clientSecret: "CLIENT_SECRET_HERE",
refreshToken: "REFRESH_TOKEN_HERE"
}
});
答案 2 :(得分:0)
const xoauth2 = require(“ xoauth2”); 实际上,在我的情况下,我没有使用上面提到的代码,但是效果很好...只是从错误中发送的链接更改了Google设置,就可以了。 但是我遇到了错误,尤其是当我尝试使用 yahoo 而不是 google 作为服务提供商时。附有错误的屏幕截图,供您参考。 The error which i get while using yahoo as service provider