Spring Boot筆記之郵件(spring-boot-starter-mail)
阿新 • • 發佈:2018-12-28
Spring Boot環境中傳送郵件
pom.xml引入spring-boot-starter-mail
Spring Boot2.x集成了mail模組,在dependencies
引入這個
<dependency >
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
application.yml配置
spring: mail: # 163 host: smtp.163.com port: username: [email protected] password: ************ protocol: smtp default-encoding: UTF-8 properties: mail.smtp.auth: true mail.smtp.starttls.enable: true mail.smtp.starttls.required: true mail.smtp.socketFactory.port: 465 mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory mail.smtp.socketFactory.fallback: false
其中spring.mail.host
spring.mail.port
spring.mail.username
spring.mail.password
不同郵箱的配置方法也不同
163郵箱
spring:
mail:
host: smtp.163.com
port:
username: [email protected]
password: ************
其中spring.mail.port
不指定;spring.mail.password
不是郵箱密碼,需要登入mail.163.com
,前往設定
客戶端授權密碼
QQ郵箱
spring:
mail:
host: smtp.qq.com
port: 587
username: [email protected]
password: ************
spring.mail.password
不是QQ密碼,登入mail.qq.com
,前往設定
賬戶
POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務
開啟POP3/SMTP服務
獲取一個16個字元的密碼
Gmail郵箱
spring:
mail:
host: smtp.gmail.com
port: 465
username: [email protected]
password: ****************
spring.mail.password
是Gmail的密碼,但是要前往Google賬戶的安全性較低的應用的訪問許可權中允許不安全應用。
傳送郵件
這是一個驗證碼模版郵件
service實現類
import com.my.demo.project.service.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Service;
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.Properties;
import java.util.UUID;
@Service
public class MailServiceImpl implements MailService {
@Autowired
private JavaMailSender mailSender;
/**
* 用來發送模版郵件
*/
@Autowired
private TemplateEngine templateEngine;
@Value("${spring.mail.username}")
private String from;
@Override
public void send(String to, String subject, String text) {
// SimpleMailMessage message = new SimpleMailMessage();
// message.setFrom(from);
// message.setTo(to);
// message.setSubject(subject);
// message.setText(text);
Context context = new Context();
context.setVariable("project", "demo");
context.setVariable("author", "yimcarson");
context.setVariable("code", text);
String emailContent = templateEngine.process("mail", context);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(emailContent, true);
} catch (MessagingException e) {
e.printStackTrace();
}
mailSender.send(message);
}
}
templates模版
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>yimcarson</title>
<style>
body {
text-align: center;
margin-left: auto;
margin-right: auto;
}
#main {
text-align: center;
position: absolute;
}
</style>
</head>
<body>
<div id="main">
<h3>Welcome <span th:text="${project}"></span> -By <span th:text=" ${author}"></span></h3>
Your Verification Code is
<h2><span th:text="${code}"></span></h2>
</div>
</body>
</html>
測試
import com.my.demo.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import java.util.Date;
import java.util.UUID;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {Application.class})
public class MailServiceTest {
@Autowired
private MailService mailService;
@Test
public void testSend() {
String to = "[email protected]";
mailService.send(to, "模板郵件", UUID.randomUUID().toString().toUpperCase());
}
}
結語
163
QQ
這兩個郵箱比較常用,至於Gmail
……,如果伺服器是阿里雲香港或者國外、亞馬遜這些的話可以正常使用,否則……,Couldn't connect to host, port: smtp.gmail.com, 465; timeout -1