您好,登錄后才能下訂單哦!
小編給大家分享一下怎么用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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。