nodemailer无法从nodeJS

时间:2017-08-24 06:27:10

标签: node.js email nodemailer

我正在使用nodemailer向Outlook发送电子邮件。

更新

使用以下代码即可获得错误:消息失败错误

var nodemailer = require('nodemailer');

 // create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
    host: 'smtp.office365.com',
    port: 587,
    secureConnection: false, // secure:true for port 465, secure:false for port 587
    auth: {
      user: 'user@domain.com',
      pass: 'password'
    },
    tls: { ciphers: 'SSLv3' }
});

// setup email data with unicode symbols
          let mailOptions = {
            from: '"Fred Foo " <foo@blurdybloop.com>', // sender address
            to: 'myemail@companydomain.com', // list of receivers
            subject: 'Hello ✔', // Subject line
            text: 'Hello world ?', // plain text body
            html: '<b>Hello world ?</b>' // html body
          };

          transporter.sendMail(mailOptions, (error, info) => {
            if (error) {
              return console.log(error);
            }
            console.log('Message %s sent: %s', info.messageId, info.response);
          });

完整错误跟踪:

  

错误:消息失败:550 5.7.60 SMTP;客户端无权作为此发件人发送[SG2PR06MB1725.apcprd06.prod.outlook.com]       在SMTPConnection._formatError(/var/www/html/Express/Local-MEAN-dev/node_modules/nodemailer/lib/smtp-connection/index.js:557:19)       在SMTPConnection._actionSMTPStream(/var/www/html/Express/Local-MEAN-dev/node_modules/nodemailer/lib/smtp-connection/index.js:1385:34)       在SMTPConnection._responseActions.push.str(/var/www/html/Express/Local-MEAN-dev/node_modules/nodemailer/lib/smtp-connection/index.js:907:22)       在SMTPConnection._processResponse(/var/www/html/Express/Local-MEAN-dev/node_modules/nodemailer/lib/smtp-connection/index.js:706:20)       在SMTPConnection._onData(/var/www/html/Express/Local-MEAN-dev/node_modules/nodemailer/lib/smtp-connection/index.js:509:14)       在TLSSocket._socket.on.chunk(/var/www/html/Express/Local-MEAN-dev/node_modules/nodemailer/lib/smtp-connection/index.js:657:51)       在emitOne(events.js:96:13)       在TLSSocket.emit(events.js:191:7)       在readableAddChunk(_stream_readable.js:176:18)       在TLSSocket.Readable.push(_stream_readable.js:134:10)       在TLSWrap.onread(net.js:563:20)     代码:&#39; EMESSAGE&#39;,     回复:&#39; 550 5.7.60 SMTP;客户端无权作为此发件人发送[SG2PR06MB1725.apcprd06.prod.outlook.com]&#39;,     responseCode:550,     命令:&#39; DATA&#39; }

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,并通过将from字段与auth.user字段对应来解决问题。

即。以下内容对我有用。

var nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    host: 'smtp.office365.com',
    port: 587,
    auth: {
        user: 'test-user@gmail.com',
        pass: 'password'
    },
    tls: { ciphers: 'SSLv3' }
});

let mailOptions = {
    from: 'user-alias <test-user@gmail.com>', // sender address
    to: 'myemail@companydomain.com', // list of receivers
    subject: 'Hello ✔', // Subject line
    text: 'Hello world ?', // plain text body
    html: '<b>Hello world ?</b>' // html body
};

transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log(error);
    }
    console.log('Message %s sent: %s', info.messageId, info.response);
);

请注意,test-user@gmail.com的两次出现应该相同。

我认为这是因为某些电子邮件服务器希望确保声明的电子邮件发件人与真正的电子邮件发件人相同。