nodejs smtp-server 电子邮件已发送但未收到

nodejs smtp-server Email gets sent but not received

const nodemailer = require('nodemailer');

const SMTPServer = require("smtp-server").SMTPServer;

const server = new SMTPServer({
    onAuth(auth, session, callback) {
        if (auth.username !== "test" || auth.password !== "password") {
            return callback(new Error("Invalid username or password"));
        }
        console.log(mailOptions.text);
        callback(null, {
            user: "test"

        }); // where 123 is the user id or similar property
    }
});

server.on("error", err => {
    console.log("Error %s", err.message);
});

server.listen(26);


var transporter = nodemailer.createTransport({
    host: "MYDOMAINNAME/IP",
    port: 26,
    secure: false,
    auth: {
        user: "test",
        pass: "password"
    },
    tls: {
        rejectUnauthorized: false
    }
});

var mailOptions = {
    from: '"MYSITENAME"<info@MYDOMAIN.com>',
    to: 'ADRESS@TLD.com',
    subject: 'Sending Email using Node.js',
    text: 'That was easy!'
};

transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
        console.log("sendmail" + error);
    } else {
        console.log('Email sent: ' + info.response);
    }
});

输出为: 那很简单! 电子邮件已发送:250 成功:邮件已排队

我只想将带有输入变量的邮件作为文本从我的域名发送到普通的 gmail 地址; 邮件没有被 gmail/ 或任何其他地址收到;检查垃圾邮件文件夹;

我认为您可能只是 排队 您的 smtp-server 实例中的电子邮件,而不是 发送 它。

documentation for smtp-server 以注释开头,说明此模块实际上如何不自行发送电子邮件:

This module does not make any email deliveries by itself. smtp-server allows you to listen on ports 25/24/465/587 etc. using SMTP or LMTP protocol and that’s it. Your own application is responsible of accepting and delivering the message to destination.

您需要将 SMTP client module smtp-connection 与您的 SMTP 服务器结合使用才能发送电子邮件。

您可以在服务器模块代码的 this line 处查看“250 OK:消息已排队”响应的来源。

对于遇到同样问题但使用 Nodemailer 的 transporter 方式的人,我解决了这个问题,将服务器的 name 添加到传输对象中。


    const transportObj = {
      name: credentials.host, // in my case, the host and the name are the same
      host: credentials.host,
      port: credentials.port,
      secure: true,
      auth: {
          user: credentials.user,
          pass: credentials.pass
      },
}

const transporter = nodemailer.createTransport(transportObj)

transporter.sendMail(...)

这个解决方案是相关的here

我的要求是不使用第三方电子邮件提供商来发送和接收来自已知客户的电子邮件。创建一个名为 send 的 node-express 路由,

var express = require('express');
var server = express();

server.use(express.json());
server.use(express.urlencoded());
server.use(cookieParser());

server.post('/send', function(req, res){
    try {
        var message = {
            from: '<' + req.body.from + '>',
            to: '<' + req.body.to + '>',
            subject: req.body.subject,
            text: req.body.message,
            html: req.body.html
        };

        var email = req.body.to;

        const dns = require('dns');
        const domain = email.split('@')[1]; 
        // lookup for any MX domains for the recipient?
        dns.resolve(domain, 'MX', function(err, addresses) {
            if (err) {
                res.on({status: 'error', message: 'No MX record exists, so email is invalid.'});
            } else if (addresses && addresses.length > 0) {      
                // use the first MX domain in the list
                const toExchange = addresses[0].exchange;

                const nodemailer = require('nodemailer');
                const transporter = nodemailer.createTransport({
                    port: 25,
                    host: toExchange,
                    logger: true,
                    debug: true,
                });

               
                transporter.sendMail(message, function(err, info){
                    if (err) {
                        res.json({status: 'error', message: err});
                    } else {
                        res.json({status: 'success', info: info});
                    }
                });
            }
        });
    } catch(error) {
        res.json({status: 'error', message: error});
    }
});

要接受传入的电子邮件,请使用 node-mailin。您需要为接收邮件服务器配置 DNS。 https://www.youtube.com/watch?v=o66UFsodUYo, the rDNS (reverse DNS) configuration can be resolved by setting the banner on the SMTP Server options as your public host name, which can be identified at https://www.whoismyisp.org/

有不错的 YouTube 视频