Nodemailer:密码字符串包含“#”时出现连接超时错误

时间:2018-10-26 08:20:33

标签: javascript node.js email nodemailer

我在nodejs中有一个应用程序,它使用nodemailer发送电子邮件。一切正常,除非密码字符串中包含“#”。

下面是我的代码

var secure = config.secure ? "smtps":"smtp";
var conURL = secure+"://"+config.user+":"+config.pass+"@"+config.host+"/?pool=false";

try {
    const transport = nodemailer.createTransport(conURL);

    // verify connection configuration
    transport.verify(function(error, success) {
    if (error) {
        console.log(error.message) // Error : Connection Timeout
    }
});

当密码包含“#”字符时,出现连接超时错误。我已经用@,!,$等进行了测试,一切正常。

有人可以帮助解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

您可能必须对URI进行编码

所以,这样的事情应该可以实现我想像的

secure+"://"+config.user+":"+encodeURIComponent(config.pass)+"@"+config.host+"/?pool=false"

编辑:尝试使用配置对象而不是像这样的URI

const config = {
    pool: false,
    host: config.host,
    secure: config.secure,
    auth: {
        user: config.user,
        pass: config.pass
    }
};

try {
    const transport = nodemailer.createTransport(conURL);

    // verify connection configuration
    transport.verify(function(error, success) {
    if (error) {
        console.log(error.message) // Error : Connection Timeout
    }
});