node 写一个定时服务,每天9点发送邮件给qq邮箱
时间: 2023-05-15 14:04:37 浏览: 95
可以使用 Node.js 的 node-schedule 模块来实现定时任务。以下是一个示例代码:
```javascript
const schedule = require('node-schedule');
const nodemailer = require('nodemailer');
// 创建一个发送邮件的 transporter
const transporter = nodemailer.createTransport({
service: 'qq',
auth: {
user: 'your_qq_email@qq.com',
pass: 'your_qq_email_password'
}
});
// 创建一个每天 9 点发送邮件的定时任务
const job = schedule.scheduleJob('0 0 9 * * *', function() {
const mailOptions = {
from: 'your_qq_email@qq.com',
to: 'recipient_email@example.com',
subject: '邮件主题',
text: '邮件正文'
};
// 发送邮件
transporter.sendMail(mailOptions, function(error, info) {
if (error) {
console.log(error);
} else {
console.log('邮件已发送: ' + info.response);
}
});
});
```
请注意,您需要将示例代码中的 "your_qq_email@qq.com" 和 "your_qq_email_password" 替换为您自己的 QQ 邮箱地址和密码,将 "recipient_email@example.com" 替换为您要发送邮件的收件人地址。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)