【发布时间】:2018-01-07 04:02:43
【问题描述】:
我正在尝试使用 Google GMail API 让 nodemailer 工作。 我已经建立了我的 Google 项目 - 使用他们的 oauth 沙箱进行重定向, 我从我的开发者控制台获得了一个客户端 ID、秘密和刷新令牌。
代码
const nodemailer = require('nodemailer');
const xoauth2 = require('xoauth2');
// create reusable transporter
let transporter = nodemailer.createTransport( {
service: 'gmail',
xoauth2: xoauth2.createXOAuth2Generator({
user: 'me@myDomain.com',
clientid: 'blahblahblah.apps.googleusercontent.com',
clientSecret: 'k33p-gUeSsINg',
refreshToken: '123BritneyIsTheBest'
}),
tls: {
rejectUnauthorized: false
}
} );
// setup email data
let mailOptions = {
from: 'me@myDomain.com',
to: 'me@yahoo.com',
subject: 'Hello there Google API...',
text: 'Hello Google API',
html: '<b>Hello Google API</b>'
};
// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
if (error) {
return console.log(error);
}
console.log('Message sent: ', info.messageId);
});
错误。
{
Error: Mail command failed: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 https://support.google.com/mail/?p=WantAuthError l24sm4075119ywk.21 - gsmtp
at SMTPConnection._formatError (C:\abc\index.js:591:19)
at SMTPConnection._actionMAIL (C:\abc\index.js:1350:34)
at SMTPConnection._responseActions.push.str (C:\abc\index.js:840:18)
at SMTPConnection._processResponse (C:\abc\index.js:747:20)
at SMTPConnection._onData (C:\abc\index.js:543:14)
at TLSSocket._socket.on.chunk (C:\abc\index.js:495:47)
at emitOne (events.js:116:13)
at TLSSocket.emit (events.js:211:7)
at addChunk (_stream_readable.js:263:12)
at readableAddChunk (_stream_readable.js:250:11)
code: 'EENVELOPE',
response: '530-5.5.1 Authentication Required. Learn more at\n530 5.5.1 https://support.google.com/mail/?p=WantAuthError l24sm4075119ywk.21 - gsmtp',
responseCode: 530,
command: 'MAIL FROM'
}
我已经尝试过 - 它有效,但据我了解,这是非常不安全且不适合生产的。
const nodemailer = require('nodemailer');
// create reusable transporter object
let transporter = nodemailer.createTransport( {
host: 'smtp.gmail.com',
port: 587,
secure: false,
auth: {
user: 'me@myDomain.com',
pass: 'Seriously?'
}
});
... all the rest is the same, removed for brevity...
现有的帖子解决方案没有帮助。 谢谢你
【问题讨论】:
-
对于生产,我建议使用OAuth2 implementation 登录。您也可以查看Authorizing Your App with Gmail 以获取更多参考。
-
我的回答对您有用吗?如果您对我的回答有疑问,请随时告诉我。我想学习解决你的问题。
标签: node.js gmail-api nodemailer