一.准备
在创建SpringBoot项目
二、选择依赖
选择依赖时 发现其选择依赖时有邮件发送与Springboot整合的jar包,我们勾选即可
如果未勾选也不要紧,咱们手动导入
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId>
</dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
三、等待依赖下载完成, 增加配置文件
spring:mail:#smtp服务主机 qq邮箱则为smtp.qq.comhost: smtp.qq.com#服务协议protocol: smtp# 编码集default-encoding: UTF-8#发送邮件的账户username: 251xxxx2@qq.com#授权码password: opwuxxxxxoceaiitest-connection: trueproperties:mail:smtp:auth: truestarttls:enable: truerequired: true
四、简单邮件发送
Springboot已经给我们邮件发送进行了非常好的整合了,我们只需要注入邮件发送接口 调用其中的方法,就能轻松而愉悦的进行邮件发送了!
我们只需要在任意交由Spring管理的类(例如你的controller、service层等)下注入以下接口即可
@Autowiredprivate JavaMailSender mailSender;
由于每一封邮件都有固定的内容 例如 收件人信息 邮件内容 邮件标题 那么我们充分利用java面向对象的特性,我们把邮件发送抽取为一个对象
获取发送方账户信息
@Value("${spring.mail.username}")private String from;
定义一个实体:
package info.lyscms.springboot.mail.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** @author Administrator
* @version 2021/5/13 0013*/@Data@AllArgsConstructor@NoArgsConstructorpublic class SimpleEmailEntity {/*** 主题*/private String subject;/*** 主题内容*/private String content;/*** 接收人邮箱列表*/private String[] tos;
}
定义发送接口
void sendSimpleMail(SimpleEmailEntity simpleEmailEntity);
发送邮件方法实现示例
@Overridepublic void sendSimpleMail(SimpleEmailEntity simpleEmailEntity) {SimpleMailMessage message = new SimpleMailMessage();message.setFrom(from);message.setSubject(simpleEmailEntity.getSubject());message.setText(simpleEmailEntity.getContent());message.setTo(simpleEmailEntity.getTos());javaMailSender.send(message);}
定义一个controller类并定义发送邮件接口
package info.lyscms.springboot.mail.controller;import info.lyscms.springboot.mail.entity.SimpleEmailEntity;import info.lyscms.springboot.mail.service.MailService;import lombok.RequiredArgsConstructor;import org.springframework.web.bind.annotation.*;@RestController@RequiredArgsConstructor@RequestMapping("/mail")public class MailController {private final MailService mailService;@PostMapping("/simple")public void sendSimpleMail(@RequestBody SimpleEmailEntity simpleEmailEntity){mailService.sendSimpleMail(simpleEmailEntity);}}
使用类似postman工具调用接口
###POST http://localhost:8080/mail/simpleContent-Type: application/json{"tos": ["ly23580512@163.com"],"subject": "Hello Watcher","content": "Welcome to lyscms.info"}
五、HTML格式邮件发送
1.创建一个MINE消息
//创建一个MINE消息MimeMessage message = javaMailSender.createMimeMessage();
2.创建一个MimeMessageHelper对象,并且需要传入一个MimeMessage
MimeMessageHelper mineHelper = new MimeMessageHelper(message, true);//谁发mineHelper.setFrom(from);//谁要接收mineHelper.setTo(htmlEmailEntity.getTos());//邮件主题mineHelper.setSubject(htmlEmailEntity.getSubject());//邮件内容 true 表示带有附件或htmlmineHelper.setText(htmlEmailEntity.getContent(), true);
3.使用JavaMailSender接口发送
javaMailSender.send(message);
4.content 是类似<h1>Hi Xiaoming…</h1><span>Hi Xiaoming…</span>这样的HTML字符串即可。
###POST http://localhost:8080/mail/htmlContent-Type: application/json{"tos": ["ly23580512@163.com],"subject": "Hello Watcher Html","content": "<h1>Hi Xiaoming...</h1><span>Hi Xiaoming...</span>"}
六、带附件邮件发送
相对html发送方式就多了一个addAttachment方法
//添加附件
mineHelper.addAttachment("附件文件名称.txt",new File("D://1.txt"));
访问
###
POST http://localhost:8080/mail/file
Content-Type: application/json{"tos": ["ly23580512@163.com"],"subject": "Hello Watcher Html File","content": "<h1>Hi Xiaoming...</h1><span>Hi Xiaoming...</span>"
}
七、带内嵌图片邮件发送
// 图片占位写法 如果图片链接写入模板 注释下面这一行
helper.addInline("img01",new FileSystemResource(filePath));
###
POST http://localhost:8080/mail/inline/image
Content-Type: application/json{"tos": ["ly23580512@163.com"],"subject": "Hello Watcher Html images","content": "<h1>Hello</h1><p><img src=\"cid:img01\"></p>"
}