The class library relies on

npm install nodemailer --save
Copy the code

The import

  const nodemailer = require("nodemailer");
Copy the code

The key point

  1. Configuration nodemailer createTransport parameter, specify the service address and port number, verify the account and password
  2. Configure mailOptions, from, to to specify the sender and destination, mail content, etc.

Writing code examples

"use strict"; const nodemailer = require("nodemailer"); const MailSettings = require(".. /config/MailSettings"); // async.. await is not allowed in global scope, must use a wrapper async function sendMailTo(mailUserName, mailContent){ // Generate test SMTP service account from ethereal.email // Only needed if you don't have a real mail account for testing let account = await MailSettings.createSenderAccount(); // create reusable transporter object using the default SMTP transport let transporter = nodemailer.createTransport({ host: "smtp.qiye.163.com", port: 994, secure: true, // true for 465, false for other ports auth: { user: account.user, // generated ethereal user pass: account.pass // generated ethereal password }, }); // setup email data with unicode symbols let mailOptions = { from: <[email protected]>', // sender address to: `${mailUserName}@aerozhonghuan.com`, // list of receivers subject: mailContent.subject, // Subject line // text: mailContent.text, // plain text body html: mailContent.html // html body }; // send mail with defined transport object let info = await transporter.sendMail(mailOptions); console.log("Message sent: %s", info.messageId); // Preview only available when sending through an Ethereal account console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info)); // Message sent: <[email protected]> // Preview URL: https://ethereal.email/message/WaQKMgKddxQDoou... } /* use DEMO let mail = {subject: "verification code ", // subject line HTML: "Your verification code is <B>123</B>", // plain text body} sendMail('zhangyunfei',mail). Catch (console.error); * /Copy the code

reference

nodemailer.com/about/