0

I'm trying to send gmail using nodemailer. Here is my code.

var transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: '<sender@gmail.com>',
            pass: '<password>'
        }
    });

    var mailOptions = {
        from: '<sender@gmail.com>',
        to: '<receiver@gmail.com>',
        subject: 'Sending Email using Node.js',
        text: 'That was easy!'
    };

    transporter.sendMail(mailOptions, function(error, info){
        if (error) {
            console.log(error);
        } else {
            console.log('Email sent: ' + info.response);
        }
    });

But I always got this error.

{ Error: Connection timeout
    at SMTPConnection._formatError (E:\Work\Web\GatherSense\gather-sense-backend\node_modules\nodemailer\lib\smtp-connection\index.js:771:19)
    at SMTPConnection._onError (E:\Work\Web\GatherSense\gather-sense-backend\node_modules\nodemailer\lib\smtp-connection\index.js:757:20)
    at Timeout._connectionTimeout.setTimeout (E:\Work\Web\GatherSense\gather-sense-backend\node_modules\nodemailer\lib\smtp-connection\index.js:229:22)
    at ontimeout (timers.js:436:11)
    at tryOnTimeout (timers.js:300:5)
    at listOnTimeout (timers.js:263:5)
    at Timer.processTimers (timers.js:223:10) code: 'ETIMEDOUT', command: 'CONN' }

I've spent much time and tried in many ways such as allowing secure less apps and turning off firewalls. But always get the same error. Please help me and thank you.

Andrew Terex
  • 341
  • 2
  • 4
  • 10

1 Answers1

0
const nodemailer = require('nodemailer');
const smtpTransport = require('nodemailer-smtp-transport');
import config from '../config/environment';


const transporter = nodemailer.createTransport(smtpTransport({
    host: config.email_config.SMTP_HOST,
    port: config.email_config.SMTP_PORT,
    tls: {
        rejectUnauthorized: false
    },
    auth: {
        user: config.email_config.SMTP_AUTH_USER,
        pass: config.email_config.SMTP_AUTH_PASS
    }
}));

export function mail(emailObj) {
    return new Promise((resolve, reject) => {
        transporter.sendMail(emailObj)
            .then(success => {
                if (success) {
                    console.log("Email success:::", success);
                    resolve();
                } else {
                    resolve(null);
                }
            }).catch(err => {
                console.log("Email Error:::", err);
                reject(err)
            })
    })
}
vicky
  • 415
  • 2
  • 10