最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
node实现定时发送邮件
时间:2017-08-28 编辑:简简单单 来源:一聚教程网
邮件的定时发送是现在我们经常会遇到的操作设计,定时发送邮件很多时候对我们工作是很重要的。这次文章就给大家介绍下node怎么实现定时发送邮件,定时发送邮件代码是什么?下面我们具体来看看。
定时发送,可做提醒使用
nodemailer
nodemailer 是一款简单易用的基于于SMTP协议(或 Amazon SES)的邮件发送组件
cron
cron可以指定每隔一段时间执行指定的程序、也可以指定每天的某个时刻执行某个程序、还可以按照星期、月份来指定。
npm install nodemailer -S npm install nodemailer-smtp-transport -S npm install cron -S
代码中有详细的注释(同时希望大家在平时写代码的时候养成写注释的习惯)
let nodemailer = require('nodemailer'),
smtpTransport = require('nodemailer-smtp-transport'),
cronJob = require('cron').CronJob;
// SMTP 连接
let transport = nodemailer.createTransport(smtpTransport({
// 主机
host: 'smtp.163.com',
// 是否使用 SSL
secure: false,
secureConnection: false,
// 网易的SMTP端口
port: 25,
auth: {
// 账号
user: '***@163.com',
// 授权码(自行百度邮箱SMTP的授权码设置),此处非密码
pass: '***',
}
}));
// 设置邮件内容
let mailOptions = {
// 发件人地址,例如 1234<1234@163.com>
from: '***<***@163.com>',
// 收件人地址,可以使用逗号隔开添加多个
// '***@qq.com, ***@163.com'
to: '***@qq.com',
// 标题
subject: 'Hello World',
// 邮件内容可以自定义样式
html: '测试"邮件轰炸机"'
}
// 定时发送邮件
// 每秒执行一次
// 具体的各项设置查看上方的链接
new cronJob('* * * * * *', () => {
transport.sendMail(mailOptions, (error, response) => {
if (error) {
console.error(error)
} else {
console.log('Message Send Ok')
}
// 记得关闭连接
transport.close();
})
}, null, true, 'Asia/Shanghai');
相关文章
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24
- Vue中的路由配置项meta使用解读 10-24
- SpringBoot自定义bean绑定解析 10-24
- SpringBoot常用计量与bean属性校验和进制数据转换规则解析 10-24
- 工厂方法在Spring框架中的运用介绍 10-24