如何使用 Nodemailer 使用我的公司域发送电子邮件?

How can I send email with my company domain using Nodemailer?

这是我的代码:我尝试使用 outlook 帐户

const transporter = nodemailer.createTransport({
  host: 'smtp.office365.com',
  auth: {
    user: 'account@mycompanydomain.com',
    pass: 'password'
  }
});

transporter.verify(function(error, success) {
  if (error) {
    console.log(error);
  } else {
    console.log('Server is ready to take our messages');
  }
});

我不确定这里有什么问题。这些是 nodemailer 日志:

[2020-03-17 09:55:34] DEBUG Creating transport: nodemailer (6.4.5; +https://nodemailer.com/; SMTP/6.4.5[client:6.4.5])
[2020-03-17 09:55:34] DEBUG [JAejdt6KMrw] Resolved smtp.office365.com as 40.100.54.194 [cache miss]
[2020-03-17 09:55:35] INFO  [JAejdt6KMrw] Connection established to 40.100.54.194:587
[2020-03-17 09:55:35] INFO  [JAejdt6KMrw] Connection upgraded with STARTTLS
[2020-03-17 09:55:36] DEBUG [JAejdt6KMrw] SMTP handshake finished
[2020-03-17 09:55:41] INFO  [JAejdt6KMrw] User "account@mycompanydomain.com" failed to authenticate
[2020-03-17 09:55:41] DEBUG [JAejdt6KMrw] Closing connection to the server using "end"
Error: Invalid login: 535 5.7.3 Authentication unsuccessful [HK2PR04CA0060.apcprd04.prod.outlook.com]
    at SMTPConnection._formatError (C:\Users\Nonoy\Documents\sample_apps\nodemailer_outlook_integration\node_modules\nodemailer\lib\smtp-connection\index.js:784:19)
    at SMTPConnection._actionAUTHComplete (C:\Users\Nonoy\Documents\sample_apps\nodemailer_outlook_integration\node_modules\nodemailer\lib\smtp-connection\index.js:1523:34)
    at SMTPConnection.<anonymous> (C:\Users\Nonoy\Documents\sample_apps\nodemailer_outlook_integration\node_modules\nodemailer\lib\smtp-connection\index.js:1481:18)
    at SMTPConnection._processResponse (C:\Users\Nonoy\Documents\sample_apps\nodemailer_outlook_integration\node_modules\nodemailer\lib\smtp-connection\index.js:942:20)
    at SMTPConnection._onData (C:\Users\Nonoy\Documents\sample_apps\nodemailer_outlook_integration\node_modules\nodemailer\lib\smtp-connection\index.js:749:14)
    at TLSSocket.SMTPConnection._onSocketData (C:\Users\Nonoy\Documents\sample_apps\nodemailer_outlook_integration\node_modules\nodemailer\lib\smtp-connection\index.js:195:44)
    at TLSSocket.emit (events.js:223:5)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:290:11)
    at TLSSocket.Readable.push (_stream_readable.js:224:10) {
  code: 'EAUTH',
  response: '535 5.7.3 Authentication unsuccessful [HK2PR04CA0060.apcprd04.prod.outlook.com]',
  responseCode: 535,
  command: 'AUTH LOGIN'
}
[2020-03-17 09:55:41] INFO  [JAejdt6KMrw] Connection closed

PS:我公司的域托管在 Microsoft

请参考nodemaler official documentation !

即在 transporter 中你缺少 port value

(你可以把它放在 host 之后)

port – is the port to connect to (defaults to 587 if is secure is false or 465 if true)

var nodemailer = require('nodemailer');

// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport('smtps://user%40gmail.com:pass@smtp.gmail.com');

// setup e-mail data with unicode symbols
var mailOptions = {
    from: '"Simple ?" <borntest@foodcita.com>', // sender address
    to: 'serta@foodcita.com, goip@foodcita.com', // list of receivers
    subject: 'Hello ✔', // Subject line
    text: 'Hello world ?', // plaintext body
    html: '<b>Hello world ?</b>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);
});

谢谢大家的回答(都试过了)。但我找到了解决方案。

在我的 office365 帐户中,我转到了设置安全和隐私附加安全验证,点击 创建和管理应用密码 。然后它会让您生成一个应用程序密码,然后您将使用该密码作为您的 nodemailer 身份验证配置密码。如果你有像我这样的验证,这也将绕过 2 因素验证。

screenshot of settings window here

请注意,您必须立即复制生成的密码。它只会让您复制密码一次。关闭 window 后,您将无法再次查看它。

希望对您有所帮助!