simpleSMTP用户身份验证

时间:2013-09-17 22:00:40

标签: node.js mail-server

我正在尝试使用nodejs构建一个SMTP服务器并坚持用户身份验证。如何创建一个函数来使用user:foo和pass:bar?

授权用户
var simplesmtp = require("simplesmtp"),
    fs = require("fs");

var options = {
    requireAuthentication: true,
    debug: true
};

var smtp = simplesmtp.createServer(options);
smtp.listen(9845);

smtp.on("authorizeUser", function (connection, username, password, callback) {
    callback(new Error("Auth fail!"), true);

});

smtp.on("startData", function(connection){
    console.log("Message from:", connection.from);
    console.log("Message to:", connection.to);
    connection.saveStream = fs.createWriteStream("message.txt");
});

smtp.on("data", function(connection, chunk){
    connection.saveStream.write(chunk);
});

smtp.on("dataReady", function(connection, callback){
    connection.saveStream.end();
    console.log("Incoming message saved to message.txt");
    callback(null, "ABC1"); // ABC1 is the queue id to be advertised to the client
    //callback(new Error("Rejected as spam!")); // reported back to the client
});

1 个答案:

答案 0 :(得分:0)

你尝试过这样的事吗?

smtp.on("authorizeUser", function (connection, username, password, callback) {
    if (username == "foo" && password == "bar") {
        callback(null, true);
    } else {
        callback(new Error("Auth fail!"), false);
    }
});