通过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.添加定时任务
@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("开始发送邮件。");
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("结束发送邮件。");
}
private MimeMessageHelper setInfoByHelper(String to, String subject, String content, MimeMessage message)
throws MessagingException {
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 {
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;
}
}