最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - emails are not sending using nodemailer - Stack Overflow

programmeradmin1浏览0评论

I am using smtp.emailcloud.smartping.io as my SMTP service provider. The emails are sent on my localhost successfully. But not sending on my server getting below error :

SendMail error: ==>> Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) {
   errno: -104,
   code: 'ESOCKET',
   syscall: 'read',
   command: 'CONN'
}

my code is as below :

const transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST,
    port: process.env.SMTP_PORT,
    secure: false,
    auth: {
        user: process.env.SMTP_USER,
        pass: process.env.SMTP_PASS
    },
    tls: {
        rejectUnauthorized: false
    },
    dnsTimeout: 180000, //3 minutes timeout
    debug: true,
    logger: true,
})

const sendEmail = async (emailData) => {
    try {
        const { senderEmail, sendTo, subject, emailBody, attachmentName } = emailData;

        const attachmentPath = path.join(process.cwd(), 'uploads/attachments', attachmentName);

        const mailOptions = {
            from: senderEmail,
            to: sendTo,
            subject: subject,
            html: emailBody,
            attachments: attachmentName !== 'null'
                ? [
                    {
                        filename: attachmentName,
                        path: attachmentPath
                    }
                ]
                : []
        }
        if (emailData) {
            mailOptions['cc'] = emailData;
        }
        console.log('mailOptions', mailOptions);
        // transporter.sendMail(mailOptions)
        transporter.sendMail(mailOptions)
            .then(info => console.log('Email sent: ==>>', info))
            .catch(error => console.error('SendMail error: ==>>', error));
    } catch (error) {
        console.log('error -->>', error);
        Sentry.captureMessage("Something went wrong in sendEmail util function");
        Sentry.captureException(error);
    }
};

I have tried below changes in config :

const transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST,
    port: 465,
    secure: true,
    auth: {
        user: process.env.SMTP_USER,
        pass: process.env.SMTP_PASS
    },
    tls: {
        rejectUnauthorized: false
    },
    dnsTimeout: 180000, //3 minutes timeout
    debug: true,
    logger: true,
})

I have tried several solutions from google but did not work.

It would be greate if you help me to resolve it.

Thank you

I am using smtp.emailcloud.smartping.io as my SMTP service provider. The emails are sent on my localhost successfully. But not sending on my server getting below error :

SendMail error: ==>> Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) {
   errno: -104,
   code: 'ESOCKET',
   syscall: 'read',
   command: 'CONN'
}

my code is as below :

const transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST,
    port: process.env.SMTP_PORT,
    secure: false,
    auth: {
        user: process.env.SMTP_USER,
        pass: process.env.SMTP_PASS
    },
    tls: {
        rejectUnauthorized: false
    },
    dnsTimeout: 180000, //3 minutes timeout
    debug: true,
    logger: true,
})

const sendEmail = async (emailData) => {
    try {
        const { senderEmail, sendTo, subject, emailBody, attachmentName } = emailData;

        const attachmentPath = path.join(process.cwd(), 'uploads/attachments', attachmentName);

        const mailOptions = {
            from: senderEmail,
            to: sendTo,
            subject: subject,
            html: emailBody,
            attachments: attachmentName !== 'null'
                ? [
                    {
                        filename: attachmentName,
                        path: attachmentPath
                    }
                ]
                : []
        }
        if (emailData) {
            mailOptions['cc'] = emailData;
        }
        console.log('mailOptions', mailOptions);
        // transporter.sendMail(mailOptions)
        transporter.sendMail(mailOptions)
            .then(info => console.log('Email sent: ==>>', info))
            .catch(error => console.error('SendMail error: ==>>', error));
    } catch (error) {
        console.log('error -->>', error);
        Sentry.captureMessage("Something went wrong in sendEmail util function");
        Sentry.captureException(error);
    }
};

I have tried below changes in config :

const transporter = nodemailer.createTransport({
    host: process.env.SMTP_HOST,
    port: 465,
    secure: true,
    auth: {
        user: process.env.SMTP_USER,
        pass: process.env.SMTP_PASS
    },
    tls: {
        rejectUnauthorized: false
    },
    dnsTimeout: 180000, //3 minutes timeout
    debug: true,
    logger: true,
})

I have tried several solutions from google but did not work.

It would be greate if you help me to resolve it.

Thank you

Share Improve this question edited Nov 20, 2024 at 6:20 JIGNESH PATEL asked Nov 19, 2024 at 12:49 JIGNESH PATELJIGNESH PATEL 3975 silver badges20 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

Can you check the sever network configuration and firewall configuration please? Before I had a similar problem with NodeJS server but after fixing firewall configuration using ufw or firewalld, it was fine.

After trying many solutions from Google and StackOverflow. I came to know that my code is correct the issue was with SMTP credentials. I have used other SMTP credentials and it worked.

发布评论

评论列表(0)

  1. 暂无评论