公司建设网站费用直接列支,学生个人网页制作 模板,wordpress qq登录免费下载,iis添加网站无法访问概述
本例子基于springboot实现了后台定时统计数据报表并将数据生成excel文件作为附件#xff0c;然后通过邮件发送通知的功能。
详细
一、准备工作 1、首先注册两个邮箱#xff0c;一个发送邮箱#xff0c;一个接收邮箱。 2、发送邮箱开启IMAP/SMTP/POP3服务#xff0c…概述
本例子基于springboot实现了后台定时统计数据报表并将数据生成excel文件作为附件然后通过邮件发送通知的功能。
详细
一、准备工作 1、首先注册两个邮箱一个发送邮箱一个接收邮箱。 2、发送邮箱开启IMAP/SMTP/POP3服务记录得到的授权码 二、项目结构及具体实现
1、项目结构 2、实现代码
配置文件
server:port: 8088spring:mail:host: smtp.163.comport:username: demodashi2021002163.compassword: WGWRIGYQSCPGBJCCprotocol: smtptest-connection: truedefault-encoding: UTF-8
email:temp: C:/email/receiver: demodashi2021001163.com
邮件服务接口实现邮件发送功能。
/*** 发送带附件的邮件** param receiver 收件人地址* param subject 邮件主题* param content 邮件内容* param filePath 附件地址* param cc 抄送地址* throws MessagingException 邮件发送异常*/
Override
public void sendAttachmentsMail(String receiver, String subject, String content, String filePath, String... cc) throws MessagingException {MimeMessage message mailSender.createMimeMessage();MimeMessageHelper helper new MimeMessageHelper(message, true);helper.setFrom(sender);helper.setTo(receiver);helper.setSubject(subject);helper.setText(content, true);if (ArrayUtil.isNotEmpty(cc)) {helper.setCc(cc);}FileSystemResource file new FileSystemResource(new File(filePath));String fileName filePath.substring(filePath.lastIndexOf(File.separator)1);helper.addAttachment(fileName, file);mailSender.send(message);
}
定时发送任务本地测试设置每十秒运行一次定时任务。
Scheduled(cron 10/20 * * ? * ?)
private void sendEventEmailTask() {LOGGER.info(启动邮件发送任务);SimpleDateFormat sdf new SimpleDateFormat(yyyy-MM-dd);String startTime getPastDate(7) 00:00:00;String endTime sdf.format(new Date()) 23:59:59;String subject 最近一周的数据;this.emailJob(startTime, endTime,subject);LOGGER.info(结束邮件发送任务);
}
三、演示效果
运行项目后等待十秒查看日志 查看接收邮箱 附件