Nodemailer - MS Exchaneg server - Error unable to verify the first certificate

主宰稳场 提交于 2019-12-10 10:11:00

问题


I am trying to send email from NodeJS using out office MS Exchange Mail server. with below code. And get error

Our Admin said no certificates are needed.

Error:-

$ node test2.js
Error :  { Error: unable to verify the first certificate
    at TLSSocket.onConnectSecure (_tls_wrap.js:1048:34)
    at TLSSocket.emit (events.js:182:13)
    at TLSSocket._finishInit (_tls_wrap.js:628:8) code: 'ESOCKET', command: 'CONN' }

NodeJS Code:-

"use strict";
const nodemailer = require("nodemailer");

async function main() {
    try {
        // create reusable transporter object using the default SMTP transport
        let transporter = nodemailer.createTransport({
            host: 'host',
            port: 25,
            secure : false, // true for 465, false for other ports
            auth: {
                user: 'user',
                pass: 'password'
            }
        });

        // setup email data
        let mailOptions = {
            from: 'me@email.com',
            to: 'me@email.com',
            subject: 'Hey you, awesome!',
            html: '<b>This is bold text</b>',
            text: 'This is text version!'
        };

        // send mail with defined transport object
        let info = await transporter.sendMail(mailOptions)
        console.log("Message sent: %s", JSON.stringify(info));

    } catch (error) {
        console.log('Error : ', error);
    }
}

main(); // For testing

回答1:


The below code change fixed the issue. Added this to the createTransport()

 tls: {rejectUnauthorized: false}

Code:-

   // create reusable transporter object using the default SMTP transport
    let transporter = nodemailer.createTransport({
        host: 'host',
        port: 25,
        secure : false, // true for 465, false for other ports
        auth: {
            user: 'user',
            pass: 'password'
        },
        tls: {
            // do not fail on invalid certs
            rejectUnauthorized: false
        },
    });


来源:https://stackoverflow.com/questions/55167741/nodemailer-ms-exchaneg-server-error-unable-to-verify-the-first-certificate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!