1. Install nodemailer
Copy the code as follows: npm install nodemailer --save
2. Call
The code copy is as follows: var nodemailer = require("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: "[email protected]", // Account
pass: "xxxxxxxx" // Password
}
});
// Set the email content
var mailOptions = {
from: "Fred Foo <[email protected]>", // Send address
to: "[email protected], [email protected]", // Received 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){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
smtpTransport.close(); // If it is not useful, close the connection pool
});
3. Common errors
The code copy is as follows:
{ [AuthError: Invalid login - 454 Authentication failed, please open smtp flag first!]
name: 'AuthError',
data: '454 Authentication failed, please open smtp flag first!',
stage: 'auth' }
Cause of error: The service is not set by the account
Solution: QQ Email->Settings->Account->Open Service: POP3/SMTP Service
The code copy is as follows:
{ [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' }
Reason for error: The sending account is different from the authenticated account