使用nodemailer发送电子邮件

时间:2014-07-26 18:03:20

标签: javascript node.js nodemailer

我正在尝试使用nodemailer发送一些电子邮件,通过这段代码放入我的server.js

server.js

var smtpTransport = mailer.createTransport("SMTP", {
    service: "gmail",
    auth: {
        user: "abc@gmail.com",
        pass: "pass"
    }
});

var mail = {
    from: "Contact <abc@gmail.com>",
    to: "abc@gmail.com",
    Subject: "Test sujet",
    text: "test text",
    html: "<b>Test text</b>"
}

smtpTransport.sendMail(mail, function(err, res){
    console.log(err, "Message sent: ");
    smtpTransport.close();
})

但是,当我将该server.js节点化时,我收到此错误消息:

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

编辑:引用to this,我也尝试检查我的ssl连接openssl s_client -connect smtp.gmail.com:465(它正在运行)。

我该如何解决它?

2 个答案:

答案 0 :(得分:4)

我收到了同样的错误,并按照nfreeze在评论中发送的链接。对于您的代码,它看起来像这样。我希望这有帮助!

  var nodemailer = require('nodemailer');
  var smtpTransport = require('nodemailer-smtp-transport');

  var transporter = nodemailer.createTransport(smtpTransport({
  service: 'Gmail',
  auth: { user: 'abc@gmail.com',
        pass: 'pass' }
  }));

  transporter.sendMail({
    from: 'Contact <abc@gmail.com>',
    to: "abc@gmail.com",
    subject: 'Test sujet',
    text: "test text",
    html: "<b>Test text</b>"
  }, function (error, response) {
    //Email not sent
    if (error) {
      res.end("Email send Falied");
    }
    //email send sucessfully
    else {
      console.log(response);
    }
  });

答案 1 :(得分:-4)

你可以删除&#34; SMTP&#34;选项。