【问题标题】:Send email in Node.js with nodemailer使用 nodemailer 在 Node.js 中发送电子邮件
【发布时间】:2014-03-16 16:11:30
【问题描述】:

我的目标是能够在不设置凭据的情况下发送电子邮件。为此,我选择了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 传输凭据的情况下发送电子邮件?

【问题讨论】:

标签: node.js email nodemailer


【解决方案1】:
  1. 当然可以,但这完全取决于服务器的配置。除非您使用身份验证,否则大多数电子邮件服务器将无法工作。现在是 2017 年?
  2. 好吧,AFAIK nodemailer 根据电子邮件域检测到正确的配置,在您的示例中,您没有设置传输器对象,因此它使用配置的默认端口 25。要更改端口,请在选项中指定类型。我强烈建议您明确指定它。
  3. 可能是 Windows 防火墙或防病毒软件阻止了传出访问。尝试获取调试/错误消息。我们需要一些东西来为您提供更多帮助。

这是一个新版本的 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);
});

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-13
    • 2021-08-23
    • 2019-11-05
    • 2020-11-17
    • 2015-05-19
    相关资源
    最近更新 更多