通过springBoot定时发送邮件
1.导入发送邮件的包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.配置yml
mail:
host: smtp.exmail.qq.com ##邮件域名
default-encoding: UTF-8
port: 465
protocol: smtps
username: zzzzz@qq.com ## 发送人邮件
password: sss ##企业邮箱获取服务授权码,可以百度一下怎么申请授权码
properties:
mail:
debug: false
smtp:
timeout: 10000
connection-timeout: 10000
writetimeout: 10000
ssl:
enabled: true
required: true
socketFactory:
class: javax.net.ssl.SSLSocketFactory
port: 465
3.添加定时任务
/**
* 每天12点更新
* <p>
* 0 0/1 * * * ?
* <p>
* 0 0 10 * * ?
*/
@Scheduled(cron = "0 0 12 * * ?")
public void endMailTask() throws IOException {
if (scheduledOpen) {
sendMailService.sendMail();
}
}
4.实现
@Service
public class SendMailServiceImpl implements SendMailService {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Resource
private JavaMailSender javaMailSender;
@Resource
private OrderDao orderDao;
//发送人
private String from = "test@qq.com";
//接收人
private String to = "test1@qq.com";
//标题
private String subject = "标题";
//正文
private String context = "标题";
@Override
public void sendMail() throws IOException {
logger.info("开始发送邮件。");
//getList
InputStreamSource orderList = getOrderList();
if (orderList == null) {
return;
}
MimeMessage message = javaMailSender.createMimeMessage();
try {
MimeMessageHelper helper = setInfoByHelper(to, subject, context, message);
helper.addAttachment("邮件名称.xls", orderList);
javaMailSender.send(message);
logger.info("带附件的邮件已经发送。");
} catch (MessagingException e) {
logger.error("发送带附件的邮件时发生异常!", e);
}
logger.info("结束发送邮件。");
}
/**
* 利用helper设置各种邮件发送相关的信息
*/
private MimeMessageHelper setInfoByHelper(String to, String subject, String content, MimeMessage message)
throws MessagingException {
//true表示需要创建一个multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
//接收人,可添加多个
String[] receiveUsers =new String[]{"sss", "sss"};
//抄送人,可添加多个
String[] copyUsers = new String[]{"sssss"};
helper.setTo(receiveUsers);
helper.setCc(copyUsers);
helper.setSubject(subject);
helper.setText(content, true);
return helper;
}
private InputStreamSource getOrderList() throws IOException {
//
// DateUtils.getDayStart()
//业务逻辑
List<OrderDO> orderAll = orderDao.getByStatusAndTime(ICBC_JH_DTQD.getItemCode(), 8 , dayStart, dayEnd);
if (orderAll.isEmpty()) {
return null;
}
//标头
String[] headers = {"订单编号", "充值电话号码", "充值金额(单位:分)", "支付完成时间"};
// 声明一个工作薄
HSSFWorkbook wb = new HSSFWorkbook();
// 生成一个表格
HSSFSheet sheet = wb.createSheet("sheet0");
HSSFRow row = sheet.createRow(0);
// 设置表头字体样式
HSSFFont columnHeadFont = wb.createFont();
columnHeadFont.setFontName("宋体");
columnHeadFont.setFontHeightInPoints((short) 10);
// 列头的样式
HSSFCellStyle columnHeadStyle = wb.createCellStyle();
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
// 左边框的颜色
columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);
// 边框的大小
columnHeadStyle.setBorderLeft(BorderStyle.valueOf((short) 1));
// 右边框的颜色
columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);
// 边框的大小
columnHeadStyle.setBorderRight(BorderStyle.valueOf((short) 1));
// 设置单元格的边框颜色
columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index);
// 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
// 设置普通单元格字体样式
HSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 10);
for (int i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(new HSSFRichTextString(headers[i]));
cell.setCellStyle(columnHeadStyle);
}
int rowIndex = 1;
for (OrderDO orderDO : orderAll){
row = sheet.createRow(rowIndex);
rowIndex++;
HSSFCell cell = row.createCell(0);
cell.setCellValue(new HSSFRichTextString(orderDO.getOrderNo()));
cell.setCellStyle(columnHeadStyle);
cell = row.createCell(1);
cell.setCellValue(new HSSFRichTextString(orderDO.getIssueAccountNum()));
cell.setCellStyle(columnHeadStyle);
cell = row.createCell(2);
cell.setCellValue(new HSSFRichTextString(orderDO.getPrizeAmount().toString()));
cell.setCellStyle(columnHeadStyle);
cell = row.createCell(3);
cell.setCellValue(new HSSFRichTextString(DateUtils.formatDateToStr(orderDO.getCompletionTime())));
cell.setCellStyle(columnHeadStyle);
}
ByteArrayOutputStream os = new ByteArrayOutputStream(1000);
wb.write(os);
wb.close();
InputStreamSource iss = new ByteArrayResource(os.toByteArray());
os.close();
return iss;
}
}
文章来源地址https://www.toymoban.com/news/detail-513575.html
文章来源:https://www.toymoban.com/news/detail-513575.html
到了这里,关于无缓存定时发送带附件(表格)等邮件的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!