SpringBoot系列—简单的邮件系统
1. 效果发送效果图
连续发送了四封邮件:普通文本邮件,带附件的邮件,内容包含图片的邮件,发送html邮件。

普通文本邮件截图

带附件的邮件截图

内容包含图片的邮件截图(图片太大,就截取一部分)

发送html邮件截图

2. 邮件开发准备工作
- 引入pom文件依赖
<!-- 邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 在application.properties 中添加邮箱配置
spring.mail.host=smtp.qq.com
spring.mail.port=587
spring.mail.username=jackdking@foxmail.com
spring.mail.password=邮箱授权码,非邮箱登入密码
from,即为邮件发送者,一般设置在配置文件中
to,邮件接收者,此参数可以为数组,同时发送多人
subject,邮件主题
content,邮件的主体
邮件发送者 from 一般采用固定的形式写到配置文件中。
在qq邮箱中开启收发邮件步骤
进入邮件开启页面

点击开启,并发送短信

确认发送,邮件收发开启

3. springboot引入mail服务
- MailServiceImpl类注入邮件API类
/**
* @author jackdking
* @date 2018/5/3 22:07
*/
@Component
public class MailServiceImpl implements IMailService {
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String mailFrom;
......
- 4种邮件类型方法
/**
* 发送简单邮件
*
* @param to
* @param subject
* @param content
*/
@Override
public void sendSimpleEmail(String to,String subject,String content) {
SimpleMailMessage message = new SimpleMailMessage();
subject="简单文本邮件";
content="你好,我是空白";
to = "jackdking@foxmail.com";//我自己的邮箱
message.setFrom(mailFrom);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
mailSender.send(message);
}
/**
* 发送html邮件
*
* @param to
* @param subject
* @param content
*/
@Override
public void sendHtmlMail(String to, String subject, String content) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
try {
//true表示需要创建一个multipart message
subject="发送html邮件";
content="<html>\n" +
"<body>\n" +
" <h3>hello world !你好,我是空白 ,发送html邮件!</h3>\n" +
"</body>\n" +
"</html>";
to = "jackdking@foxmail.com";//我自己的邮箱
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(mailFrom);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
mailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}
/**
* 发送带附件的邮件
*
* @param to
* @param subject
* @param content
* @param filepath
*/
@Override
public void sendFileMail(String to, String subject, String content, String filepath) {
MimeMessage mimeMessage = mailSender.createMimeMessage();
subject="发送带附件的邮件";
content="你好,我是空白";
to = "jackdking@foxmail.com";//我自己的邮箱
filepath="D:\\微信图片_20200524230149.jpg";
try {
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
helper.setFrom(mailFrom);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content,true);
FileSystemResource file = new FileSystemResource(new File(filepath));
String fileName = filepath.substring(filepath.lastIndexOf(File.separator));
helper.addAttachment(fileName,file);
mailSender.send(mimeMessage);
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void sendPictureMail(String to, String subject, String content, String picturepath) {
// TODO Auto-generated method stub
String Id = "jackdking1314";
content="<html><body>图片邮件:<img src=\'cid:" + Id + "\' ></body></html>";
String imgPath = "D:\\微信图片_20200524230149.jpg";
to = "jackdking@foxmail.com";//我自己的邮箱
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(mailFrom);
helper.setTo(to);
helper.setSubject("这是有图片的邮件");
helper.setText(content, true);
FileSystemResource res = new FileSystemResource(new File(imgPath));
helper.addInline(Id, res);
mailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
4. 启动应用,开始4种邮件发送测试
- SpringbootMailApplication应用启动类实现了ApplicationRunner接口,应用启动成功就执行run方法,发送4种邮件。
@SpringBootApplication
public class SpringbootMailApplication implements ApplicationRunner
{
@Autowired
IMailService mailService;
public static void main( String[] args )
{
SpringApplication.run(SpringbootMailApplication.class, args);
}
//启动应用后直接发送邮件
@Override
public void run(ApplicationArguments args) throws Exception {
// TODO Auto-generated method stub
mailService.sendSimpleEmail(null, null, null);
mailService.sendHtmlMail(null, null, null);
mailService.sendFileMail(null, null, null,null);
mailService.sendPictureMail(null, null, null,null);
}
}
- 应用启动成功,并成功发送了4封邮件

转载这篇文章需要标注作者和出处:空白-bittechblog
完整的demo项目,请关注公众号“前沿科技bot“并发送"邮件系统"获取。

SpringBoot系列—简单的邮件系统的更多相关文章
- SpringBoot系列——利用系统环境变量与配置文件的分支选择实现“智能部署”
前言 通过之前的博客:SpringBoot系列——jar包与war包的部署,我们已经知道了如果实现项目的简单部署,但项目部署的时候最烦的是什么?修改成发布环境对应的配置!数据库连接地址.Eureka注 ...
- Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件
前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...
- Springboot 系列(九)使用 Spring JDBC 和 Druid 数据源监控
前言 作为一名 Java 开发者,相信对 JDBC(Java Data Base Connectivity)是不会陌生的,JDBC作为 Java 基础内容,它提供了一种基准,据此可以构建更高级的工具和 ...
- SpringBoot系列——Spring-Data-JPA(究极进化版) 自动生成单表基础增、删、改、查接口
前言 我们在之前的实现了springboot与data-jpa的增.删.改.查简单使用(请戳:SpringBoot系列——Spring-Data-JPA),并实现了升级版(请戳:SpringBoot系 ...
- SpringBoot系列——Logback日志,输出到文件以及实时输出到web页面
前言 SpringBoot对所有内部日志使用通用日志记录,但保留底层日志实现.为Java Util Logging.Log4J2和Logback提供了默认配置.在不同的情况下,日志记录器都预先配置为使 ...
- SpringBoot系列——Security + Layui实现一套权限管理后台模板
前言 Spring Security官网:https://spring.io/projects/spring-security Spring Security是一个功能强大且高度可定制的身份验证和访问 ...
- SpringBoot系列教程之Bean加载顺序之错误使用姿势辟谣
在网上查询 Bean 的加载顺序时,看到了大量的文章中使用@Order注解的方式来控制 bean 的加载顺序,不知道写这些的博文的同学自己有没有实际的验证过,本文希望通过指出这些错误的使用姿势,让观文 ...
- SpringBoot系列教程之Bean之指定初始化顺序的若干姿势
上一篇博文介绍了@Order注解的常见错误理解,它并不能指定 bean 的加载顺序,那么问题来了,如果我需要指定 bean 的加载顺序,那应该怎么办呢? 本文将介绍几种可行的方式来控制 bean 之间 ...
- Springboot 系列(十五)如何编写自己的 Springboot starter
1. 前言 Springboot 中的自动配置确实方便,减少了我们开发上的复杂性,那么自动配置原理是什么呢?之前我也写过了一篇文章进行了分析. Springboot 系列(三)Spring Boot ...
随机推荐
- mysql查询表内所有字段名和备注
select distinct column_name as 字段名,column_comment as 字段备注 from information_schema.columns where tabl ...
- 支付宝小程序serverless---插入数据后获取数据的主键_id(mongodb)
支付宝小程序serverless---插入数据后获取数据的主键_id(mongodb) 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除, ...
- qt creator源码全方面分析(4-3)
内外命名空间 QtCreator源码中,每一个子项目都有内外两层命名空间,一个是外部的,一个是内部的. 示例如下 namespace ExtensionSystem { namespace Inter ...
- 面试官:你说你懂动态代理,那你知道为什么JDK中的代理类都要继承Proxy吗?
之前我已经写过了关于动态代理的两篇文章,本来以为这块应该没啥问题,没想到今天又被难住了- 太难了!!! 之前文章的链接: 动态代理学习(一)自己动手模拟JDK动态代理. 动态代理学习(二)JDK动态代 ...
- Spring学习笔记(八)Spring Data JPA学习
jpa简单的命名规则如下,这个不多做介绍,放在这里也是给自己以后查找起来方便,这篇文章主要介绍之前一直忽略了的几个点,像@NoRepositoryBean这个注解,以及怎么自定义Repositor ...
- OpenCV 经纬法将鱼眼图像展开
文章目录 前言 理论部分 鱼眼展开流程 鱼眼标准坐标计算 标准坐标系与球坐标的转换 代码实现 测试效果如下图 总结 this demo on github 前言 鱼眼镜头相比传统的镜头,视角更广,采集 ...
- linux-Curl error (37): Couldn't read a file:// file for file:///etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-x86_64 [Couldn't open file /e tc/pki/rpm-gpg/RPM-GPG-KEY-fedora-x86_64]
在安装Python3-pip 的时候遇到 [root@localhost rpm-gpg]# yum install python3-pipFedora 31 - x86_64 - Updates - ...
- 黑马程序员_毕向东_Java基础视频教程——位运算练习(随笔)
位运算(练习) 最有效率的方式算出 2乘以 8等于几 2 << 3 = 2 * 2^3 = 2 * 8 = 16 对于两个整数变量的值进行互换(不需要第三方变量) class Test { ...
- .Net Core3.0 WebApi 项目框架搭建 五: 轻量型ORM+异步泛型仓储
.Net Core3.0 WebApi 项目框架搭建:目录 SqlSugar介绍 SqlSugar是国人开发者开发的一款基于.NET的ORM框架,是可以运行在.NET 4.+ & .NET C ...
- tomcat 添加 ssl 证书
1. 将证书提供方给的证书(server.crt)及密钥文件(server.key)上传到服务器 tomcat 的 conf 目录 2. 在tomcat conf 目录下执行如下命令 (1) 生成P1 ...