Springboot系列之Springboot的郵件服務
前言
Springboot集常用的功能於一體,當然郵件功能作為最常見的功能,自然不能缺席Springboot的大家庭,spring-boot-starter-mail這個jar裡面封裝了SpringBoot的郵件功能。下面來看一下常見的郵件用途
預備知識:
Springboot 2.1.1
Maven 3.5.3
Lomlok 1.16.18(可以參考:lombok 簡化 Java 程式碼)
thymeleaf:3.0.11
junit 4.12
程式碼地址:
博文只是列舉核心操作步驟,需要自己實際操作。
https://github.com/Tojian/spring-treasure-box/tree/master/tao-springboot-mail
一、pom檔案新增mail相關依賴
郵件功能主要用到這個spring-boot-starter-mail這個starter,這個Starter裡面集成了很多java的mail和spring框架裡面的一些mail操作,比如核心的JavaMailSender類就是在Spring-context裡面封裝的。
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> <scope>provided</scope> </dependency> </dependencies>
二、在application.properties中新增郵箱配置
# 郵箱伺服器名字
spring.mail.host=smtp.163.com
# 傳送方郵箱地址
[email protected]
# 授權碼
spring.mail.password=XXX
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
# 收件郵箱地址
[email protected]
說明一下spring.mail.password 這個並不是你的郵箱密碼,而是你的郵箱的授權碼,下面以163郵箱截圖說明
三.簡單郵件實現
1.程式碼實現
@Autowired
private TemplateEngine templateEngine;
@Value("${spring.mail.username}")
private String from;
@Value("${mail.sender.to}")
private String to;
@Override
public void sendTxtMail(String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
mailSender.send(message);
log.info("send simple mail done");
} catch (Exception e) {
log.error("send simple mail happen exception", e);
}
}
2.程式碼測試
@Test
public void testSimpleMail() throws Exception {
mailSend.sendTxtMail("test simple mail", " hello this is text mail");
}
四.傳送Html格式
1.程式碼實現
@Override
public void sendHtmlMail(String subject, String content) {
MimeMessage message = mailSender.createMimeMessage();
try {
//true表示需要建立一個multipart message
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
// 設定為true
helper.setText(content, true);
mailSender.send(message);
log.info("send html mail done");
} catch (MessagingException e) {
log.error("send html mail happen exception", e);
}
}
2.程式碼測試
@Test
public void testHtmlMail() throws Exception {
String content = "<html>\n" +
"<body>\n" +
" <h3>hello world ! 這是一封Html郵件!</h3>\n" +
"</body>\n" +
"</html>";
mailSend.sendHtmlMail("test simple mail", content);
}
五.傳送附件
1.程式碼實現
@Override
public void sendAttachmentsMail(String subject, String content,String path) {
try {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setTo(to);
helper.setFrom(from);
helper.setSubject(subject);
helper.setText(content);
FileSystemResource file = new FileSystemResource(new File(path));
helper.addAttachment("附件-1.jpg", file);
helper.addAttachment("附件-2.jpg", file);
mailSender.send(mimeMessage);
log.info("send AttachmentsMail done ");
} catch (Exception e) {
log.error("send AttachmentsMail happen exception");
}
}
2.程式碼測試
@Test
public void testsendAttachmentsMail() {
String path = "/Users/taojian/Files/projectSofeware/github/spring-treasure-box/tao-springboot-mail/src/test/java/com/taojian/mail/tao.jpg";
mailSend.sendAttachmentsMail("附件傳送", "有附件", path);
}
六.傳送靜態資源內嵌
1.程式碼實現
@Override
public void sendInlineMail(String subject, String content,String path) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
FileSystemResource file = new FileSystemResource(new File(path));
helper.addInline("tao", file);
mailSender.send(mimeMessage);
log.info("send InlineMail done ");
} catch (Exception e) {
log.error("send InlineMail happen exception");
}
}
2.程式碼測試
@Test
public void testsendInlineMail() {
String content = "<html><body><img src=\"cid:tao\" ></body></html>";
String path = "/Users/taojian/Files/projectSofeware/github/spring-treasure-box/tao-springboot-mail/src/test/java/com/taojian/mail/tao.jpg";
mailSend.sendInlineMail("sendInlineMail", content, path);
}
七.傳送郵件模版
1.程式碼實現
1.建立郵件模版email.html
這裡採用了org.springframework.boot:spring-boot-starter-thymeleaf:2.1.1.RELEASE,thymeleaf和springboot有很好的整合,官方也推薦這種方式
<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Title</title>
</head>
<body>
您好 <td th:text="${username}"></td>,這是驗證郵件,請點選下面的連結完成驗證,<br/>
<a href="#" th:href="@{ http://www.taobigbig.com/ }">啟用賬號</a>
</body>
</html>
2.java程式碼
@Override
public void sendTemplateMail(String title, String content) {
try {
//建立郵件正文
Context context = new Context();
context.setVariable("username", "taojian");
// email是郵件模版
String emailContent = templateEngine.process("email", context);
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(title);
helper.setText(emailContent,true);
mailSender.send(mimeMessage);
log.info("send TemplateMail done ");
} catch (Exception e) {
log.error("send TemplateMail happen exception");
}
}
2.程式碼測試
@Test
public void testsendTemplateMail() {
mailSend.sendTemplateMail("send mail template", "send mail template test");
}
五、參考文獻
http://www.ityouknow.com/springboot/2017/05/06/springboot-mail.html
http://tengj.top/categories/Spring-Boot%E5%B9%B2%E8%B4%A7%E7%B3%BB%E5%88%97/
https://blog.csdn.net/clementad/article/details/51833416
https://github.com/pkhanal/spring-boot-starter-mail-sample
http://repo.spring.io/release/org/springframework/boot/spring-boot-starter-mail/
歡迎關注技術公眾號:Coder辰砂(太難聽的,有誰能幫我想個普通技術相關的公眾號,想不到,暫時用這個吧)
公眾號主要整理自己的一些技術方向的,計算機基礎課程,java框架,分散式等。