1.pom.xml文件内容如下
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>activiti.demo</groupId>
<artifactId>activiti-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<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);
}
}
转载于:https://blog.51cto.com/suyanzhu/2390229