小编给大家分享一下怎么用Spring发送电子邮件,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
Spring基于javax.mail封装了一套用于发送电子邮件的API,位于org.springframework.mail包下,下面将介绍下发送普通文本邮件和基于thymeleaf模版的邮件。
<!-- ...... --> <!-- 省略Spring依赖 --> <dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency>
@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; } }
#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); } }
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.11.RELEASE</version> </dependency>
@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; }
<!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); }
以上是“怎么用Spring发送电子邮件”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。