1.pom.xml文件內容以下html
<?xml version="1.0" encoding="UTF-8"?>java
<project xmlns="http://maven.apache.org/POM/4.0.0"web
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"spring
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">apache
<modelVersion>4.0.0</modelVersion>springboot
<groupId>activiti.demo</groupId>app
<artifactId>activiti-demo</artifactId>maven
<version>1.0-SNAPSHOT</version>ide
<packaging>war</packaging>spring-boot
<name>activiti-demo</name>
<description>spring-activiti-demo</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.屬性文件配置以下
# 端口號
server.port=8082
spring.mail.host=smtp.163.com
spring.mail.username=xx@163.com
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
3.測試類以下
package com.springboot.demo.controller;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;
import javax.mail.internet.MimeMessage;
import java.io.File;
/**
* @ClassName LeaveControllerTest
* @Description TODO
* @Author yunshuodeng
* @Date 2019-05-06 11:03
* @Version 1.0
**/
@RunWith(SpringRunner.class)
@SpringBootTest
public class EmailSendTest {
@Autowired
private JavaMailSender javaMailSender;
@Value("${spring.mail.username}")
private String senderUserName;
/**
* 發送帶靜態資源的郵件-單元測試
*/
@Test
public void sendInlineMail() throws Exception{
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
mimeMessageHelper.setFrom(senderUserName);
mimeMessageHelper.setTo("691258810@qq.com");
mimeMessageHelper.setSubject("發送帶靜態資源郵件");
mimeMessageHelper.setText("<html><body>帶靜態資源的郵件內容 圖片:<img src='cid:picture' /></body></html>",true);
FileSystemResource fileSystemResource = new FileSystemResource(new File("/Users/yunshuodeng/Downloads/QQ20190507-102734.png"));
mimeMessageHelper.addInline("picture",fileSystemResource);
javaMailSender.send(mimeMessage);
}
/**
* 發送帶附件的郵件-單元測試
*/
@Test
public void sendAttachmentsMail() throws Exception{
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
mimeMessageHelper.setFrom(senderUserName);
mimeMessageHelper.setTo("691258810@qq.com");
mimeMessageHelper.setSubject("發送帶附件郵件");
mimeMessageHelper.setText("這是一個有附件的郵件");
// 附件
FileSystemResource fileSystemResource = new FileSystemResource(new File("/Users/yunshuodeng/Downloads/leavedemo.bpmn20.xml"));
mimeMessageHelper.addAttachment("leavedemo.bpmn20.xml",fileSystemResource);
javaMailSender.send(mimeMessage);
}
/**
* 發送html郵件-單元測試
*/
@Test
public void sendHtmlMail() throws Exception{
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
mimeMessageHelper.setFrom(senderUserName);
mimeMessageHelper.setTo("2247053450@qq.com");
mimeMessageHelper.setSubject("發送html郵件");
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("<h1>大標題</h1>");
stringBuffer.append("<h2>二標題</h2>");
stringBuffer.append("<h3>三標題</h3>");
mimeMessageHelper.setText(stringBuffer.toString(),true);
javaMailSender.send(mimeMessage);
}
/**
* 發送簡單文本郵件-單元測試
*/
@Test
public void sendSimpleTextMail() throws Exception{
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
// 發送人
simpleMailMessage.setFrom(senderUserName);
// 接收人
simpleMailMessage.setTo("691258810@qq.com");
// 標題
simpleMailMessage.setSubject("發送簡單文本郵件");
// 內容
simpleMailMessage.setText("這是文本描述");
javaMailSender.send(simpleMailMessage);
}
}