无法使用expressjs和nodejs发送电子邮件

时间:2014-12-22 11:15:21

标签: node.js email express nodemailer

我必须使用expressjs和nodejs在网站上的提交按钮发送电子邮件,但我无法在发布电话上发送电子邮件,因为我在控制台上收到以下错误。我正在使用nodemailer在此处发送电子邮件。

错误: -

 [Error: connect ECONNREFUSED]
 code: 'ECONNREFUSED',
 errno: 'ECONNREFUSED',
 syscall: 'connect' }

以下是代码: -

var nodemailer = require('nodemailer');
var ctrl={
    post:function(req,res)
    {
        console.log('If this function returns promise it would be handled accordingly.'+req.body.Name);
                // create reusable transporter object using SMTP transport
        var transporter = nodemailer.createTransport({
        service: 'smtp.gmail.com',
        auth: {
        user: 'example@e20.in',
        pass: 'password'
        },

    });

// setup e-mail data with unicode symbols
    var mailOptions = {
        from: 'example@e20.in', // sender address
        to: 'example@e20.in', // list of receivers
        subject: 'Hello', // Subject line
        text: 'Hello world', // plaintext body
        html: '<b>Hello world</b>', // html body
        replyTo:req.body.Email
    };

// send mail with defined transport object
    transporter.sendMail(mailOptions, function(error, info){
     if(error){
        console.log(error);
     }else{
        console.log('Message sent: ' + info.response);
     }
    });
    return {Name:req.body.Name}; //req.body form post
        //return {Name:req.query.Name}; //req.query url querystring
    },
    put:function(req,res){
        return {};
    }
}

module.exports = ctrl;

1 个答案:

答案 0 :(得分:0)

您应该使用XOAuth2令牌连接到Gmail。不用担心,Nodemailer已经知道了这一点:

var smtpTransport = nodemailer.createTransport('SMTP', {
  service: 'Gmail',
  auth: {
    XOAuth2: {
      user: smtpConfig.user,
      clientId: smtpConfig.client_id,
      clientSecret: smtpConfig.client_secret,
      refreshToken: smtpConfig.refresh_token,
      accessToken: smtpConfig.access_token,
      timeout: smtpConfig.access_timeout - Date.now()
   }
  }
};

检查此link是否为nodemailer SMTP。这将有助于澄清