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

I am using smtp.emailcloud.smartping.io as my SMTP service provider.The emails are sent on my localhos

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745561184a4633114.html

相关推荐

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

    I am using smtp.emailcloud.smartping.io as my SMTP service provider.The emails are sent on my localhos

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信