nodemailer发邮件失败 unable to verify the first certificate
发布于 6 年前 作者 ugrg 5680 次浏览 来自 分享

快过年了,要做个监控,需要用到发邮件的功能,下载了nodemailer,一路照着DEMO来,结果出现了个诡异的异常.

{ Error: unable to verify the first certificate
    at Error (native)
    at TLSSocket.<anonymous> (_tls_wrap.js:1092:38)
    at emitNone (events.js:86:13)
    at TLSSocket.emit (events.js:185:7)
    at TLSSocket._finishInit (_tls_wrap.js:610:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:440:38) code: 'ECONNECTION', command: 'CONN' }

查了半天,网上说各种的都有,就是没这个错的描述,没办法翻墙去问Google,查到了一个,说是因为TLS的问题,不是nodemailer的锅,那么TLS出了什么问题?

回查了一下TLS的说明.找到了这么一条.

rejectUnauthorized <boolean> If not false the server will reject any connection which is not authorized with the list of supplied CAs. This option only has an effect if requestCert is true. Defaults to true.

也就是说,如果我的邮件服务器的证书不在CA里,就会被拒绝,那么好了,看样子是要把rejectUnauthorized设置成false, 再回头查nodemailer的说明,还好创建链接时,有个TLS的配置节点,所以配置改成:

let transporter = nodemailer.createTransport({
    host: 'mail.xxxxx.com',
    port:465,
    secure: true,
    auth: {
        user: "xxxx@xxxx.com",
        pass: "***********"
    },
    tls:{rejectUnauthorized: false}
});

问题解决,哈哈,这个年终于可以安心的过了.

2 回复

踩过这个坑,在stackflow好像有大神解释过原因的。。

@Yangk030208 说真的,baidu了好半天,就是没找到一条靠谱的说明。最络只能翻墙问google,哎!

回到顶部