Nodemailer 可以作为独立的 SMTP 服务器吗?

Can Nodemailer be a standalone SMTP server?

我读到 Nodemailer 是 using SMTP or sendmail or Amazon SES and i also discovered more smtp servers 所以我假设除了我的 nodemailer 应用程序之外我必须制作其中一个 SMTP 服务器 运行 并让我的应用程序连接到它但是有人告诉我 Nodemailer 本身可以是一个自治的(或者可能是独立的)SMTP 服务器 "Nodemailer is autonomous to send mails (it is not necessary to go through a server / SMTP service)".

所以我再次阅读了它的文档,我发现 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 发送电子邮件:(让我们称之为 snippet1)

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 error : Error: connect ECONNREFUSED 127.0.0.1:25

所以我决定使用此代码在同一个应用程序中使用 smtp-server :

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 定义中(在上面的最后一行中)我写了所有 snippet1 的代码和结果,现在我得到了以下日志:

[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]

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

'self signed certificate'

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

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

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

Nodemailer 本身不充当服务器,但它可以使用直接传输方法在没有中继 SMTP 的情况下发送邮件 https://github.com/andris9/nodemailer-direct-transport

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

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

tls: {
   rejectUnauthorized: false
},