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. vue element select多选回显

    我们经常在使用 Element组件里面的 select多选 场景:添加账号的时候需要选择可见分公司(分公司为多选),添加成功之后可以编辑,需要回显添加时所提交的分公司 代码如下: 多选框: data( ...

  2. CentOS安装jsoncpp

    两种安装方式: 通过cmake安装 通过scons安装 cmake安装见cmake安装jsoncpp,scons安装见下文. 1. 安装scons .tar.gz export MYSCONS=/ro ...

  3. Thinkphp 缓存RCE

     5.0.0<=ThinkPHP5<=5.0.10 .   漏洞利用条件: 1.基于tp5开发的代码中使用了Cache::set 进行缓存 2.在利用版本范围内 3.runtime目录可以 ...

  4. 王颖奇 20171010129《面向对象程序设计(java)》第十周学习总结

    实验十  泛型程序设计技术 实验时间 2018-11-1 1.实验目的与要求 (1) 理解泛型概念: (2) 掌握泛型类的定义与使用: (3) 掌握泛型方法的声明与使用: (4) 掌握泛型接口的定义与 ...

  5. js中刷新页面的方式总结

    1.window.onload / document.onload 2.history.go(num): (1)num为参数,num为正表示前进几个页面,类似于history.forward(): ( ...

  6. OpenCV Error: Unspecified Error(The Function is not implemented)

    Ubuntu 或者 Debian 系统显示窗口的时候遇到了这个问题 error: (-2:Unspecified error) The function is not implemented. Reb ...

  7. Linux设备子系统初始化

    本文介绍的内容是基于Linux3.1源码,并参考了很多网上找来的资料 Linux内核的启动的流程如下: start_kernel->rest_init->kernel_init->d ...

  8. 局域网ip地址扫描_v1版本

    局域网ip地址扫描 工作中,我们有时需要对局域网中ip地址使用情况进行统计.可以使用shell脚本进行扫. 脚本功能: 在线使用IP写入list_online.txt文件 未在线IP写入list_of ...

  9. REST模式中HTTP请求方法

    一直在测试REST模式的WEB SERVICE接口,客户端的HTTP的请求方式一般分为四种:GET.POST.PUT.DELETE,这四种请求方式有什么不同呢.简单的说,GET就是获取资源,POST就 ...

  10. Codeforces 1272E (Nearest Opposite Parity,反向建边)

    题意:给你n个数,每个数的值为a[i],每个点可以从i这号点跳转至(i - a[i]) 或 (i + a[i])点,点的范围为[1,n],然后问的是从偶数点跳至奇数点,从奇数点跳至偶数点的最少次数是多 ...