preface

Today, we will develop a small effect for you. The length is short, mainly to show you the effect.

In actual combat

  1. First we initialize a Node project

    npm init -y
    Copy the code
  2. Create an app.js file

    'use strict';
    
    const nodemailer = require('nodemailer');
    
    let transporter = nodemailer.createTransport({
      // host: 'smtp.ethereal.email',
      service: 'qq'./ / use the built-in email transmission check the support list: https://nodemailer.com/smtp/well-known/
      port: 465./ / SMTP port
      secureConnection: true.// SSL is used
      auth: {
        user: '[email protected]'.// Use QQ mailbox as an example
        pass: 'kzudmlwerknibejc'.// SMTP authorization code}});let mailOptions = {
      from: '"xxx" <[email protected]>'.// sender
      to: '[email protected]'.// receivers
      subject: 'Hello'.// Subject line
      // Send text or HTML
      // text: 'Hello world? ', // plain text body
      html: 'Hello world? ' // html body
    };
    
    // send mail with defined transport object
    transporter.sendMail(mailOptions, (error, info) = > {
      if (error) {
        return console.log(error);
      }
      console.log('Message sent: %s', info.messageId);
      // Message sent: <[email protected]>
    });
    Copy the code

    Nodemailer is an easy-to-use Node.js mail sending component.

    Nodemailer.com

    GitHub address: github.com/nodemailer/…

    Nodemailer’s key features include:

    • Support Unicode encoding

    • Windows operating system is supported

    • Support HTML content and plain text content

    • Supporting attachments (sending large attachments)

    • Support for embedding images in HTML content

    • Supports SSL/STARTTLS secure email sending

    • Support for built-in transport methods and transport methods implemented by other plug-ins

    • Support for custom plug-ins to handle messages

    • XOAUTH2 login authentication is supported

  3. Obtain the SMTP authorization code

    Login QQ mailbox, click the mailbox Settings.

After the IMAP/SMTP service is enabled, you can obtain the authorization code randomly.

  1. Start the project

    node app.js
    Copy the code

To optimize the

  1. You can use node-schedule to create scheduled tasks

    npm install node-schedule
    Copy the code

    Unit time can execute code to send mail.

conclusion

Thanks for reading and I hope I didn’t waste your time.