# SpringBoot-email
**Repository Path**: wei_dongdong/spring-boot-email
## Basic Information
- **Project Name**: SpringBoot-email
- **Description**: SpringBoot整合Email
- **Primary Language**: Java
- **License**: GPL-3.0
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 1
- **Forks**: 0
- **Created**: 2020-08-30
- **Last Updated**: 2021-03-08
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# SpringBoot-email
#### 介绍
SpringBoot整合Email
#### 软件架构
软件架构说明
#### 安装教程
### spring boot发送Email
#### 1、发送普通邮件
##### 1、开启邮箱的POP3/SMTP服务
##### 2、创建项目添加mail依赖
```xml
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-mail
```
##### 3、配置xml文件的位置
```xml
src/main/java
**/*.xml
```
##### 4、添加配置文件(redis和mail)
```yml
server:
port: 8080
#servlet:
#context-path: /demo4
spring:
mail:
#邮箱主机
host: smtp.sina.com
#用户名
username: kingqingqing@sina.com
#授权码
password: cc7cb383d1d80c39
redis:
# Redis数据库索引(默认为0)
database: 0
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码(默认为空)
# password:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/tcmp?useSSL=false&characterEncoding=utf8&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
username: root
password: 123456
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: HTML5
encoding: utf-8
mybatis:
type-aliases-package: cn.kgc.entity
#pagehelper:
#helper-dialect: mysql
#reasonable: true
#support-methods-arguments: true
#params: count=countSql
```
##### 5、MailUtil工具类(util包)
```java
package cn.kgc.util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
/**
* @author Rock
* @create 2020-03-26 16:33
*/
@Component
public class MailUtil {
@Value("${spring.mail.username}")
private String from;
@Autowired
private JavaMailSender javaMailSender;
/**
* @param to 收件人
* @param subject 邮件标题
* @param context 邮件内容
*/
public void send(String to,String subject,String context){
//消息处理类
MimeMessage mimeMessage = null;
//发件人
try {
mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
//发件人
helper.setFrom(from);
//收件人
helper.setTo(to);
//邮件标题
helper.setSubject(subject);
//邮件内容
helper.setText(context);
//发送
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
```
##### 6、测试controller
```java
@RestController
public class MailController {
@Autowired
private MailUtil mailUtil;
@RequestMapping(value = "/send",method = RequestMethod.GET)
public String send(){
String to = "417496479@qq.com";
String subject = "3.26的测试信息";
String context = "作业";
mailUtil.send(to,subject,context);
return "发送邮件完成!";
}
}
```
##### 7、用Postman进行测试
```java
地址栏get方式输入:http://localhost:8080/send,点击send进行测试;
控制台输出:发送邮件完成!则成功。
```
#### 2、发送带附件的邮件
##### 1、MailUtil工具类(util包)
```java
/**
*
* @param to 收件人
* @param subject 邮件标题
* @param context 邮件内容
* @param filepath 附件地址
*/
public void send2(String to,String subject,String context,String filepath){
//消息处理类
MimeMessage mimeMessage = null;
try {
mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
//发件人
helper.setFrom(from);
//收件人
helper.setTo(to);
//邮件标题
helper.setSubject(subject);
//邮件内容
helper.setText(context);
//附件
FileSystemResource fileSystemResource = new FileSystemResource(new File(filepath));
//解析文件名
String fileName = filepath.substring(filepath.lastIndexOf(File.separator));
helper .addAttachment(fileName,fileSystemResource);
//发送邮件
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
```
##### 2、测试controller
```java
@RequestMapping(value = "/send2",method = RequestMethod.GET)
public String send2(){
String to = "417496479@qq.com";
String subject = "报表文件";
String context = "您好,附件是报表文件,请查收";
String filepath = "F:\\学习笔记\\springboot学习杂记.md";
mailUtil.send2(to,subject,context,filepath);
return "发送带附件邮件成功!";
}
```
##### 3、用Postman进行测试
#### 3、发送的邮件是HTML的
##### 1、MailUtil工具类(util包)
```java
public void send3(String to,String subject,String context){
try {
//消息处理类
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
//发件人
helper.setFrom(from);
//收件人
helper.setTo(to);
//邮件标题
helper.setSubject(subject);
//邮件内容
helper.setText(context,true);
//发送邮件
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
```
##### 2、测试controller
```java
public String send3(){
String to = "417496479@qq.com";
String subject = "报表文件";
String context = "您好,附件是报表文件,请查收";
mailUtil.send3(to,subject,context);
return "发送邮件完成!";
}
```
##### 3、用Postman进行测试
#### 4、模板方式发送HTML邮件
##### 1、创建页面模板(hello.html)
```html
Title
您好,
您是商城的第幸运用户。
```
##### 2、MailUtil工具类(util包)
```java
public void send4(String to, String subject, String templateName, Map map){
Context context = new Context();
context.setVariables(map);
//将页面解析成字符串类型的数据
String text = templateEngine.process(templateName,context);
try {
//消息处理类
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
//发件人
helper.setFrom(from);
//收件人
helper.setTo(to);
//邮件标题
helper.setSubject(subject);
//邮件内容
helper.setText(text,true);
//发送邮件
javaMailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
```
##### 3、测试controller
```java
@RequestMapping("/send4")
public String send4(){
String to = "417496479@qq.com";
String subject = "作业3";
String templateName = "hello.html";
Map map = new HashMap<>();
map.put("name","wangqing");
map.put("num","26");
mailUtil.send4(to,subject,templateName,map);
return "发送页面邮件成功!";
}
```
#### 使用说明
1. xxxx
2. xxxx
3. xxxx
#### 参与贡献
1. Fork 本仓库
2. 新建 Feat_xxx 分支
3. 提交代码
4. 新建 Pull Request
#### 码云特技
1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md
2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com)
3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目
4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目
5. 码云官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help)
6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)