如何使用 smtp nodemailer 发送电子邮件

How to send emails using smtp node mailer

我试图在节点邮件程序上使用 SMTP 发送测试电子邮件,但它说连接超时。我使用的片段在下方。

const nodemailer = require("nodemailer");

async function main() {

  const nodemailer = require('nodemailer');
  let transporter = nodemailer.createTransport({
        pool:true,
         host: '213.55.96.132',
         port: 25,
         auth: {
             user: "user@ethionet.et",
             pass: "drafgthsjaid321##"
         },
         pool: true,
         logger :true,
         debug:true,
         secure: false,
 })
  transporter.verify(function(error, success) {
    if (error) {
         console.log(error);
    } else {
         console.log('Server is ready to take our messages');
    }
 });
 let mailOptions = {
    from: "user@ethionet.et",
    to: ["someemail@gmail.com",],
    subject: 'Test email',
    text: `Hello world`
  };
  transporter.sendMail(mailOptions, function(err, data) {
if (err) {
    console.log("Error " + err);
} else {
    console.log("Email sent successfully");
}
  });

}


main().catch(console.error);

我不介意泄露凭据,当我尝试通过 SMTP 从 here 发送电子邮件时它起作用了。 为什么会这样?

您需要阅读比文档第一页多一点的内容:)

创建您的消息

let message = {
    ...,
    from: 'mailer@nodemailer.com', // listed in rfc822 message header
    to: 'daemon@nodemailer.com', // listed in rfc822 message header
    envelope: {
        from: 'Daemon <deamon@nodemailer.com>', // used as MAIL FROM: address for SMTP
        to: 'mailer@nodemailer.com, Mailer <mailer2@nodemailer.com>' // used as RCPT TO: address for SMTP
    }
}

通过传输器发送消息

transporter.sendMail(...).then(info=>{
    console.log('Preview URL: ' + nodemailer.getTestMessageUrl(info));
});

原来问题是我的 ISP 阻塞了端口 25。