nodemailer和gmail API

时间:2018-01-07 04:02:44

标签: node.js gmail-api nodemailer

我试图让nodemailer使用Google GMail API。 我已经设置了我的Google项目 - 使用他们的oauth沙箱进行重定向, 我的开发人员控制台上有一个clientID,secret和refresh令牌。

守则

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...

现有的职位解决方案没有帮助。 谢谢

1 个答案:

答案 0 :(得分:0)

这次修改怎么样?

来自:

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。
  • 可能需要启用&#34;允许安全性较低的应用:ON&#34;在https://myaccount.google.com/lesssecureapps

参考:

在我的环境中,我确认这很好用。但如果这对你没用,我很抱歉。