如何用Spring發送電子郵件
簡介
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郵箱賬戶名 [email protected] #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("[email protected]"); //接收方 helper.setTo("[email protected]"); //主題 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("[email protected]"); helper.setTo("[email protected]"); 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發送電子郵件的詳細內容,更多關於Spring發送電子郵件的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- SpringBoot實現定時發送郵件的三種方法案例詳解
- SpringBoot Mail郵件任務詳情
- SpringBoot QQ郵箱發送郵件實例代碼
- Springboot實現發送郵件
- Spring Boot實現發送郵件