【问题标题】:Can Nodemailer be a standalone SMTP server?Nodemailer 可以作为独立的 SMTP 服务器吗?
【发布时间】:2023-03-13 08:23:01
【问题描述】:

我正在阅读 Nodemailer 是 using SMTP or sendmail or Amazon SES 并且我还发现了更多 smtp servers 所以我假设除了我的 nodemailer 应用程序之外,我必须让其中一个 SMTP 服务器运行并让我的应用程序连接到它但是有人告诉我 Nodemailer 本身可以是一个自治的(或者可能是独立的)SMTP 服务器“Nodemailer 可以自主发送邮件(不需要通过服务器 / SMTP 服务)”。

所以我再次阅读它的文档,发现 Nodemailer 传输对象默认使用 nodemailer-smtp-transport 模块

var transporter = nodemailer.createTransport(smtpTransport(options)) (1)

或(默认使用 smtpTransport)

var transporter = nodemailer.createTransport(options) (2)

(我想这意味着(1)等价于(2))

所以我想也许是 nodemailer-smtp-transport 使 Nodemailer 成为一个独立的 smtp 服务器,然后我尝试使用此代码来定义 Nodemailer 传输器并使其使用 sendMail 发送电子邮件:(我们称之为sn-p1)

var transporter = nodemailer.createTransport(smtpTransport({
    host: 'localhost',
    port: 465,
    auth: {
        user: 'username',
        pass: 'pass'
    }
}));

// NB! No need to recreate the transporter object. You can use 
// the same transporter object for all e-mails 

// setup e-mail data with unicode symbols 
var mailOptions = {
    from: 'Ahmed Feki ✔ <fekiajob@gmail.com>', // sender address 
    to: 'fekiajob@yahoo.fr, mido.feki@gmail.com', // list of receivers 
    subject: 'Hello ✔', // Subject line 
    text: 'Hello world ✔', // plaintext body 
    html: '<b>Hello world from nodemailer test ✔</b>' // html body 
};


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

});

当我运行应用程序时出现此错误:

sendMail 错误:错误:连接 ECONNREFUSED 127.0.0.1:25

所以我决定使用这段代码在同一个应用程序中使用 smtp 服务器:

var SMTPServer = require('smtp-server').SMTPServer;

var server = new SMTPServer({
    onConnect: function(session, callback){
        if(session.remoteAddress === '127.0.0.1'){
            return callback(new Error('No connections from localhost allowed'));
        }
        return callback(); // Accept the connection
    },
    onError:function(err){
        console.log('Error onError %s', err.message);
    }
});

/*
        onError wasn't mentioned on the documentation i just wrote it myself 
        as to replace the error on listener :

          server.on('error', function(err){
             console.log('Error %s', err.message);
          }); 
        and i don't know if it would work or not.. i actually kept both of them      and just modified the log for i can know from where it would come.
*/

server.listen(465, onConnectCb);

在 onConnectCb 定义中(在上面的最后一行)我编写了所有 sn-p1 的代码和结果,现在我得到了以下日志:

[2015-11-02 15:33:11] INFO: Connection from ::ffff:127.0.0.1]
[2015-11-02 15:33:11] DEBUG: S: 220 Fekis-PC ESMTP
[2015-11-02 15:33:11] DEBUG: C: EHLO [127.0.0.1]
[2015-11-02 15:33:11] DEBUG: S: 250-OK: Nice to meet you [::ffff:127.0.0.1]
[2015-11-02 15:33:11] DEBUG: [mhJFwLqR43aX] C: STARTTLS
[2015-11-02 15:33:11] DEBUG: [mhJFwLqR43aX] S: 220 Ready to start TLS
[2015-11-02 15:33:11] INFO: [mhJFwLqR43aX] Connection upgraded to TLS
####  sendMail error : Error: self signed certificate
[2015-11-02 15:33:11] INFO: Connection closed to [::ffff:127.0.0.1]

即使电子邮件没有发送也更好,因为它仍然有错误

'自签名证书'

我想这是因为我仍然没有将密钥和证书设置为 SMTPServer 对象中的选项(因为我仍然不知道如何获取它们)。

var server = new SMTPServer({
    /*secure: true,
    key: fs.readFileSync('private.key'),
    cert: fs.readFileSync('server.crt')*/
});

所以我问这个问题是为了确认 Nodemailer 不能成为独立的 SMTP 服务器。

【问题讨论】:

    标签: node.js smtp nodemailer


    【解决方案1】:

    要修复自签名证书错误,请将以下内容添加到 smtpTransport 选项中,

    tls: {
       rejectUnauthorized: false
    },
    

    【讨论】:

      【解决方案2】:

      Nodemailer 本身不充当服务器,但它能够使用直接传输方法 https://github.com/andris9/nodemailer-direct-transport 发送邮件而无需中继 SMTP

      不建议在没有中继的情况下发送邮件,因为接收主机可能不够信任发送主机被拒绝或放入垃圾邮件文件夹的可能性要高得多

      【讨论】:

        猜你喜欢
        • 2016-09-07
        • 1970-01-01
        • 2021-08-16
        • 2012-08-24
        • 2019-10-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多