/**定时任务:每周一将一周要发送的数据导出到excel并发送邮件*/
//每周一上午08:00 执行一次 @Scheduled(cron = "0 0 8 ? * MON")
@Scheduled(cron = "0 0 8 ? * MON")
public void exportWeekData() throws ApplicationException {
ByteArrayOutputStream file = wxSendMsgService.exportWeekData();//导出一周数据报表
LocalDateTime currTime = LocalDateTime.now();
String format = currTime.format(DateTimeFormatter.ofPattern("yyyy-M-d HH:mm:ss"));
logger.info("定时任务于 :{} 启动完成! ",format);
// 发邮件
// 标题内容
String subject = "xxxx";
// 指定收件人
String[] recipient = {"xxx@xxx.com","xxx@xxx.com"};
// 指定抄送人
String[] ccList = null;
// 正文内容
String content = "xxxx";
// 附件名
String fileName = "xxxx.xls";
SendMail sendMail = new SendMail();
sendMail.send(subject,content,file,fileName,recipient,ccList);
logger.info("邮件发送成功!");
}
发送邮件工具类:
public class SendMail {
private static String host = "smtp.exmail.qq.com";
private static String account = "xxx@xxx.com";
private static String pass = "xxxxxxx";
private static String port = "465";
private static String protocol = "smtp";
/**
* 发送内容和附件, 发送一个人recipient为String 类型或多个人为String数组类型
*
* @param recipient
* 收信人邮件
* @param subject
* 信的主题
* @param content
* 信的内容
* @param file
* 信的附件
* @return 是否发送成功
* @throws AddressException
*/
public boolean send(String subject, String content, ByteArrayOutputStream file, String fileName, String[] recipient, String[] cs) {
try {
Properties props = new Properties();
props.put("mail.smtp.host", host);//设置发送邮件的邮件服务器
props.put("mail.smtp.auth", "true"); //需要经过授权,也就是用户名和密码的校验,这样才能通过验证
props.setProperty("mail.transport.protocol", protocol);//设置协议
props.setProperty("mail.smtp.port", port);//设置端口
//使用SSL 企业邮箱必须
//开启安全协议
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
} catch (GeneralSecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
// 获取默认session对象
Session session = Session.getDefaultInstance(props,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(account, pass); //发件人邮件用户名、授权码
}
});
MimeMessage msg = new MimeMessage(session); // 邮件信息处理类
msg.setFrom(new InternetAddress(account)); // 设置发信邮箱
InternetAddress[] recipients = new InternetAddress[recipient.length]; // 多个发送地址
for (int i = 0; i < recipient.length; i++) {
recipients[i] = new InternetAddress(recipient[i]);
}
msg.setRecipients(RecipientType.TO, recipients); // 设置多收信人邮箱
msg.setSubject(subject); // 写邮件主题
if (cs != null) // 抄送
{
InternetAddress[] iaToListcs = new InternetAddress[cs.length];
for (int i = 0; i < cs.length; i++) {
iaToListcs[i] = new InternetAddress(cs[i]);
}
msg.setRecipients(RecipientType.CC, iaToListcs); // 抄送人
}
MimeMultipart multipert = new MimeMultipart(); // 要发送数据包(邮件内容和邮件附件)
// 邮件内容
MimeBodyPart contentpart = new MimeBodyPart(); // 邮件内容
contentpart.setContent(content, "text/html;charset=utf-8"); // 邮件内容数据
multipert.addBodyPart(contentpart);// 加入内容到数据包
// 判断
if (file != null) {
MimeBodyPart fileBody = new MimeBodyPart(); // 邮件附件
DataSource source = new ByteArrayDataSource(file.toByteArray(), "application/msexcel");
fileBody.setDataHandler(new DataHandler(source));
// 中文乱码问题
fileBody.setFileName(MimeUtility.encodeText(fileName));// 解决乱码
multipert.addBodyPart(fileBody); // 加入附件到发送主体
}
msg.setContent(multipert); // 把邮件内容和邮件附件加入到信息处理类
msg.saveChanges(); // 保存邮件
Transport.send(msg);// 发邮件
return true;
} catch (Exception e) {
throw new RuntimeException("[" + account + "]发送给"+ Arrays.toString(recipient) + "的邮件失败...", e);
}
}
}