Sending mail in node.js using nodemailer

旧城冷巷雨未停 提交于 2019-12-06 02:55:58

问题


I am trying to send mail in node.js using Nodemailer but it shows some error like { [Error: self signed certificate in certificate chain] code: 'ECONNECTION', command: 'CONN' }

My node.js code is

var express    =    require('express');
var app        =    express();
var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport('smtps://something%40gmail.com:password@smtp.gmail.com');

var mailOptions = {
  to: 'stevecameron2016@gmail.com',
  subject: 'Hello ?', 
  text: 'Hello world ??', 
  html: '<b>Hello world ??</b>' 
};

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

var server     =    app.listen(8900,function(){
  console.log("We have started our server on port 8900");
});

回答1:


To allow to send an email via “less secure apps”, go to the link and choose “Turn on”.

(More info about less secure apps)

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

var mailAccountUser = '<YOUR_ACCOUNT_USER>'
var mailAccountPassword = '<YOUR_ACCOUNT_PASS>'

var fromEmailAddress = '<FROM_EMAIL>'
var toEmailAddress = 'TO_EMAIL'

var transport = nodemailer.createTransport(smtpTransport({
    service: 'gmail',
    auth: {
        user: mailAccountUser,
        pass: mailAccountPassword
    }
}))

var mail = {
    from: fromEmailAddress,
    to: toEmailAddress,
    subject: "hello world!",
    text: "Hello!",
    html: "<b>Hello!</b><p><a href=\"http://www.yahoo.com\">Click Here</a></p>"
}

transport.sendMail(mail, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + response.message);
    }

    transport.close();
});



回答2:


try https://github.com/nodemailer/nodemailer/issues/406

add tls: { rejectUnauthorized: false } to your transporter constructor options

p.s It's not a good idea to post your mail server address, if it's a real one




回答3:


i was in this trouble too, what i did is next code line:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

just before creating the smpttransport

for example, in your code just put this:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; var transporter = nodemailer.createTransport('smtps://something%40gmail.com:password@smtp.gmail.com');




回答4:


what @user3985565 said is correct. However , if you are using gmail you also need to change some settings in your gmail account. More specifically you need to "allow less secure apps" in you gmail account. To do that just follow these steps:

  1. test nodemailer as it is
  2. node will throw an error and gmail will send you a security alert email informing you that "an unsafe app tried to access your account"
  3. in this email you need to click on "check activity" and then, in the folowing screen, you must unswer "yes"
  4. Your next clicks in the following screens are "more information" and then "less secure apps".
  5. finally you will see a toggle switch and you must turn it on.


来源:https://stackoverflow.com/questions/38431592/sending-mail-in-node-js-using-nodemailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!