日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

Java Mail+Thymeleaf模板引擎实现发送HTML格式邮件

發布時間:2024/10/8 HTML 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java Mail+Thymeleaf模板引擎实现发送HTML格式邮件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Java Mail+Thymeleaf模板引擎實現發送HTML格式郵件

基于Spring boot 1.5,Spring boot 2.x請使用Spring boot mail

1.依賴坐標

// buildscript 代碼塊中腳本優先執行 buildscript {//springBootVersion = '1.5.2.RELEASE' 經典穩定版本// ext 用于定義動態屬性ext {springBootVersion = '1.5.2.RELEASE'}// 自定義 Thymeleaf 和 Thymeleaf Layout Dialect 的版本ext['thymeleaf.version'] = '3.0.3.RELEASE'ext['thymeleaf-layout-dialect.version'] = '2.2.0'// 自定義 Hibernate 的版本ext['hibernate.version'] = '5.2.8.Final'// 使用了 Maven 的中央倉庫(你也可以指定其他倉庫)repositories {//mavenCentral()maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'}}// 依賴關系dependencies {// classpath 聲明說明了在執行其余的腳本時,ClassLoader 可以使用這些依賴項classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")} }// 使用插件 apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot'// 打包的類型為 jar,并指定了生成的打包的文件名稱和版本 jar {baseName = 'blog-search'version = '1.0.0' }// 指定編譯 .java 文件的 JDK 版本 sourceCompatibility = 1.8// 默認使用了 Maven 的中央倉庫。這里改用自定義的鏡像庫 repositories {//mavenCentral()maven {url 'http://maven.aliyun.com/nexus/content/groups/public/'} }// 依賴關系 dependencies {// 該依賴對于編譯發行是必須的compile('org.springframework.boot:spring-boot-starter-web')// 添加 Thymeleaf 的依賴compile('org.springframework.boot:spring-boot-starter-thymeleaf')// 添加 Spring Security 依賴compile('org.springframework.boot:spring-boot-starter-security')// 添加 Spring Boot 開發工具依賴//compile("org.springframework.boot:spring-boot-devtools")// 添加 Spring Data JPA 的依賴compile('org.springframework.boot:spring-boot-starter-data-jpa')// 添加 MySQL連接驅動 的依賴compile('mysql:mysql-connector-java:6.0.5')// 添加 Thymeleaf Spring Security 依賴,與 Thymeleaf 版本一致都是 3.xcompile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:3.0.2.RELEASE')// 添加 Apache Commons Lang 依賴compile('org.apache.commons:commons-lang3:3.5')// 添加 Markdown parser 依賴compile('es.nitaur.markdown:txtmark:0.16')// 添加 Spring Data Elasticsearch 的依賴compile('org.springframework.boot:spring-boot-starter-data-elasticsearch')// 添加 JNA 的依賴compile('net.java.dev.jna:jna:4.3.0')// 該依賴對于編譯測試是必須的,默認包含編譯產品依賴和編譯時依testCompile('org.springframework.boot:spring-boot-starter-test')/*FreeMarker是一款模板引擎: 即一種基于模板和要改變的數據, 并用來生成輸出文本(HTML網頁、電子郵件、配置文件、源代碼等)的通用工具。它不是面向最終用戶的,而是一個Java類庫,是一款可以嵌入所開發產品的組件。*///compile('org.springframework.boot:spring-boot-starter-freemarker')//添加 JavaMail 發送郵件compile("javax.mail:javax.mail-api:1.5.6")compile("com.sun.mail:javax.mail:1.5.6")//添加Spring Boot Mail 的依賴,注意SpringBoot2.x以上版本//compile("org.springframework.boot:spring-boot-starter-mail") }

2.配置文件

#Java Mail # 郵件服務器地址 mail.host=smtp.qq.com #發件人郵箱地址 mail.user=***@qq.com #發件人郵箱密碼 mail.password=****

3.郵件工具類

import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.thymeleaf.TemplateEngine; import org.thymeleaf.context.Context;import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Map; import java.util.Properties;@Component public class MailTemplateSenderUtils {@Value("${mail.user}")private String USER;@Value("${mail.password}")private String PASSWORD;@Value("${mail.host}")private String HOST;@Autowiredprivate TemplateEngine templateEngine;public Boolean sendMail(Map<String, Object> valueMap) {try {final Properties props = new Properties();props.put("mail.smtp.auth", "true");props.put("mail.smtp.host", HOST);// 發件人的賬號props.put("mail.user", USER);//發件人的密碼props.put("mail.password", PASSWORD);// 構建授權信息,用于進行SMTP進行身份驗證Authenticator authenticator = new Authenticator() {@Overrideprotected PasswordAuthentication getPasswordAuthentication() {// 用戶名、密碼String userName = props.getProperty("mail.user");String password = props.getProperty("mail.password");return new PasswordAuthentication(userName, password);}};// 使用環境屬性和授權信息,創建郵件會話Session mailSession = Session.getInstance(props, authenticator);// 創建郵件消息MimeMessage message = new MimeMessage(mailSession);// 設置發件人String username = props.getProperty("mail.user");InternetAddress form = new InternetAddress(username);message.setFrom(form);// 設置收件人String to = (String) valueMap.get("to");InternetAddress toAddress = new InternetAddress(to);message.setRecipient(Message.RecipientType.TO, toAddress);// 設置郵件標題String title = (String) valueMap.get("title");message.setSubject(title);// 設置郵件的內容體Context context = new Context();context.setVariables(valueMap);String content = templateEngine.process("MailTemplate", context);message.setContent(content, "text/html;charset=UTF-8");// 發送郵件Transport.send(message);return true;} catch (Exception e) {e.printStackTrace();}return false;} }

總結

以上是生活随笔為你收集整理的Java Mail+Thymeleaf模板引擎实现发送HTML格式邮件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。