0

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

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

根据文档,应该使用“直接”传输方法(实际上我对传输方法一无所知)。但不幸的是,这种方法绝对不稳定——有时有效,有时无效。任何人都可以对此有所了解吗?如何在不配置 SMTP 传输凭据的情况下发送电子邮件?

4

1 回答 1

0
  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);
});

我希望它有所帮助。

于 2017-07-25T07:07:01.873 回答