實現郵件發送功能步驟:
建立springboot項目 ---> 申請qq郵箱授權碼 ---> 配置文件 ---> 代碼實現
目錄
一、新建springboot服務
添加依賴包:
<!-- mail -->
<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>
二、申請qq郵箱授權碼
申請方法:QQ郵箱->設置->賬戶->POP3/SMTP服務->開啓;開啓服務後發送短信驗證會獲得QQ的授權碼。
三、application.yml 文件配置
spring:
mail:
host: smtp.qq.com
password: #qq授權碼
username: #自己的qq賬號
default-encoding: utf-8
四、實現
Java代碼:
@Service
public class MailService {
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender sender;
//發送簡單郵件
public void sendMail(String sendTo,String subject,String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject(subject);
message.setTo(sendTo);
message.setText(content);
message.setFrom(from);
sender.send(message);
}
//發送簡單html郵件
public void sendHtmlMail(String sendTo,String subject,String content) throws MessagingException {
MimeMessage message = sender.createMimeMessage();
//封裝郵件格式內容
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(from);
helper.setTo(sendTo);
helper.setSubject(subject);
helper.setText(content,true);
sender.send(message);
}
//發送附件郵件,多附件
public void sendAttachMail(String sendTo, String subject, String content, List<String> filePathList) throws MessagingException {
MimeMessage message = sender.createMimeMessage();
//封裝郵件格式內容
MimeMessageHelper helper = new MimeMessageHelper(message,true);
helper.setFrom(from);
helper.setTo(sendTo);
helper.setSubject(subject);
helper.setText(content,true);
//附件
for (int i = 0; i < filePathList.size(); i++) {
FileSystemResource resource = new FileSystemResource(new File(filePathList.get(i)));
//獲取文件名
String fileName = resource.getFilename();
//添加附件
helper.addAttachment(fileName,resource);
}
sender.send(message);
}
}
html模板:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>郵件模板</title>
</head>
<body>
這是一個驗證郵件,點擊下面的鏈接註冊
<a href="#" th:href="@{http://www.baidu.com/{id}(id=${id})}">激活賬號</a>
</body>
</html>
五、單元測試
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DemoApplication.class)//springboot啓動類
public class MailServiceTest {
@Resource
MailService service;
@Resource
TemplateEngine templateEngine;
//發送簡單郵件
@Test
public void sendMail() {
service.sendMail("接收方的郵箱地址","郵件標題","郵件內容");
}
//發送多附件郵件
@Test
public void sendAttachMail() throws MessagingException {
List<String> fileList = new ArrayList<>();
fileList.add("你的附件地址1");
fileList.add("你的附件地址2");
StringBuffer sb = new StringBuffer();
sb.append("<h1>我的多附件郵件</h1>");
sb.append("<p>附件1</p>");
sb.append("<p>附件2</p>");
service.sendAttachMail("接收方的郵箱地址","我的第一封附件郵件。。",sb.toString(),fileList);
}
//通過郵件模板發送郵件
@Test
public void testTemplates() throws MessagingException {
Context context = new Context();
context.setVariable("id","220");
String content = templateEngine.process("emailTemplate",context);
service.sendHtmlMail("接收方的郵箱地址","模板郵件22",content);
}
}