文章目录
提示:以下是本篇文章正文内容,下面案例可供参考
一、pom依赖包
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.12</version>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
二、定时任务,发送邮件。
代码如下(示例):
import cn.deling.common.enums.MailEnum;
import cn.deling.device.entity.dataobject.TuyaAuthDTO;
import cn.deling.device.service.AutoAuthorizationService;
import cn.hutool.extra.mail.MailUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@Component
@EnableScheduling
@Slf4j
public class TuyaAuthMailTask {
@Resource
private AutoAuthorizationService autoAuthorizationService;
//@Scheduled(cron ="0/40 * * * * ?") // 每40s 执行一次。
@Scheduled(cron ="0 0 9 * * ?") // 每天9点执行一次
public void TuyaAuthMail() {
log.error("=====================================================定时任务触发============================================================");
List<TuyaAuthDTO> tuyaAuthList = autoAuthorizationService.queryAllTuyaAuthMail();
MailUtil.send("ryan2103@qq.com", "涂鸦PID授权余量,通知!", SenMailHTML(tuyaAuthList), true);
}
private String SenMailHTML(List<TuyaAuthDTO> list) {
if (CollectionUtils.isNotEmpty(list)) {
// Excel内容在邮件正文显示
StringBuilder htmlText = new StringBuilder();
String htmlStar = "<html><body><table border=\"1px\" cellpadding=\"10px\">";
String htmlEnd = "</table></body></html>";
// 表头
String th = "<tr><th>PID类型</th>" +
"<th>产品PID</th>" +
"<th>PID剩余总量</th></tr>";
// 行数据
StringBuilder tr = new StringBuilder();
// 遍历集合,封装table行数据。
for (int i = 0; i < list.size(); i++) {
// 只展示前20条数据
if (i == 20) {
tr.append("更多数据请打开Excel附件查看详情O(∩_∩)O~");
break;
}
tr.append(
"<tr>" +
"<td>" + list.get(i).getPidType() + "</td>" +
"<td>" + list.get(i).getPid() + "</td>" +
"<td>" + list.get(i).getUnusedCount() + "</td>" +
"</tr>"
);
}
// 拼接HTML页面
htmlText.append(htmlStar);
htmlText.append(th);
htmlText.append(tr);
htmlText.append(htmlEnd);
return String.valueOf(htmlText);
}
return "PID权限查询列表List集合,为空!!!";
}
}
// 遍历集合,封装table行数据。
list.stream().peek(item -> {
int size = item.getProjectName().size();
if (size > 1) {
Stream.iterate(0, num -> num +1).limit(size).forEach( a-> {
if (a == 0) {
tr.append(
"<tr>" +
"<td>" + item.getProjectName().get(a) + "</td>" +
"<td rowspan = "+ item.getProjectName().size() +">" + item.getPid() + "</td>" +
"<td rowspan = "+ item.getProjectName().size() +">" + item.getPidType() + "</td>" +
"<td rowspan = "+ item.getProjectName().size() +">" + item.getUnusedCount() + "</td>" +
"</tr>"
);
} else {
tr.append(
"<tr >" +
"<td>" + item.getProjectName().get(a)+ "</td>" +
"</tr>"
);
}
});
} else {
tr.append(
"<tr>" +
"<td>" + item.getProjectName().get(0) + "</td>" +
"<td>" + item.getPid() + "</td>" +
"<td>" + item.getPidType() + "</td>" +
"<td>" + item.getUnusedCount() + "</td>" +
"</tr>"
);
}
}).collect(Collectors.toList());
1256

被折叠的 条评论
为什么被折叠?



