Spring发送电子邮件
程序员文章站
2022-07-04 22:53:59
简介 Spring基于javax.mail封装了一套用于发送电子邮件的API,位于org.springframework.mail包下,下面将介绍下发送普通文本邮件和基于thymeleaf模版的邮件。 普通邮件 导入javax.mail依赖 Spring配置类 邮件配置:mail.propertie ......
简介
spring基于javax.mail封装了一套用于发送电子邮件的api,位于org.springframework.mail包下,下面将介绍下发送普通文本邮件和基于thymeleaf模版的邮件。
普通邮件
导入javax.mail依赖
<!-- ...... --> <!-- 省略spring依赖 --> <dependency> <groupid>com.sun.mail</groupid> <artifactid>javax.mail</artifactid> <version>1.6.2</version> </dependency>
spring配置类
@propertysource(value="classpath:mail.properties") @configuration public class springconfig { @autowired environment environment; /** * spring邮件配置 */ @bean public javamailsenderimpl javamailsenderimpl(){ javamailsenderimpl javamailsenderimpl = new javamailsenderimpl(); javamailsenderimpl.sethost(environment.getproperty("mail.host")); javamailsenderimpl.setusername(environment.getproperty("mail.username")); javamailsenderimpl.setpassword(environment.getproperty("mail.password")); javamailsenderimpl.setdefaultencoding(environment.getproperty("mail.encoding")); return javamailsenderimpl; } }
邮件配置:mail.properties
#qq邮箱发送邮件服务器 mail.host=smtp.qq.com #qq邮箱账户名 mail.username=xxxxxx@qq.com #qq邮箱授权码,开启imap/smtp服务时会生成 mail.password=lrorsapyffcwbegg #邮件发送编码 mail.encoding=utf-8
测试
@runwith(springrunner.class) @contextconfiguration(classes=springconfig.class) public class mailtest { @autowired private javamailsenderimpl javamailsenderimpl; @test public void testsimplemail() throws messagingexception{ mimemessage message = javamailsenderimpl.createmimemessage(); //发送带附件和内联元素的邮件需要将第二个参数设置为true mimemessagehelper helper = new mimemessagehelper(message, true); //发送方邮箱,和配置文件中的mail.username要一致 helper.setfrom("xxxxxx@qq.com"); //接收方 helper.setto("xxxxxx@qq.com"); //主题 helper.setsubject("邮件测试"); //邮件内容 helper.settext("<html><body><img src='cid:myphoto'></body></html>", true); //邮件内容内嵌的图片,addinline中的contentid要和邮件内容中的cid相对应 filesystemresource res = new filesystemresource(new file("e:/yc/1.jpg")); helper.addinline("myphoto", res); //邮件附件 filesystemresource file = new filesystemresource(new file("e:/yc/myphoto.zip")); helper.addattachment("myphoto.zip", file); javamailsenderimpl.send(message); } }
基于thymeleaf模版的邮件
导入thymeleaf-spring依赖
<dependency> <groupid>org.thymeleaf</groupid> <artifactid>thymeleaf-spring4</artifactid> <version>3.0.11.release</version> </dependency>
spring配置类中添加thymeleaf模板配置
@bean public templateengine emailtemplateengine() { springtemplateengine templateengine = new springtemplateengine(); classloadertemplateresolver templateresolver = new classloadertemplateresolver(); templateresolver.setresolvablepatterns(collections.singleton("*.html")); templateresolver.setprefix("/templates/"); templateresolver.setsuffix(".html"); templateresolver.settemplatemode(templatemode.html); templateresolver.setcharacterencoding("utf-8"); templateresolver.setcacheable(false); templateengine.addtemplateresolver(templateresolver); return templateengine; }
模版文件:/src/main/resources/templates/msg.html
<!doctype html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <style type="text/css">span{color:orange}</style> </head> <body> <p> 您好, <span th:text="${name}">xxx</span>,感谢您使用xxx服务,您正在进行邮箱验证,本次请求的验证码为: </p> <p><span th:text="${validcode}">1111</span>(为了保障您帐号的安全性,请在1小时内完成验证。)</p> <p> xxx团队<br /> 2019年06月06日 </p> </body> </html>
测试
@autowired private templateengine emailtemplateengine; @test public void testtemplatemail() throws messagingexception{ mimemessage message = javamailsenderimpl.createmimemessage(); mimemessagehelper helper = new mimemessagehelper(message, true); helper.setfrom("xxxxxx@qq.com"); helper.setto("xxxxxx@qq.com"); helper.setsubject("邮件测试"); //thymeleaf模版解析成string context ctx = new context(); ctx.setvariable("name", "小姐姐"); ctx.setvariable("validcode", "88888888"); string htmlcontent = emailtemplateengine.process("msg.html", ctx); helper.settext(htmlcontent, true); javamailsenderimpl.send(message); }
上一篇: 美团点评CAT监控平台研究
下一篇: 制作防毒磁盘(移动硬盘/U盘)