Spring Boot 整合之JavaMail

1.添加依賴

	org.springframework.boot
	spring-boot-starter-mail


	org.springframework.boot
	spring-boot-starter-freemarker

Spring Boot 整合之JavaMail

本次郵箱測試使用了freemarker模板

2.添加配置

在 application.properties 中添加

spring.mail.host=smtp.qq.com
spring.mail.username=********@qq.com
#郵箱授權碼
spring.mail.password=授權碼
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
Spring Boot 整合之JavaMail

3.創建JavaMailComponent

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
@Component
@EnableConfigurationProperties(MailProperties.class)
public class JavaMailComponent {
 private static final String template = "mail.ftl";
 @Autowired
 private FreeMarkerConfigurer freeMarkerConfigurer;
 @Autowired
 private JavaMailSender javaMailSender;
 @Autowired
 private MailProperties mailProperties;
 public void sendMail(String email) {
 Map map = new HashMap();
 map.put("email", email);
 try {
 // 獲取內容
 String text = this.getTextByTemplate(template, map);
 // 發送
 this.send(email, text);
 } catch (Exception e) {
 e.printStackTrace();
 }
 }
 private String getTextByTemplate(String template, Map model) throws Exception {
 return FreeMarkerTemplateUtils
 .processTemplateIntoString(this.freeMarkerConfigurer.getConfiguration().getTemplate(template), model);
 }
 private String send(String email, String text) throws MessagingException, UnsupportedEncodingException {
 MimeMessage message = this.javaMailSender.createMimeMessage();
 MimeMessageHelper helper = new MimeMessageHelper(message, true, "UTF-8");
 InternetAddress from = new InternetAddress();
 from.setAddress(this.mailProperties.getUsername());
 from.setPersonal("Java記", "UTF-8");
 helper.setFrom(from);
 helper.setTo(email);
 helper.setSubject("SpringBoot 發送的第一封郵件");
 helper.setText(text, true);
 this.javaMailSender.send(message);
 return text;
 }
}
Spring Boot 整合之JavaMail

4.創建郵箱模板

在 src/main/resources 下的 template 目錄下創建名為 mail.ftl 的文件,其內容如下:

Spring Boot 整合之JavaMail

5.測試

@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {
	@Autowired
	private JavaMailComponent javaMailComponent;
	
	 @Test
 public void test() {
 this.javaMailComponent.sendMail("********@qq.com");
 }
}
Spring Boot 整合之JavaMail

運行結果如下:

Spring Boot 整合之JavaMail

Spring Boot 整合之JavaMail


分享到:


相關文章: