代码先直接亮代码app.js,后面有说明。点击博客食用味道更鲜美哦!!talk is cheap show me the code’use strict’;const nodemailer = require(’nodemailer’) // 引入发邮箱模块const schedule = require(’node-schedule’) // 引入定时发任务模块const transporter = nodemailer.createTransport({ service: ‘QQ’, port: 465, secureConnection: true, secure: true, auth: { user: ‘xxxx@qq.com’, // qq 邮箱 pass: ‘xxxx’ // IMAP/SMTP授权码,需要从qq邮箱获取 }})transporter.verify((error, success) => { if (error) { console.warn(‘邮件客户端初始化连接失败’); } else { console.log(‘邮件客户端初始化连接成功,随时可发送邮件’); }})let mailOptions = { from: ‘“test ????” <xxxx@qq.com>’, // 发送邮箱 to: ‘xxxx@gmail.com’, // 接收邮箱 可以是多个用,隔开 subject: ‘Hello ✔’, // 邮件主题 text: ‘Hello world?’, // 邮件表题 html: ‘<b>Hello world?</b>’ // 邮件主题}const dateOpt = { year: ’numeric’, month: ’numeric’, day: ’numeric’, hour: ’numeric’, minute: ’numeric’, second: ’numeric’, hour12: false}const scheduleCronstyle = () => { //每分钟的第30秒定时执行一次: schedule.scheduleJob(‘30 * * * * *’, async () => { console.log(‘scheduleCronstyle:’, (new Date()).toLocaleString(‘zh’, dateOpt)) let info = await transporter.sendMail(mailOptions) console.log(‘Message sent: %s’, info.response) })}// 执行定时任务scheduleCronstyle()安装npm install nodemailer node-schedule执行node app.js就可以直接每分钟的第30秒定时执行一次邮箱了这里可能有点不解的是,怎么拿到QQ邮箱的IMAP/SMTP授权码。请点击我配置定时任务有不解的, 请点击我点击博客再看一遍!!