This article mainly introduced the Node.js use Nodemailer to send the mail the method, the example uses is the QQ mailbox, you may also modify into other mailbox like 163, Gmail and so on, needs the friend to be possible to refer to under
First, install Nodemailer code as follows: NPM install Nodemailer--save Two, the calling code is as follows: var Nodemailer = require ("Nodemailer"); //Open an SMTP connection pool var smtptransport = Nodemailer.createtransport ("SMTP", { Host: "Smtp.qq.com",//host S Ecureconnection:true,//using SSL port:465,//SMTP Port Auth: { User: "xxxxxxxx@qq.com",//account number Pass: "xxxxxxxx"//Password }}); //Set message content var mailoptions = { from: "Fred Foo <xxxxxxxx@qq.com>",//Send address to: "2838890xx@qq.com , minimixx@126.com ",//Inbox list Subject:" Hello World ",//title HTML:" <b>thanks A for visiting!</b> Hello, world! "//HTML content} //Send mail Smtptransport.sendmail (mailoptions, function (Error, response) { if (error) { &NBS P Console.log (Error); }else{ Console.log ("message Sent:" + response.message); } Smtptransport.close (); If it's not working, close the Connection pool}); Three, common error code is as follows: {[Autherror:invalid login-454Authentication failed, please open SMTP flag first!] Name: ' Autherror ', data: ' 454 authentication failed, please open SMTP flag first! ', stage: ' Auth '} & nbsp Error reason: Account does not set the service solution: QQ mailbox-> set-> account-> Open service: POP3/SMTP service Code as follows: {[Sendererror:mail from command failed- 501 mail from address must to same as authorization user] name: ' Sendererror ', data: ' 501 mail from address Must be same as authorization user ', stage: ' Mail '} error Reason: The sender account and the authentication account is different