带有我的SMTP服务器的Nodemailer仅使电子邮件排队,而不发送

时间:2019-07-06 09:58:03

标签: javascript node.js smtp nodemailer

我正在使用Nodemailer通过我的节点SMTP服务器将邮件发送到客户端。但是,在发送邮件时会显示:“确定:邮件已排队”,但邮件尚未到达。但是当我使用例如gmail SMTP,它可以正常工作。但是我必须使用SMTP服务器发送邮件。

SMTP服务器代码:

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

var SMTPemailServer = new SMTPServer({
    onAuth(auth, session, callback) {
        if (auth.username !== "abc" || auth.password !== "def") {
            return callback(new Error("Invalid username or password"));
        }
        console.log(auth);
        callback(null, { user: 123 });
    },
    disabledCommands: ['STARTTLS'],
    logger: true,
    onData(stream, session, callback){
        stream.pipe(process.stdout); // print message to console
        stream.on('end', callback);
    }
});

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

SMTPemailServer.listen(587, function () {
    console.log("SMTP-Server listening on *:587");
});

以及使用Nodemailer发送邮件的代码:

var transporter = nodemailer.createTransport({
    host: "my.smtpserver.domain",
    port: 587,
    auth: {
        user: "abc",
        pass: "def"
    }
});

let mailOptions = {
    from: 'sender@email.adress', // sender address
    to: "client@email.adress", // list of receivers
    subject: "TEST", // Subject line
    text: "TEST", // plain text body
    html: "<h2>TEST</h2>" // html body
};

// send mail with defined transport object
let info = transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
        console.log("error" + error);
        res.send(error);
    } else {
        console.log("Email sent: " + info.response);
        res.send(info);
    }
});

0 个答案:

没有答案