通过Nodemailer发送邮件时,出现无效登录错误

时间:2018-10-05 04:03:42

标签: node.js email smtp nodemailer

我试图通过使用此代码使用nodejs nodemailer发送邮件

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 587,
    secure: false,
    requireTLS: true,
  auth: {
    user: 'mymail@gmail.com',
    pass: 'mypassword'
  }
});

var mailOptions = {
  from: 'mymail@gmail.com',
  to: 'tosendmail@gmail.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + 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 c124-v6sm6149345pfa.119 - gsmtp
    at SMTPConnection._formatError (/home/rajat/node_modules/nodemailer/lib/smtp-connection/index.js:606:19)
    at SMTPConnection._actionAUTHComplete (/home/rajat/node_modules/nodemailer/lib/smtp-connection/index.js:1335:34)
    at SMTPConnection._responseActions.push.str (/home/rajat/node_modules/nodemailer/lib/smtp-connection/index.js:366:26)
    at SMTPConnection._processResponse (/home/rajat/node_modules/nodemailer/lib/smtp-connection/index.js:762:20)
    at SMTPConnection._onData (/home/rajat/node_modules/nodemailer/lib/smtp-connection/index.js:558:14)
    at TLSSocket._socket.on.chunk (/home/rajat/node_modules/nodemailer/lib/smtp-connection/index.js:510: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: '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 c124-v6sm6149345pfa.119 - gsmtp',
  responseCode: 535,
  command: 'AUTH PLAIN' }

我用reference

通过nodejs发送电子邮件的正确方法

以及下面通过使用上述参考文献给出的代码:

const nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 587,
    secure: false,
    requireTLS: true,
    auth: {
        user: 'mymail@gmail.com',
        pass: 'mypassword'
    }
});

let mailOptions = {
    from: 'mymail@gmail.com',
    to: 'tosendmail@gmail.com',
    subject: 'Test',
    text: 'Hello World!'
};

transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log(error.message);
    }
    console.log('success');
});

我得到了成功的消息。

现在我想问一下以上两个代码都差不多,那么我的第一个代码有什么问题。 我试图通过做一些小的改动来使第一个作品起作用,但未能做到。我该怎么做才能使第一个作品起作用。

0 个答案:

没有答案