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


分享到:


相關文章: