简体   繁体   中英

Sending emails using nodemailer

I'm trying to send some emails using nodemailer via this snippet of code putted into my 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();
})

But, when I node that server.js I get this error message:

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

EDIT : Refering to this , I've also tried to check my ssl connection openssl s_client -connect smtp.gmail.com:465 (it's working).

How do I please to fix it ?

I got the same error and followed the link that nfreeze sent in the comment. For your code, it would look something like this. I hope this helps!

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

您可以删除“ SMTP”选项。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM