Node. js USES nodemailer to send mail instances

  • 2020-03-30 02:17:56
  • OfStack

Install nodemailer

npm install nodemailer --save

Second, the call
var nodemailer = require("nodemailer");
//Open an SMTP connection pool
var smtpTransport = nodemailer.createTransport("SMTP",{
  host: "smtp.qq.com", //  The host 
  secureConnection: true, //Using SSL
  port: 465, //SMTP port
  auth: {
    user: "xxxxxxxx@qq.com", //  account 
    pass: "xxxxxxxx" //  password 
  }
});
//Set email content
var mailOptions = {
  from: "Fred Foo <xxxxxxxx@qq.com>", //Send an address
  to: "2838890xx@qq.com, minimixx@126.com", //Receipt list
  subject: "Hello world", //  The title 
  html: "<b>thanks a for visiting!</b>  Hello, world! " //HTML content
}
//Send E-mail
smtpTransport.sendMail(mailOptions, function(error, response){
  if(error){
    console.log(error);
  }else{
    console.log("Message sent: " + response.message);
  }
  smtpTransport.close(); //If not, close the connection pool
});

Common mistakes

{ [AuthError: Invalid login - 454 Authentication failed, please open smtp flag first!]
  name: 'AuthError',
  data: '454 Authentication failed, please open smtp flag first!',
  stage: 'auth' }

Error cause: the service was not set on the account
Solution: QQ mailbox -> Settings - > Account - > Open service: POP3/SMTP service


{ [SenderError: Mail from command failed - 501 mail from address must be same as authorization user]
  name: 'SenderError',
  data: '501 mail from address must be same as authorization user',
  stage: 'mail' }

Error reason: the sending account is different from the authenticated account


Related articles: