使用nodemailer在Node.js中发送电子邮件

时间:2014-03-16 16:11:31

标签: node.js email nodemailer

我的目标是能够在不设置凭据的情况下发送电子邮件。为此,我选择了nodemailer模块。这是我的代码:

var nodemailer = require('nodemailer');
var message = {
  from: "test@gmail.com",
  to: "test1@gmail.com",
  subject: "Hello ✔",
  text: "Hello world ✔",
  html: "<b>Hello world ✔</b>"
};
nodemailer.mail(message);

根据文件,应该使用“直接”运输方法(实际上我根本不了解运输方法)。但不幸的是,这种方法绝对不稳定 - 有时它有时会起作用。 任何人都能对此有所了解吗?如何在不配置SMTP传输凭据的情况下发送电子邮件?

1 个答案:

答案 0 :(得分:0)

  1. 当然,但这完全取决于服务器的配置。除非您使用身份验证,否则大多数电子邮件服这是2017年
  2. 好吧,AFAIK nodemailer会根据电子邮件域检测到正确的配置,在您的示例中,您尚未设置传输器对象,因此它使用配置的默认端口25。要更改端口,请在选项中指定类型。我强烈建议您明确指定。
  3. 可能是Windows防火墙或防病毒软件阻止了外发访问。尝试获取调试/错误消息。我们需要一些东西来帮助你。
  4. 以下是nodemailer的新版本,以下是如何使用它的示例:

    const nodemailer = require('nodemailer');
    
    // create reusable transporter object using the default SMTP transport
    let transporter = nodemailer.createTransport({
        host: 'smtp.example.com',
        port: 465,
        secure: true, // secure:true for port 465, secure:false for port 587
        auth: {
            user: 'username@example.com',
            pass: 'userpass'
        }
    });
    
    // setup email data with unicode symbols
    let mailOptions = {
        from: '"Fred Foo " <foo@blurdybloop.com>', // sender address
        to: 'bar@blurdybloop.com, baz@blurdybloop.com', // list of receivers
        subject: 'Hello ✔', // Subject line
        text: 'Hello world ?', // plain text body
        html: '<b>Hello world ?</b>' // html body
    };
    
    // send mail with defined transport object
    transporter.sendMail(mailOptions, (error, info) => {
        if (error) {
            return console.log(error);
        }
        console.log('Message %s sent: %s', info.messageId, info.response);
    });
    

    我希望它有所帮助。