Home > Article > Web Front-end > node.js uses nodemailer to send emails example_javascript skills
1. Install nodemailer
//Open an SMTP connection pool
var smtpTransport = nodemailer.createTransport("SMTP",{
host: "smtp.qq.com", // Host
secureConnection: true, // Use SSL
port: 465, // SMTP port
auth: {
user: "xxxxxxxx@qq.com", // account number
pass: "xxxxxxxx" // password
}
});
//Set email content
var mailOptions = {
from: "Fred Foo
to: "2838890xx@qq.com, minimixx@126.com", // Recipient list
subject: "Hello world", // Title
html: "thanks a for visiting! Hello world! " // html content
}
//Send mail
smtpTransport.sendMail(mailOptions, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " response.message);
}
smtpTransport.close(); // If useless, close the connection pool
});
Cause of error: The service is not set up in the account
Solution: QQ Mail-> Settings-> Account-> Enable service: POP3/SMTP service