用wordpress建站案例,提供响应式网站建设,绍兴专业网站建设,百度广告推广springboot发送邮件,内容使用thymeleaf模板引擎排版 1、导入jar包2、yml设置3、收件人以及收件信息设置4、发邮件service5、模版页面6、controller 1、导入jar包
!--发送邮件--dependencygroupIdorg.springframework.boot/groupIdartifac… springboot发送邮件,内容使用thymeleaf模板引擎排版 1、导入jar包2、yml设置3、收件人以及收件信息设置4、发邮件service5、模版页面6、controller 1、导入jar包
!--发送邮件--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-mail/artifactId/dependency
!--使用thymeleaf 形式--dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-thymeleaf/artifactId/dependency2、yml设置
发件邮箱信息设置 spring:mail:host: smtp.xx.cnport: 465username: 你的邮箱password: 你的密码
# default-encoding: UTF-8protocol: smtpproperties: # 配置以SSL的方式发送, 这个需要使用这种方式并且端口是465mail:smtp:auth: truessl:enable: truesocketFactory:class: com.sun.mail.util.MailSSLSocketFactoryfallback: falsedebug: true
3、收件人以及收件信息设置
Getter
Setter
public class AcceptMailParam {//标题private String title;//接收人邮件地址private String receiveEmail[];//抄送人邮件地址private String chaoSongPersonEmail[];//附件value 文件的绝对地址/动态模板数据private MapString, Object attachment;//thymeleaf模版引擎页面对象数据邮件排版内存ListOperateDataResultInfoView pageViewList;
}页面对象数据展示
Setter
Getter
public class OperateDataResultInfoView {private Long id;/*** 存储当前年月,eg:202301*/private Integer curMonth;/*** 库名*/private String dbName;/*** 表名*/private String tableName;/*** 计划生成数据量*/private Long planGenDataCnt;/*** 实际生成数据量*/private Long actualGenDataCnt;/*** 备注*/private String remark;private String gmtCreate;private String gmtModified;/* ** 剩余待生成数据量*/private Long remainToGenCnt;/* ** 生成数据成功率(实际生成数量/计划生成数量),单位%*/private BigDecimal genDataSuccessRate;/**模拟生成数据*/public OperateDataResultInfoView() {this.id ThreadLocalRandom.current().nextLong(100L, 999L);this.curMonth DateUtil.getPreMonth();String[] dbAndTableName (dbName id .tableName id).split(\\.);this.dbName dbAndTableName[0];this.tableName dbAndTableName[1];this.planGenDataCnt id ThreadLocalRandom.current().nextLong(10L, 99L);this.actualGenDataCnt id;this.remark null;this.gmtCreate DateUtil.getNowStr();this.gmtModified DateUtil.getNowStr();this.remainToGenCnt (planGenDataCnt - actualGenDataCnt) 0 ? (planGenDataCnt - actualGenDataCnt) : 0;BigDecimal rate new BigDecimal(actualGenDataCnt).divide(new BigDecimal(planGenDataCnt), 4, BigDecimal.ROUND_HALF_UP);this.genDataSuccessRate rate.compareTo(new BigDecimal(1)) 0 ? new BigDecimal(100) : rate.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);}
}4、发邮件service
public interface SendMailService {/*** 邮件内容排版调试*/AcceptMailParam getMailContentAndSendMail(Integer curMonth);
}/*** 获取指定月份的邮件【邮件内容生成数据的结果集 信息】* 次月1日8点 发送*/Overridepublic AcceptMailParam getMailContentAndSendMail(Integer curMonth) {AcceptMailParam acceptMailParam new AcceptMailParam();ListOperateDataResultInfoView pageViewList new ArrayList();for (int i 0; i 10; i) {pageViewList.add(new OperateDataResultInfoView());}Collections.sort(pageViewList, Comparator.comparing(OperateDataResultInfoView::getId).reversed());acceptMailParam.setPageViewList(pageViewList);String[] emails {xxx1qq.com};String[] chaoSongPerson {xx2qq.com};//收件人支持多人acceptMailParam.setReceiveEmail(emails);//抄送人支持多人acceptMailParam.setChaoSongPersonEmail(chaoSongPerson);acceptMailParam.setTitle(统计业务表生成数据结果);//添加附件MapString, Object map new HashMap();//key为邮件附件文件名称路径改为你的服务器路径即可可添加不同类型文件map.put(1-test1.zip, /Users/gina/xx/data/test2.zip);map.put(2-test2.xlsx, /Users/gina/xx/data/test1.xlsx);acceptMailParam.setAttachment(map);//调试邮件内容排版时此处可注释掉。//this.sendTemplateMail(acceptMailParam,/mail/list);return acceptMailParam;}5、模版页面
在resources下在路径templates/mail/下添加文件list.html
!DOCTYPE htmlhtml xmlns:thhttp://www.thymeleaf.orgstyletr.odd td {background-color: #f0faaa;}td.number {text-align: right;}table {width: 100%;border: 1px solid #000;border-collapse: collapse;}table tbody tr td, table thead tr th {padding: 2px 10px;border-left: 1px dotted #000000;vertical-align: top;line-height: 20px;}table tbody tr, table thead tr {border-bottom: 1px solid #000000;}table th {background-color: #99cc33;}table tbody tr td a, table tbody tr td a:hover {text-decoration: none;font-weight: bold;background-color: #352726;padding: 2px 10px;color: #f0faaa;margin-left: 10px;text-transform: uppercase;border: 0px;}/styleheadtitle统计业务表生成数据结果/titlemeta http-equivContent-Type contenttext/html; charsetUTF-8 //headbodyh1统计业务表生成数据结果/h1tabletrth生成月份/thth库名/thth表名/thth计划生成(条)/thth实际生成(条)/thth成功率%/thth未生成(条)/thth创建时间/thth更新时间/thth备注/th/trtr th:eacho : ${pageViewList} th:class${o.id % 2 0}? odd td th:text${o.curMonth} /tdtd th:text${o.dbName} /tdtd th:text${o.tableName} /tdtd th:text${o.planGenDataCnt} /tdtd th:text${o.actualGenDataCnt}/tdtd th:text${o.genDataSuccessRate}/tdtd th:text${o.remainToGenCnt}/tdtd th:text${o.gmtCreate}/tdtd th:text${o.gmtModified}/tdtd th:text${o.remark}/td/tr/table/body
/html6、controller
Controller
Slf4j
public class SendMailController {/*** 调试生成的页面样式不发邮件* 数据处理结果集view*/Resourceprivate SendMailService sendMailService;/** 点此可直接访问* http://localhost:8081/sendMail?curMonth202311*/RequestMapping(sendMail)public String sendMail(Model model, RequestParam(name curMonth) Integer curMonth) {try {AcceptMailParam acceptMailParam sendMailService.getMailContentAndSendMail(curMonth);model.addAttribute(title, acceptMailParam.getTitle());model.addAttribute(pageViewList, acceptMailParam.getPageViewList());} catch (Exception e) {log.error(sendMail fail ,msg{}, e.getMessage());}return mail/list;}
}7、启动服务后访问页面展示
邮箱内容样式为 有对thymeleaf感兴趣的同学可通过中文官网去了解thymeleaf中文官网