GT Test
GT Test

Reputation: 31

Unable to send email through node js, tried few methods

var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport({
    host: "smtp.gmail.com",
    secureConnection: false,
    port: 587,
    tls: {
        ciphers: 'SSLv3'
    },
    requireTLS: true,
    auth: {
        user: 'mygmail',
        pass: 'mypass'
    }
});

var mailOptions = {
    from: 'mygmail',
    to: 'receiver gmail',
    subject: 'Sending Email using Nodemailer',
    text: 'That was easy!'
};

transporter.sendMail(mailOptions, (error, info) => {
    if (error)
        return console.log(error);

    console.log('Email sent: ' + info.response);
});

Above is my code for sending email through node js but I keep encounter a timeout error as below

{ Error: connect ETIMEDOUT 74.125.24.109:587
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1191:14)
errno: 'ETIMEDOUT',
code: 'ECONNECTION',
syscall: 'connect',
address: '74.125.24.109',
port: 587,
command: 'CONN' }

I also tried another basic way as for the following link: https://www.w3schools.com/nodejs/nodejs_email.asp but it is not working as well.
I've already turned on allow the less secure app on my google account.

Beside of that, I also tried the method like the one in the following the link: https://jsfiddle.net/burawi/1u9m2mou/ and it is still unable to work, I generated all the client_id, client_secret, access_token, and refresh_token.

Does anyone have any latest guide or solution for sending email through node js?

Thanks

Upvotes: 3

Views: 2851

Answers (2)

nsubhadipta
nsubhadipta

Reputation: 49

This is a network restriction issue.

Please try the same thing in your mobile network and it will work.

router.get('/mailTest', (req, res) => {
   let transporter = nodemailer.createTransport({
       service: 'gmail',  
       auth: {
           user: "[email protected]", // generated ethereal user
           pass: "Test@123" // generated ethereal password
       }
   });

   let mailOptions = {
       from: "[email protected]", // sender address
       to: '[email protected]', // list of receivers
       subject: "test subject", // Subject line
       text: 'demo text'
   };
   // send mail with defined transport object
   transporter.sendMail(mailOptions, (error, info) => {
       if (error) {
           res.json({ status: -1, message: 'Error Occured', error: error });
       }
       else {
           res.json({ status: 1, message: "Email Sent" });
       }
   });

});

Upvotes: 2

antzshrek
antzshrek

Reputation: 9953

Ensure you turn on your Less secure app access, else your emails will not go through.

Then you might want to try this example:

{
    let transporter = nodemailer.createTransport({
        service: 'gmail',
        auth: {
            user: 'mymail',
            pass: 'mypass'
        }
    });

    let mailOptions = {
        from: 'mygmail',
        to: 'receiver gmail',
        subject: 'Sending Email using Nodemailer',
        text: 'That was easy!'
    };

        transporter.sendMail(mailOptions, (error, info)=>{
            if (error) {
                console.log(error);
            } else {
                console.log('Email sent ' + info.response');
            }
        }); 
    res.json({success: 'whatever message you plan on writing'});
}

Upvotes: 0

Related Questions