邮件发送,在我们的日常开发中,也非常的多,Springboot也帮我们做了支持

  • 邮件发送需要引入spring-boot-start-mail

  • SpringBoot 自动配置MailSenderAutoConfiguration

  • 定义MailProperties内容,配置在application.yml中

  • 自动装配JavaMailSender

  • 测试邮件发送

1、引入pom依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2、查看自动配置类:MailSenderAutoConfiguration

@Configuration(
proxyBeanMethods = false
)
@ConditionalOnClass({MimeMessage.class, MimeType.class, MailSender.class})
@ConditionalOnMissingBean({MailSender.class})
@Conditional({MailSenderAutoConfiguration.MailSenderCondition.class})
@EnableConfigurationProperties({MailProperties.class})
@Import({MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class})
public class MailSenderAutoConfiguration {
public MailSenderAutoConfiguration() {
} static class MailSenderCondition extends AnyNestedCondition {
MailSenderCondition() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
} @ConditionalOnProperty(
prefix = "spring.mail",
name = {"jndi-name"}
)
static class JndiNameProperty {
JndiNameProperty() {
}
} @ConditionalOnProperty(
prefix = "spring.mail",
name = {"host"}
)
static class HostProperty {
HostProperty() {
}
}
}
}

然后我们去看下配置文件MailProperties

public class MailProperties {
private static final Charset DEFAULT_CHARSET;
private String host;
private Integer port;
private String username;
private String password;
private String protocol = "smtp";
private Charset defaultEncoding;
private Map<String, String> properties;
private String jndiName;
.....
}

MailProperties 中就是我们能自己去定义的属性。

3、配置文件:

spring.mail.username=1763204261@qq.com
spring.mail.password=qq邮箱授权码
spring.mail.host=smtp.qq.com
# qq需要配置ssl
spring.mail.properties.mail.smtp.ssl.enable=true

获取授权码:在QQ邮箱中的设置->账户->开启pop3和smtp服务

4、Spring单元测试

    @Autowired
JavaMailSenderImpl mailSender; @Test
public void emailTest(){
//邮件设置1:一个简单的邮件
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject("通知-你收到邮件了");
message.setText("神圣的时刻来了!"); message.setTo("1763204261@qq.com");
message.setFrom("1763204261@qq.com");
mailSender.send(message);
}

查看邮箱,邮件接收成功!

上面这个是比较简单的邮件发送,有时候会需要更复杂的,比如带入文件之类的:

此时需要用 MimeMessage

  @Autowired
JavaMailSenderImpl mailSender; @Test
public void contextLoads2() throws MessagingException {
//邮件设置2:一个复杂的邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setSubject("通知-收到文件邮件");
helper.setText("<b style='color:red'>又是神圣的时刻!</b>",true); //发送附件
helper.addAttachment("1.jpg",new File("G:\\1.jpg")); helper.setTo("1763204261@qq.com");
helper.setFrom("1763204261@qq.com"); mailSender.send(mimeMessage);
}

测试

和预想的一样!

SpringBoot-邮件任务的更多相关文章

  1. SpringBoot邮件报警

    SpringBoot邮件报警 一.介绍 邮件报警,大体思路就是收集服务器发生的异常发送到邮箱,做到服务器出问题第一时间知道,当然要是不关注邮箱当我没说 二.配置邮箱 (1).注册两个邮箱账号(一个用来 ...

  2. SpringBoot邮件发送

    这篇文章介绍springboot的邮件发送. 由于很简单就没有分出server和imp之类,只是在controller简单写个方法进行测试. 首先pom文件加入spring-boot-starter- ...

  3. (转)Springboot邮件服务

    springboot仍然在狂速发展,才五个多月没有关注,现在看官网已经到1.5.3.RELEASE版本了.准备慢慢在写写springboot相关的文章,本篇文章使用springboot最新版本1.5. ...

  4. springboot 邮件服务

    springboot仍然在狂速发展,才五个多月没有关注,现在看官网已经到1.5.3.RELEASE版本了.准备慢慢在写写springboot相关的文章,本篇文章使用springboot最新版本1.5. ...

  5. SpringBoot邮件推送功能

    鞠躬,道歉 抱歉,迟到了近一年的更新,这一年挺忙的,发生了很多事情,就厚脸皮拖更了,抱歉. 现在状态回来了,打算分享下近期学到的东西,这一年期间学到的东西可能会随意更新,其实也就是玩了下C# + un ...

  6. Springboot邮件发送思路分析

    毕业设计里需要邮件发送,所以学习,总的来讲,我考虑以下几点, 代码量少,代码简单.配置少,一看就懂,使用 JavaMail 太麻烦了. 异步执行,添加员工之后会发送入职邮件, 多线程处理,设计里有一个 ...

  7. springboot邮件发送与接收读取

    发送邮件 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  8. 19.springboot邮件服务服务器部署访问不到邮箱服务器解决方案

    1.前言 在Springboot项目的生产环境中,win系统环境下,邮箱服务是可以正常使用的. 当项目部署到阿里云服务器上之后,因为服务器端口采用安全组的方式,25端口访问不到. 在网上查找了一部分资 ...

  9. SpringBoot邮件发送功能

    快速入门 在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依赖: <dependency> <groupId>org.sp ...

  10. springboot 邮件

    <!-- 邮件end --><dependency> <groupId>org.springframework.boot</groupId> <a ...

随机推荐

  1. ES6——静态属性与静态方法

    静态方法只能写在class内,constructor外.通过static关键字声明 静态属性只能写在class外,通过 类名.属性名 = 属性值 声明 //静态属性与静态方法(ES6明确规定,Clas ...

  2. 实型(浮点型):float、double

    实型(浮点型):float.double 实型变量也可以称为浮点型,浮点型变量是用来存储小数数值的.在C语言中,浮点型分为两种:单精度浮点型(float).双精度浮点型(double),但是doubl ...

  3. Servlet学习笔记(二)之Servlet路径映射配置、Servlet接口、ServletConfig、ServletContext

    Servlet路径映射配置 要使Servlet对象正常的运行,需要进行适当的配置,以告诉Web容器哪个请求调用哪个Servlet对象处理,对Servlet起到一个注册的作用.Servlet的配置信息包 ...

  4. LVS实现(VS/DR)负载均衡和Keepalived高可用

    LVS是Linux Virtual Server的简写即Linux虚拟服务器,是一个虚拟的服务器集群系统一组服务器通过高速的局域网或者地理分布的广域网相互连接,在它们的前端有一个负载调度器(Load ...

  5. 简单明了的Java线程池

    线程池 线程池从功能上来看,就是一个任务管理器.在Java中,Executor接口是线程池的根接口,其中只包含一个方法: Executor void execute(Runnable command) ...

  6. 虚拟数字存储表——SQLServer2012可高用

    窗口函数之虚拟数字辅助表 数字辅助表是一个整数序列,可以用它来完成多种不同的查询任务.数字表有很多任务,如生成日期和时间值序列,及分裂值列表.通常,建议在数据库中保存这样一个永久表,并填充尽可能多的数 ...

  7. Docker 学习目录

    docker 概述 docker是什么 使用最广泛的肉开源容器引擎 其他如rocket,containerd,pouch等容器引擎 一种系统级虚拟化技术 传统的kvm xen,exsi,vmware ...

  8. HashSet的存储原理

    HashSet的底层用哈希散列表来存储对象(默认长度为16的数组),假如: Set set=new HashSet(); set.add(obj); 内部存储过程为:定义h=obj.hashCode, ...

  9. MySQL——select语句

    select: 基本语法:select 列名 或 * from 对象(表.视图...) where: = > < <> != like ----> like 'old%' ...

  10. MySQL(3)-日志

    3. InnoDB日志 3.1 InnoDB架构 分为 内存区域架构 buffer pool log buffer 磁盘区域架构 redo log undo log 2.1.1 内存区域架构 1)Bu ...