接下来测试邮件任务,在我们开发中也用的非常多,SpringBoot也加入了相关的场景支持,我们只需要引入spring-boot-starter-mail,加上一些少量的配置,我们就可以发送邮件,我们来测试一下,我们pom文件中引入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId>
</dependency>邮件的场景启动器我们在starter里面来查一下https://docs.spring.io/spring-boot/docs/1.5.22.RELEASE/reference/html/using-boot-build-systems.html
#using-boot-starterspring-boot-starter-mail我们把它引入,我们来看一下他的依赖,他引入了javax.mail,我们再来看他的自动配置,自动配置我们自然来到autoconfigure包下,来看跟邮件有关的配置,我们来找到mailorg.springframework.boot.autoconfigure.mail邮件配置这里有一个MailSenderAutoConfiguration,他下面给我们放了一个组件,叫JavaMailSenderImpl,这个组件是用来发送邮件的,但是邮件要的属性,@Bean
public JavaMailSenderImpl mailSender() {JavaMailSenderImpl sender = new JavaMailSenderImpl();if (this.session != null) {sender.setSession(this.session);}else {applyProperties(sender);}return sender;
}属性可以在MailProperties里面配置private final MailProperties properties;这个Properties里面能够配哪些属性,比如我们邮件的主机,/*** SMTP server host.*/
private String host;SMTP主机的地址,以及端口/*** SMTP server port.*/
private Integer port;包括用户名/*** Login user of the SMTP server.*/
private String username;密码/*** Login password of the SMTP server.*/
private String password;这些邮件发送的相关配置,特别说邮件发送

比如zhangsan@qq要给lisi@163发邮件,不是说他直接跟他交互的,而是说zhangsan@qq,先登录他自己的邮箱服务器,就要用账号密码来登陆,我们要配账号密码,登陆进来以后呢,以这个账号为名,来给lisi发邮件,lisi发邮件呢,是QQ邮箱服务器,把zhangsan邮件发送给163服务器,lisi上线以后呢再从163服务器,获取他的邮件,是这么一个流程,所以我们邮件发送需要配置的是,我们发件人的用户名,密码,以及他所在的服务器的地址,我就以QQ邮箱给163发送为例,我这儿有QQ邮箱,然后我们要给163发邮件,那我得来配登陆进163邮箱,我在这来配置一下,这个密码大家注意,QQ邮箱,网易等等,为了安全性起见,填的不是你QQ的密码,需要来到邮箱设置里面,账户里面,首先大家要来开通这些服务,POP3,SMTP,开通了以后呢,这里有第三方登陆邮箱,要有一个授权码,你要使用邮箱,你就可以来使用授权码,这样就能保证安全,我在这填一个授权码nomhhyejwyztdcdcspring.mail.username=306758218788@qq.com
spring.mail.password=nomhhyejwyztdcdc
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true需要你你手机绑定来发短信,这个授权码就是我们要记住的,我们来把这个授权码填上,包括我们还要来填地址,mail.host我们的主机地址,主机地址填什么呢,填我们smtp服务器的地址,如何收发邮件,smtp.qq.com,我们的QQ服务器,配置我们就做完了,我们在单元测试里面来测试一下,测试只要注,邮件发送器就自动注入进来,这怎么发送邮件呢,mailSender有一个方法,有一个叫send发送邮件,能传Mime类型的邮件,能传简单的邮件,我先来以简单的邮件为例,我来new一个SenderMailMessage,先来创建一个简单邮件,这个简单邮件我把它传递过来,这就发送了,邮件设置,比如邮件的标题了,宝库偶邮件的内容,setTo发给谁,包括这个邮件是谁发的,发现这会有报错,报错的原因我们来看一下,530,他说需要一个安全的连接,比如SSL,我们连接QQ邮箱的话,是需要安全连接的,我们得需要额外配一些东西,配置什么呢,我们来到MailProperties里面,额外的配置都写在mail.properties里面,以key value的方式,我们来开启sslspring.mail.properties.mail.smtp.ssl.enable=true然后我们再来测试,我们看现在能不能发送成功,我们发现这一块邮件已经发送成功,在未读邮件里面就有一个今晚开会
package com.learn.springboot;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootTaskApplicationTests {@AutowiredJavaMailSenderImpl mailSender;@Testpublic void contextLoads() {SimpleMailMessage message = new SimpleMailMessage();message.setSubject("通知-今晚开会");message.setText("今晚7:30开会");message.setTo("870116654@qq.com");message.setFrom("306758218788@qq.com");mailSender.send(message);}
}
我们这个邮件就发出来了,没问题,我们测试的这个呢,只是一个简单邮件发送,我们如何发送一个带附件的,或者一些HTML页面的复杂邮件呢,我们在这来测试一下,这个也比较简单,我们测在第二个方法里面,简单邮件叫SimpleMailMessage,而复杂邮件要怎么做呢,我们首先第一步,创建一个复杂的消息邮件,怎么创建呢,利用mailSender.createMimeMessage,创建出来,这个MimeMessage呢,就是我们最终要发送的消息,我们要给他设置一些内容,但是直接用它来设置是不行的,他没有这个方法的支持,那我们这块要怎么设置呢,/*** Create a new MimeMessageHelper for the given MimeMessage,* in multipart mode (supporting alternative texts, inline* elements and attachments) if requested.* <p>Consider using the MimeMessageHelper constructor that* takes a multipartMode argument to choose a specific multipart* mode other than MULTIPART_MODE_MIXED_RELATED.* <p>The character encoding for the message will be taken from* the passed-in MimeMessage object, if carried there. Else,* JavaMail's default encoding will be used.* @param mimeMessage MimeMessage to work on* @param multipart whether to create a multipart message that* supports alternative texts, inline elements and attachments* (corresponds to MULTIPART_MODE_MIXED_RELATED)* @throws MessagingException if multipart creation failed* @see #MimeMessageHelper(javax.mail.internet.MimeMessage, int)* @see #getDefaultEncoding(javax.mail.internet.MimeMessage)* @see JavaMailSenderImpl#setDefaultEncoding*/
public MimeMessageHelper(MimeMessage mimeMessage, boolean multipart) throws MessagingException {this(mimeMessage, multipart, null);
}如果我们要上传文件,我们就写成multipart,拿到helper以后,我们使用helper来设置内容,红色报错有异常,我还可以把内容设置成HTML片段,加粗的一段文字,就能兼容HTML片段了,比如我要上传一些文件,那要怎么办呢,我们还能用helper来上传文件,上传咱们的附件,比如多上传几个文件,/*** Set the given text directly as content in non-multipart mode* or as default body part in multipart mode.* Always applies the default content type "text/plain".* <p><b>NOTE:</b> Invoke {@link #addInline} <i>after</i> {@code setText};* else, mail readers might not be able to resolve inline references correctly.* @param text the text for the message* @throws MessagingException in case of errors*/
public void setText(String text) throws MessagingException {setText(text, false);
}这里还有一个属性,html,默认是false的,我们把它设为true,我写的这段内容是html,我们来重新发送
package com.learn.springboot;import java.io.File;import javax.mail.internet.MimeMessage;import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootTaskApplicationTests {@AutowiredJavaMailSenderImpl mailSender;@Testpublic void contextLoads() {SimpleMailMessage message = new SimpleMailMessage();message.setSubject("通知-今晚开会");message.setText("今晚7:30开会");message.setTo("870116654@qq.com");message.setFrom("306758218788@qq.com");mailSender.send(message);}@Testpublic void test02() throws Exception {// 创建一个复杂的消息邮件MimeMessage mimeMessage = mailSender.createMimeMessage();MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);// 邮件设置helper.setSubject("通知-今晚开会");helper.setText("<b style='color:red'>今晚7:30开会</b>",true);helper.setTo("870116654@qq.com");helper.setFrom("306758218788@qq.com");// 上传文件helper.addAttachment("1.jpg", new File("C:\\Users\\Leon.sun\\Desktop\\Temp\\image\\1.jpg"));helper.addAttachment("2.jpg", new File("C:\\Users\\Leon.sun\\Desktop\\Temp\\image\\2.jpg"));mailSender.send(mimeMessage);}
}

SpringBoot高级-任务-邮件任务相关推荐

  1. SpringBoot高级特性

    SpringBoot高级特性 SpringBoot缓存 基本环境搭建 导入数据库文件,创建出 department 和 employee 数据表 创建 JavaBean 封装数据 整合 Mybatis ...

  2. springboot高级——消息队列相关

    写在前边:本文学习尚硅谷的springboot高级整理笔记. 消息队列是什么,有什么好处? 我们可以把消息队列比作是一个存放消息的容器,当我们需要使用消息的时候可以取出消息供自己使用.消息队列是分布式 ...

  3. springboot发送qq邮件

    springboot发送qq邮件 1_开启邮箱相关权限并获取邮箱授权码 2_实现功能 2.1_添加mail的依赖 2.1.1_创建工程时添加 2.1.2_在工程中添加 2.2_配置文件applicat ...

  4. SpringBoot发送QQ邮件图片显示不出来

    SpringBoot发送QQ邮件图片显示不出来 用springBoot给QQ邮箱发送带图片的邮件时,图片在电脑上显示不出来,但是从手机上可以看到,查了一些方法,好多说在邮件上有个腾讯的黄条提示,点击显 ...

  5. 【SpringBoot高级篇】springboot实现上传docdocx文件格式转html在线预览v2.0

    [SpringBoot高级篇]springboot实现上传doc&docx文件格式转html在线预览v2.0 pom 上传路径工具类 SpringMvc虚拟路径映射 doc转html工具类 d ...

  6. SpringBoot整合Email 邮件发送

    文章目录 一.准备 二.邮件发送需要的配置 获取163邮箱授权码 获取QQ邮箱授权码 YML配置 三.代码编写 普通邮件发送 HTML邮件发送 含静态资源邮件发送 带附件邮件发送 SpringBoot ...

  7. 【SpringBoot高级篇】SpringBoot集成Elasticsearch搜索引擎

    [SpringBoot高级篇]SpringBoot集成Elasticsearch搜索引擎 1. 什么是Elasticsearch? 2. 安装并运行Elasticsearch 2.1 拉取镜像 2.2 ...

  8. SpringBoot集成163邮件发送详细配置,从163邮箱开始配置

    SpringBoot集成163邮件发送详细配置,从163邮箱开始配置 1.登录163邮箱 2.配置163邮箱 3.开始编写SpringBoot代码 1.创建SpringBoot项目然后引入依赖 2.编 ...

  9. SpringBoot +RabbitMQ实现邮件发送

    SpringBoot +RabbitMQ实现邮件发送 新建一个模块mail(与服务端分开来) 导入依赖 <!--rabbitmq 依赖--><dependency><gr ...

最新文章

  1. 余弦相似和内积的意义? 区别
  2. Linux C编程--进程介绍5--system函数
  3. 什么是光纤的波长?看看有哪些是你不知道的!
  4. 安装后改中文界面_非常详尽图文KVM安装CentOS
  5. RMAN 的备份保留策略
  6. PAT 1003 Emergency
  7. 给Fedora11安装五笔
  8. Transforming Cooling Optimization for Green Data Center via Deep Reinforcement Learning 笔记
  9. html邮件格式完整规范,邮件模板css及html设计规范
  10. win10终端中如何切换磁盘
  11. lis =[2,3,'k',['qwe',20,['k1',['tt',3,'1']],89],'ab','adv'] 将列表lis中的'tt'变成大写(用两种方式)。...
  12. Chelly个人训练
  13. 问题 J: Frosting on the Cake
  14. 联邦学习首次被纳入Gartner隐私计算技术成熟度曲线
  15. Rate Limiting速率限制
  16. 顶配梧桐树金玉满堂增额终身寿险,对抗“资产荒”的高增长神器
  17. cannot find method ‘value‘
  18. gvdp哪个工厂用_和汽车主机厂打交道,你不可不知这些英文缩写!实用!大伙速览速记!...
  19. 制作简单大气的倒计时时钟(带源码)js+css+html
  20. 杆单元四节点MATLAB,平面四节点等参单元matlab实现

热门文章

  1. HDU 4588 Count The Carries 数学
  2. inotify监听文件夹的变动
  3. 数字图像处理之点运算
  4. 【struts2】struts2配置文件—struts.properties
  5. Re: 从零开始的【comic spider】(序幕)
  6. 参观北京移动信息港森华易腾机房
  7. NSubstitute完全手册(八)替换返回值
  8. c 语言登录系统源代码,c语言源代码---------------个人图书管理系统
  9. 集合(Collection和Map)
  10. vue检测对象值_Vue 不能检测到对象属性的添加或删除,注意!!!