【问题标题】:nodemailer and gmail APIsnodemailer 和 gmail API
【发布时间】: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...

现有的帖子解决方案没有帮助。 谢谢你

【问题讨论】:

标签: node.js gmail-api nodemailer


【解决方案1】:

这个修改怎么样?

发件人:

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
        }
    } );

收件人:

let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        type: 'oauth2',
        user: 'me@myDomain.com',
        clientId: 'blahblahblah.apps.googleusercontent.com', // This key is "clientId".
        clientSecret: 'k33p-gUeSsINg',
        refreshToken: '123BritneyIsTheBest'
    },
    tls: {
        rejectUnauthorized: false
    }
});

注意:

  • 请在检索刷新令牌时确认https://mail.google.com/是否包含在范围内。
  • 请确认gmail API是否开启。
  • 可能需要在https://myaccount.google.com/lesssecureapps 开启“允许安全性较低的应用:开启”。

参考:

在我的环境中,我确认这可以正常工作。但如果这对你没有用,我很抱歉。

【讨论】:

  • 这不起作用,不幸的是,我将不得不尝试组合你描述的选项,但现在我正在使用“不安全”的方式来完成我的演示。谢谢你。我相信我很快会再接再厉。
  • @jpmyob 很抱歉给您带来不便。当您运行此脚本时,我可以询问您有关错误消息的信息吗?通过了解错误消息,可能有助于思考解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-21
  • 2018-02-28
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多