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“并发送"邮件系统"获取。

Hi,admin 如果你想 注销 ?                              

SpringBoot系列—简单的邮件系统的更多相关文章

  1. SpringBoot系列——利用系统环境变量与配置文件的分支选择实现“智能部署”

    前言 通过之前的博客:SpringBoot系列——jar包与war包的部署,我们已经知道了如果实现项目的简单部署,但项目部署的时候最烦的是什么?修改成发布环境对应的配置!数据库连接地址.Eureka注 ...

  2. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  3. Springboot 系列(九)使用 Spring JDBC 和 Druid 数据源监控

    前言 作为一名 Java 开发者,相信对 JDBC(Java Data Base Connectivity)是不会陌生的,JDBC作为 Java 基础内容,它提供了一种基准,据此可以构建更高级的工具和 ...

  4. SpringBoot系列——Spring-Data-JPA(究极进化版) 自动生成单表基础增、删、改、查接口

    前言 我们在之前的实现了springboot与data-jpa的增.删.改.查简单使用(请戳:SpringBoot系列——Spring-Data-JPA),并实现了升级版(请戳:SpringBoot系 ...

  5. SpringBoot系列——Logback日志,输出到文件以及实时输出到web页面

    前言 SpringBoot对所有内部日志使用通用日志记录,但保留底层日志实现.为Java Util Logging.Log4J2和Logback提供了默认配置.在不同的情况下,日志记录器都预先配置为使 ...

  6. SpringBoot系列——Security + Layui实现一套权限管理后台模板

    前言 Spring Security官网:https://spring.io/projects/spring-security Spring Security是一个功能强大且高度可定制的身份验证和访问 ...

  7. SpringBoot系列教程之Bean加载顺序之错误使用姿势辟谣

    在网上查询 Bean 的加载顺序时,看到了大量的文章中使用@Order注解的方式来控制 bean 的加载顺序,不知道写这些的博文的同学自己有没有实际的验证过,本文希望通过指出这些错误的使用姿势,让观文 ...

  8. SpringBoot系列教程之Bean之指定初始化顺序的若干姿势

    上一篇博文介绍了@Order注解的常见错误理解,它并不能指定 bean 的加载顺序,那么问题来了,如果我需要指定 bean 的加载顺序,那应该怎么办呢? 本文将介绍几种可行的方式来控制 bean 之间 ...

  9. Springboot 系列(十五)如何编写自己的 Springboot starter

    1. 前言 Springboot 中的自动配置确实方便,减少了我们开发上的复杂性,那么自动配置原理是什么呢?之前我也写过了一篇文章进行了分析. Springboot 系列(三)Spring Boot ...

随机推荐

  1. tomcat 在linux下启动时找不到JDK

    方案一.  修改bashrc (转载: https://www.cnblogs.com/hongzg1982/articles/2101792.html)  $ vim ~/.bashrc #加入JA ...

  2. Codeforces Round #563 (Div. 2) A-D

    A. Ehab Fails to Be Thanos 这个A题很简单,就是排个序,然后看前面n个数和后面的n个数是不是相同,相同就输出-1 #include <cstdio> #inclu ...

  3. LDheatmap | SNP连锁不平衡图(LD)可视化,自己数据实现版!

    本文首发于“生信补给站”,https://mp.weixin.qq.com/s/Gl6BChxSYbSHMo9oMpufPg 连锁不平衡图,用来可视化不同SNP之间的连锁程度,前同事间俗称“倒三角”图 ...

  4. Day_11【集合】扩展案例2_使用普通for循环获取集合中索引为3的元素并打印,统计集合中包含字符串"def"的数量,删除集合中的所有字符串",将集合中每个元素中的小写字母变成大写字母def",

    分析以下需求,并用代码实现 1.定义ArrayList集合,存入多个字符串"abc" "def" "efg" "def" ...

  5. nodejs开发准备工作(1)

    nvm工具(node版本管理工具) (1) 下载nvm: https://github.com/coreybutler/nvm-windows/releases: (2) 推荐下载压缩包,解压安装就好 ...

  6. QtCreator MSVC 搭建 Debugger

    QtCreatorForWindows搭建Debugger QtCreator for windows选择mingw或者msvc: qt-opensource-windows-x86-msvc2015 ...

  7. 手把手教你撸一套Redux(Redux源码解读)

    Redux 版本:3.7.2 Redux 是 JavaScript 状态容器,提供可预测化的状态管理. 说白了Redux就是一个数据存储工具,所以数据基础模型有get方法,set方法以及数据改变后通知 ...

  8. Git管理修改、撤销修改、删除文件

    什么是修改?比如你新增了一行,这就是一个修改,删除了一行,也是一个修改,更改了某些字符,也是一个修改,删了一些又加了一些,也是一个修改,甚至创建一个新文件,也算一个修改. a.管理修改 对于提交修改, ...

  9. 想要年薪百万,阿里Sentinel支持RESTful接口都搞不定?

    最近正准备用阿里Sentinel,发现RESTful接口支持的不是很好.有些童鞋可能对Sentinel不是很了解,我们先简单介绍一下. Sentinel简介 Sentinel是一套阿里巴巴开源的流量防 ...

  10. OpenStack黄金十年:我与OpenStack的故事

    导读:从2010年到2020年,OpenStack项目整整走过了十个春夏秋冬.不管是OpenStack基金会,还是积极参与OpenStack社区的厂商.企业乃至开发者,想必都有肺腑之言想对OpenSt ...