简体   繁体   中英

how to send Email using node js

i tried nodemailer but its not working. i want to send email to user through my node js website. can anyone please help me to get it done //code

var nodemailer = require('nodemailer');

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

var mailOptions = {
  from: 'youremail@gmail.com',
  to: 'myfriend@yahoo.com',
  subject: 'Sending Email using Node.js',
  text: 'That was easy!'
};

transporter.sendMail(mailOptions, function(error, info){
  if (error) {
    console.log(error);
  } else {
    console.log('Email sent: ' + info.response);
  }
});

In my experience sending emails can be made a lot easier by outsourcing the work to a provider like https://www.mailgun.com , the cost is $0.0008 per email. The reason for this is not only simplicity but deliverability. So many people have sent so many fake and junk emails over the years, emails often end up in spam folders when the server is not set up correctly. Things like adding SPF records to domains can obviously help but in all honesty, I now just use a sending provider.

Try this. The port is important to be 465 .

var transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'youremail@gmail.com',
    pass: 'yourpassword'
  },
  port: 465,
  secure: true,
  tls: {
    rejectUnauthorized: false
  }
});

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