Nodemailer在实时服务器上失败

时间:2018-09-19 20:40:23

标签: javascript node.js authentication server nodemailer

每当我尝试从本地运行的服务器发送电子邮件时,它都可以正常工作,但是在生产环境中会失败。

这是日志文件:

at SMTPConnection._actionAUTHComplete (/home/7278/mailsender/node_modules/nodemailer/lib/smtp-connection/index.js:1335:34)
    at SMTPConnection._responseActions.push.str (/home/7278/mailsender/node_modules/nodemailer/lib/smtp-connection/index.js:1293:18)
    at SMTPConnection._processResponse (/home/7278/mailsender/node_modules/nodemailer/lib/smtp-connection/index.js:762:20)
    at SMTPConnection._onData (/home/7278/mailsender/node_modules/nodemailer/lib/smtp-connection/index.js:558:14)
    at TLSSocket._socket.on.chunk (/home/7278/mailsender/node_modules/nodemailer/lib/smtp-connection/index.js:709:51)
    at TLSSocket.emit (events.js:182:13)
    at TLSSocket.EventEmitter.emit (domain.js:442:20)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    code: 'EAUTH',   
    response:
          '535 5.7.3 Authentication unsuccessful [YTOPR0101CA0050.CANPRD01.PROD.OUTLOOK.COM]',   responseCode: 535,   command: 'AUTH LOGIN' }

{ Error: Invalid login: 535 5.7.3 Authentication unsuccessful [YTXPR0101CA0004.CANPRD01.PROD.OUTLOOK.COM]
    at SMTPConnection._formatError (/home/7278/mailsender/node_modules/nodemailer/lib/smtp-connection/index.js:606:19)
    at SMTPConnection._actionAUTHComplete (/home/7278/mailsender/node_modules/nodemailer/lib/smtp-connection/index.js:1335:34)
    at SMTPConnection._responseActions.push.str (/home/7278/mailsender/node_modules/nodemailer/lib/smtp-connection/index.js:1293:18)
    at SMTPConnection._processResponse (/home/7278/mailsender/node_modules/nodemailer/lib/smtp-connection/index.js:762:20)
    at SMTPConnection._onData (/home/7278/mailsender/node_modules/nodemailer/lib/smtp-connection/index.js:558:14)
    at TLSSocket._socket.on.chunk (/home/7278/mailsender/node_modules/nodemailer/lib/smtp-connection/index.js:709:51)
    at TLSSocket.emit (events.js:182:13)
    at TLSSocket.EventEmitter.emit (domain.js:442:20)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
  code: 'EAUTH',
  response:
   '535 5.7.3 Authentication unsuccessful [YTXPR0101CA0004.CANPRD01.PROD.OUTLOOK.COM]',
  responseCode: 535,
  command: 'AUTH LOGIN' }

这是node.js服务器的代码:

#!/usr/bin/env node
const http = require("http");
const url = require('url');
const nodemailer = require('nodemailer');

const transporter = nodemailer.createTransport({
  service: 'Hotmail',

  auth: {
    user: 'my email',
    pass: 'my password'
  },

});

const server = http.createServer(function (req, res) {
    //console.log(res.getHeader("Message-Author"));
    //Parse the address:
    var q = url.parse(req.url, true);

    /*The parse method returns an object containing url properties*/
    /*The query property returns an object with all the querystring parameters as properties:*/
    var qdata = q.query;
    /*console.log(qdata.name);
    console.log(qdata.email);
    console.log(qdata.subject);
    console.log(qdata.message);*/

    var mailOptions = {
      from: 'my email',
      to: 'my email',
      subject: '['+qdata.name+', '+qdata.email+']'+qdata.subject,
      text: '[tag]\n\n'+qdata.message
    }; 

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

    res.writeHead(302, {
        'Location': 'url here'
    });
    res.end();
});

server.listen(80, (err) => {
    if ( ! err) {
        console.log(`server is listening on 80`)
    }
})

我不知道为什么在本地运行时它可以连接并正常发送电子邮件,但是告诉我Authentication unsuccessful服务器处于活动状态。任何帮助/指针将不胜感激。

2 个答案:

答案 0 :(得分:0)

您需要为此连接打开一个端口。 从nodemailer文档中:

port –是要连接的端口(如果安全性为false,则默认为587,如果为true,则为465)

答案 1 :(得分:0)

尝试一下,

const transporter = nodemailer.createTransport({
host: "smtp-mail.outlook.com", // hostname
secureConnection: false, // TLS requires secureConnection to be false
port: 587, // port for secure SMTP - TLS
tls: {
   ciphers:'SSLv3'
},
auth: {
    user: 'yourmail@live.com',
    pass: 'password'
}

});

相关问题