node.js - 无法使用expressjs和nodejs发送电子邮件

标签 node.js email express nodemailer

我必须使用expressjs和nodejs在网站的提交按钮上发送电子邮件,但我无法在post调用上发送电子邮件,因为我在控制台上收到以下错误。我正在使用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;

最佳答案

您应该使用 XOAuth2 token 连接到 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对于 Node 邮件程序 SMTP。这将有助于澄清

关于node.js - 无法使用expressjs和nodejs发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27601594/

相关文章:

c# - 如何在 Exact Target 中使用现有电子邮件模板发送电子邮件?

django - 禁用 gmail 中不太安全的应用程序设置后立即发送邮件进行测试

javascript - 语法错误: Unexpected token < : using angular 2 and webpack on heroku

javascript - 如何通过 Rxjs 中的简单缓冲来缓冲可观察值?

javascript - 如何在 Express 4.0 中发送 Flash 消息?

node.js - 如何连接solr和nodejs?

jquery - 电子邮件验证是否应该要求域部分包含一个点?

javascript - 错误 : Unknown authentication strategy "local" (Express & Passport)

node.js - express-validator 验证数组参数

node.js - 错误 : npm update