无法通过Node JS发送电子邮件,尝试了几种方法

时间:2018-10-30 07:22:47

标签: node.js smtp gmail nodemailer

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);
});

上面是我通过节点js发送电子邮件的代码,但我仍然遇到如下超时错误

  

{错误:连接ETIMEDOUT 74.125.24.109:587
      在TCPConnectWrap.afterConnect上[完成时](net.js:1191:14)
      errno:“ ETIMEDOUT”,
      代码:“ ECONNECTION”,
      syscall:“连接”,
      地址:“ 74.125.24.109”,
      端口:587,
      命令:“ CONN”}

对于以下链接,我也尝试了另一种基本方法:https://www.w3schools.com/nodejs/nodejs_email.asp,但效果不佳。
我已经在我的Google帐户上启用了安全性较低的应用。

除此之外,我还尝试了以下链接中的方法:https://jsfiddle.net/burawi/1u9m2mou/,但仍然无法正常工作,我生成了所有client_idclient_secretaccess_tokenrefresh_token

有人通过节点js发送电子邮件的最新指南或解决方案吗?

谢谢

2 个答案:

答案 0 :(得分:1)

这是网络限制问题。

请在您的移动网络中尝试相同的操作,它将起作用。

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

   let mailOptions = {
       from: "mrmanagementbbsr@gmail.com", // sender address
       to: 'nsubhadipta@gmail.com', // 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" });
       }
   });

});

答案 1 :(得分:0)

确保您打开{strong> 您的Less secure app access,否则您的电子邮件将不会通过。

然后您可能要尝试以下示例:

{
    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'});
}