SpringBoot配置发送Email

引入依赖

在 pom.xml 文件中引入邮件配置:

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

配置文件

# JavaMailSender 邮件发送的配置
spring.mail.host=smtp.163.com
spring.mail.username=用户163邮箱
spring.mail.password=邮箱密码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true

注意:若使用QQ邮箱发送邮件,则需要修改为spring.mail.host=smtp.qq.com,同时spring.mail.password改为QQ邮箱的授权码。
QQ邮箱->设置->账户->POP3/SMTP服务:开启服务后会获得QQ的授权码


但是真正运行程序时,还是会爆 535 认证失败。

解决方案:因为JDK1.8中jre\lib\security中两个 jar 包替换的缘故。将下载后的local_policy.jarUS_export_policy.jar替换到JDK1.8的jre\lib\security文件夹即可。
地址:http://www.oracle.com/technetwork/java/javase/downloads/jce-7-download-432124.html

发送简单文本邮件

贴 java 代码:

@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("163")
public class EmailTest { @Autowired
private JavaMailSender mailSender; //自动注入的Bean @Value("${spring.mail.username}")
private String Sender; //读取配置文件中的参数 @Test
public void sendSimpleMail() throws Exception {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(Sender);
message.setTo(Sender); //自己给自己发送邮件
message.setSubject("主题:简单邮件");
message.setText("测试邮件内容");
mailSender.send(message);
}
}

发送Html邮件

java 代码:

   @Test
public void sendHtmlMail() {
MimeMessage message = null;
try {
message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(Sender);
helper.setTo(Sender);
helper.setSubject("标题:发送Html内容"); StringBuffer sb = new StringBuffer();
sb.append("<h1>大标题-h1</h1>")
.append("<p style='color:#F00'>红色字</p>")
.append("<p style='text-align:right'>右对齐</p>");
helper.setText(sb.toString(), true);
} catch (Exception e) {
e.printStackTrace();
}
mailSender.send(message);
}

发送带附件的邮件

java 代码:

  @Test
public void sendAttachmentsMail() {
MimeMessage message = null;
try {
message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(Sender);
helper.setTo(Sender);
helper.setSubject("主题:带附件的邮件");
helper.setText("带附件的邮件内容");
//注意项目路径问题,自动补用项目路径
FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/picture.jpg"));
//加入邮件
helper.addAttachment("图片.jpg", file);
} catch (Exception e){
e.printStackTrace();
}
mailSender.send(message);
}

发送带静态资源的邮件

java 代码:

    @Test
public void sendInlineMail() {
MimeMessage message = null;
try {
message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(Sender);
helper.setTo(Sender);
helper.setSubject("主题:带静态资源的邮件");
//第二个参数指定发送的是HTML格式,同时cid:是固定的写法
helper.setText("<html><body>带静态资源的邮件内容 图片:<img src='cid:picture' /></body></html>", true); FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/picture.jpg"));
helper.addInline("picture",file);
} catch (Exception e){
e.printStackTrace();
}
mailSender.send(message);

发送模板邮件

在Spring Boot中也能使用模板引擎来实现模板化的邮件发送。关于模板邮件,SpringBoot 原本是支持 velocity,在 1.4 版本后又抛弃了 velocity,暂时只支持 freemaker。
引入 freemaker 依赖:

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

java 代码:

   @Autowired
private FreeMarkerConfigurer freeMarkerConfigurer; //自动注入 @Test
public void sendTemplateMail(){
MimeMessage message = null;
try {
message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(Sender);
helper.setTo(Sender);
helper.setSubject("主题:模板邮件"); Map<String, Object> model = new HashedMap();
model.put("username", "zggdczfr"); //修改 application.properties 文件中的读取路径
// FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
// configurer.setTemplateLoaderPath("classpath:templates");
//读取 html 模板
Template template = freeMarkerConfigurer.getConfiguration().getTemplate("mail.html");
String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
helper.setText(html, true);
} catch (Exception e) {
e.printStackTrace();
}
mailSender.send(message);
}

效果截图

spring boot 学习(十)SpringBoot配置发送Email的更多相关文章

  1. spring boot 学习(十四)SpringBoot+Redis+SpringSession缓存之实战

    SpringBoot + Redis +SpringSession 缓存之实战 前言 前几天,从师兄那儿了解到EhCache是进程内的缓存框架,虽然它已经提供了集群环境下的缓存同步策略,这种同步仍然需 ...

  2. Spring Boot 学习摘要--关于配置

    date: 2019-12-27 09:00:00 updated: 2019-12-30 13:20:00 Spring Boot 学习摘要--关于配置 学习教程来自:B站 尚硅谷 1. 关于配置 ...

  3. spring boot学习(十三)SpringBoot缓存(EhCache 2.x 篇)

    SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...

  4. spring boot学习(2) SpringBoot 项目属性配置

    第一节:项目内置属性 application.properties配置整个项目的,相当于以前的web.xml: 注意到上一节的访问HelloWorld时,项目路径也没有加:直接是http://loca ...

  5. spring boot 学习(五)SpringBoot+MyBatis(XML)+Druid

    SpringBoot+MyBatis(xml)+Druid 前言 springboot集成了springJDBC与JPA,但是没有集成mybatis,所以想要使用mybatis就要自己去集成. 主要是 ...

  6. spring boot学习(4) SpringBoot 之Spring Data Jpa 支持(1)

    第一节:Spring Data Jpa 简介 Spring-Data-Jpa JPA(Java Persistence API)定义了一系列对象持久化的标准,目前实现这一规范的产品有Hibernate ...

  7. Spring Boot学习一之配置类及自动配置

    一.配置类 1. 导入其他配置类 你不需要将所有的 @Configuration 放进一个单独的类, @Import 注解可以用来导入其他配置类.另外,你也可以使用 @ComponentScan 注解 ...

  8. spring boot 学习(十二)拦截器实现IP黑名单

    拦截器实现IP黑名单 前言 最近一直在搞 Hexo+GithubPage 搭建个人博客,所以没怎么进行 SpringBoot 的学习.所以今天就将上次的”?秒防刷新”进行了一番修改.上次是采用注解加拦 ...

  9. spring boot学习(3) SpringBoot 之MVC 支持

    第一节:@RequestMapping 配置url 映射   第二节:@Controller 处理http 请求 转发到一个页面,以前是转发到jsp页面,现在使用freemarker: 在pom.xm ...

随机推荐

  1. Nodejs -- 使用koa2搭建数据爬虫

    当前爬虫项目开发所需中间件: cheerio: 则能够对请求结果进行解析,解析方式和jquery的解析方式几乎完全相同 cheerio中文文档 开发参考node - cheerio模块 superag ...

  2. 小工具:使用Python自动生成MD风格链接

    很久之前我在Github上搞了一个LeetCode的仓库,但一直没怎么维护.最近发现自己刷了不少LC的题目了,想搬运到这个仓库上. 玩Github最重要的当然是写README了,MD的逼格决定了项目牛 ...

  3. C++ 项目中直接使用JsonCpp源码文件

    之前在网上看到使用JsonCpp都是以库的形式使用(编译源码为静态库或者动态库),这样引用很方便,但有时候报错调试看不到错误的地方,所以就想直接把源文件添加到项目中,方便调试 这是用到源码文件: 创建 ...

  4. Spring Boot + thymeleaf 实现文件上传下载

    参考博客:https://juejin.im/post/5a326dcaf265da431048685e 技术选型:spring boot 2.1.1+thymeleaf+maven 码云地址:htt ...

  5. Spring通过ApplicationContext主动获取bean

    有些场景无法通过AutoWired和compoment注解传递进来,于是希望通过Spring context主动去获取beandemo: package com.qhong.Util; import ...

  6. Educational Codeforces Round 27 A B C

    A. Chess Tourney   Berland annual chess tournament is coming! Organizers have gathered 2·n chess pla ...

  7. Visual Studio 项目模板制作(四)

    上一篇,介绍了VSIX安装模板的方法,那么,你是不是要问,为何有些项目模板却可以有向导,那是怎么做到的 今天这篇文章就是介绍如何为自己的模板添加向导,向导可以引导你完成项目中各种参数的设置,比如项目创 ...

  8. 利用Ajax和Servlet实现输入框提示功能

    目的和效果:     输入框输入字符串x,匹配后台传入的数据str,如果str中的元素包含字符串x,则提示. 后台代码:                   String x = request.ge ...

  9. python 时间元组转时间戳

    #!/usr/bin/python # -*- coding: UTF- -*- import time print(time.mktime((, , , , , , , , ))) 输出 15382 ...

  10. 使用rviz 查看远程主机

    一.安装好ros环境 https://www.cnblogs.com/sea-stream/p/9809590.html 二.配置参数 vim ~/.bashrc #输入内容 export ROS_H ...