Spring Boot 整合之JavaMail
阿新 • • 發佈:2018-11-16
1.新增依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
本次郵箱測試使用了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
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<String, Object> map = new HashMap<String, Object>(); 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<String, Object> 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; } }
4.建立郵箱模板
在 src/main/resources 下的 template 目錄下建立名為 mail.ftl 的檔案,其內容如下:
<!DOCTYPE html>
<html lang="zh">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<div style="width: 600px; text-align: left; margin: 0 auto;">
<h1 style="color: #005da7;">Java記</h1>
<div style="border-bottom: 5px solid #005da7; height: 2px; width: 100%;"></div>
<div style="border: 1px solid #005da7; font-size: 16px; line-height: 50px; padding: 20px;">
<div>${email},您好!</div>
<div>
這是個測試
</div>
<div>
想了解更多資訊,請訪問 <a href="https://www.javaj.work">https://www.javaj.work</a>
</div>
</div>
</div>
</body>
</html>
5.測試
@RunWith(SpringRunner.class)
@SpringBootTest
public class MailTest {
@Autowired
private JavaMailComponent javaMailComponent;
@Test
public void test() {
this.javaMailComponent.sendMail("********@qq.com");
}
}
執行結果如下: