Using nodemailer

// Use nodejs to automatically send mail, Using nodemailer HTTP: / / https://nodemailer.com/about/ / / [email protected] / / NPM install nodemailer / / need to open the pop3 sMTP / / IMAP service "use strict"; const nodemailer = require("nodemailer"); // const schedule = require('node-schedule') function random(Max,min){return Math.round(Math.random()*(max-min)+min); } const math = random(1000, 9999) const ToEmail = "[email protected], [email protected]" // async.. await is not allowed in global scope, must use a wrapper async function main() { // Generate test SMTP service account from ethereal.email // Only needed if you don't have a real mail account for testing // let testAccount = await nodemailer.createTestAccount(); // create reusable transporter object using the default SMTP transport let transporter = nodemailer.createTransport({ host: "smtp.163.com", port: 465, secure: true, // true for 465, false for other ports auth: { // user: testAccount.user, // generated ethereal user // pass: testAccount.pass, // generated ethereal password user: '[email protected]', pass: '88888888', }, }); // send mail with defined transport object let info = await transporter.sendMail({ from: '"GitCoding" <[email protected]>', // Sender address // to comma-separated list or arrangement of recipient email addresses to: ToEmail, // List of Receivers // // Subject line "Welcome to sign up for GitCoding", // cc comma-separated list or array of recipient email addresses to be displayed in the cc field cc: '[email protected]', text: "Hello world?", // plain text body // If HTML is defined, text HTML is ignored: 'GitCoding, your email verification code is :<b>${math}</b>', // HTML body // attachments: [{// Filename in the current directory: "Fujie. Js", "path" : ". / index. Js'}, {/ / create the file filename: 'content. TXT, content:' send content '}}); // console.log("Message sent: %s", info.messageId); // Message sent: <[email protected]> // Preview only available when sending through an Ethereal account //  Preview URL: https://ethereal.email/message/EUMOVKRRGYBBXBIB... } main().catch(console.error); Schedule.schedulejob ('30 * * * * *', () => {// main().catch(console.error); })Copy the code

Sending a Scheduled Task

Use node-schedule github.com/node-schedu…

// Trigger at 30 seconds per minute: '30 * * * * *' // Trigger at 1 minute 30 seconds per hour: '30 1 * * * *' // Trigger at 1 minute 1 minute 30 seconds per day: '30 1 1 * * *' // Trigger at 1 minute 1 minute 30 seconds per month: '30 1 1 1 * *' // January 1st 2016 1:1:30 trigger: '30 1 1 1 2016 *' // week 1 1:1:30 trigger: '30 1 1 * * 1' schedule.scheduleJob('1-40 * * * * *', () => { console.log('aa', new Date()) })Copy the code
/ / 1-40 seconds per minute '1-40 * * * * *' schedule. ScheduleJob (' 1-40 * * * * *, () = > {the console. The log (" aa ", new Date ())})Copy the code
// dayOfWeek // month // dayOfMonth // hour // minute // second // The incoming object triggers schedule.scheduleJob({second: 10, minute: 2, hour: 11}, () => { console.log('aa', new Date()) })Copy the code

Cancel timer

 const cancelTime = schedule.scheduleJob({second: 10, minute: 2, hour: 11}, () => {
   console.log('aa', new Date())
 })
 cancelTime.cancel();   

Copy the code

note

Github.com/Vincedream/…