使用nodejs(nodemailer)设置stmp邮件

时间:2018-07-09 08:58:12

标签: node.js smtp nodemailer

我正在尝试使用一个简单的nodejs应用程序发送电子邮件。但是我得到了错误 错误是:

  

错误:无效的登录:534-5.7.14请通过Web浏览器登录,然后   534-5.7.14,然后重试。   534-5.7.14了解更多信息   534 5.7.14 https://support.google.com/mail/answer/78754 22-v6sm29249729pfl.126-gsmtp       在SMTPConnection._formatError(C:\ Users \ USER \ Desktop \ codeworkr \ node_modules \ nodemailer \ lib \ smtp-connection \ index.js:606:19)       在SMTPConnection._actionAUTHComplete(C:\ Users \ USER \ Desktop \ codeworkr \ node_modules \ nodemailer \ lib \ smtp-connection \ index.js:1335:34)       在SMTPConnection._responseActions.push.str(C:\ Users \ USER \ Desktop \ codeworkr \ node_modules \ nodemailer \ lib \ smtp-connection \ index.js:366:26)       在SMTPConnection._processResponse(C:\ Users \ USER \ Desktop \ codeworkr \ node_modules \ nodemailer \ lib \ smtp-connection \ index.js:762:20)       在SMTPConnection._onData(C:\ Users \ USER \ Desktop \ codeworkr \ node_modules \ nodemailer \ lib \ smtp-connection \ index.js:558:14)       在TLSSocket._socket.on.chunk(C:\ Users \ USER \ Desktop \ codeworkr \ node_modules \ nodemailer \ lib \ smtp-connection \ index.js:510:47)       在emitOne上(events.js:116:13)       在TLSSocket.emit(events.js:211:7)       在addChunk(_stream_visible.js:263:12)       在readAddChunk(_stream_visible.js:250:11)       在TLSSocket.Readable.push(_stream_visible.js:208:10)       在TLSWrap.onread(net.js:597:20)

我的代码是这样:

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
  service: 'gmail',
  secure: false,
 // port: 25,
  auth: {
    user: 'surendrap7208@gmail.com',
    pass: ''
  }
 /* tls: {
    rejectUnauthorized: false
  }*/
});



var mailOptions = {
  from: 'surendrap7208@gmail.com',
  to: 'surendrap720@gmail.com',
  subject: 'Sending Email using Node.js',
  text: 'Hello'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);=
  }

    });

出于安全考虑,我已删除密码。

2 个答案:

答案 0 :(得分:1)

仔细检查您的凭据,还需要允许使用安全性较低的gmail应用。

1)转到https://myaccount.google.com/lesssecureapps

2)启用“不太安全的应用程序”选项

答案 1 :(得分:0)

我对此有另一种解决方案。如果出于安全考虑要删除密码,那么您也可以使用我的解决方案。

首先,您需要一些发件人帐户凭据,您可以从此处创建该凭据: enter image description here

Node.js,Nodemailer,SMTP问题

这是我的测试代码[file:server.js]

//start server
var http = require('http');
// Create a Transport instance using nodemailer
var nodemailer = require('nodemailer');

var smtpTransport = nodemailer.createTransport({
    host: "smtp.gmail.com",
    auth: {
        type: "OAuth2",
        user: "sender_email_id",
        clientId: "YOUR_CLIENT_ID",
        clientSecret: "YOUR_CLIENT_SECRET",
        refreshToken: "YOUR_REFRESH_TOKEN"
      }
});

var htmlBody = '<h2>Hello Body</h2>';
// Setup mail configuration
var mailOptions = {
    from: 'serder_email_id', // sender address
    to: 'recipent_email_id', // list of receivers
    subject: 'TEST SUBJECT', // Subject line
    text: 'Hello Body', // plaintext body
    html: htmlBody // html body
  };

var app = http.createServer(function (req, res) {
    // send mail
    smtpTransport.sendMail(mailOptions, function(error, info) {
        res.writeHead(200, {'Content-Type': 'text/html'});
        if (error) {
            console.log('error', error)
            res.write('Error');
            res.end();
        } 
        console.log('Message %s sent: %s', info.messageId, info.response);
        smtpTransport.close();
        res.write('Email sent');
        res.end();
    });
}).listen(3000);