SpringBoot开发六-发送邮件
需求介绍—发送邮件
首先要进行邮箱设置,要启用客户端SMTP服务。
而且SpringBoot也给了JavaMailSender发送邮件。
代码实现
首先你需要设置好邮箱,步骤百度一大堆,记住要配置一个授权码,是需要在后续进行配置的password。
然后就是正式的来写了。
首先引入一个jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
然后在application.properties进行邮箱的配置
# MailProperties
spring.mail.host=smtp.sina.com
spring.mail.port=465
spring.mail.username=***
spring.mail.password=***
spring.mail.protocol=smtps
spring.mail.properties.smtp.auth=true
spring.mail.properties.mail.smtp.ssl.enable=true
那么这个邮箱发送的话我们就建一个工具类MailClient,实现能够复用。
package com.nowcoder.community.util; import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage; @Component
public class MailClient {
private static final Logger logger = LoggerFactory.getLogger(MailClient.class); @Autowired
private JavaMailSender mailSender; @Value("${spring.mail.username}")
private String from; /**
* @param to: 发送目标
* @param subject: 标题
* @param content: 内容
*/
public void sendMail(String to, String subject, String content) {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
// 允许支持html文件,默认是文本
helper.setText(content, true);
mailSender.send(helper.getMimeMessage());
} catch (MessagingException e) {
logger.error("发送邮件失败:" + e.getMessage());
}
} }
那么我们写一个测试类测试一下MailTests:
package com.nowcoder.community; import com.nowcoder.community.util.MailClient;
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.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context; @RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class MailTests { @Autowired
private MailClient mailClient; @Autowired
private TemplateEngine templateEngine; @Test
public void testTextMail() {
mailClient.sendMail("***@163.com", "TEST", "Welcome.");
} @Test
public void testHtmlMail() {
Context context = new Context();
context.setVariable("username", "sunday"); String content = templateEngine.process("/mail/demo", context);
System.out.println(content); mailClient.sendMail("***@163.com", "HTML", content);
} }
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>邮件实例</title>
</head>
<body>
<p>欢迎你,<span style="color:red;" th:text="${username}"></span>!</p>
</body>
</html>
SpringBoot开发六-发送邮件的更多相关文章
- SpringBoot开发mockserver及生成swagger接口文档
通过springboot开发mock server,包含get及post接口,用于练习接口自动化及jmeter很方便 当然,也为后面jenkins持续集成做基础(开发push代码后 → jenkin ...
- 基于SpringBoot开发一个Restful服务,实现增删改查功能
前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...
- SpringBoot(六):springboot热部署
在j2ee项目开发中,热部署插件是JRebel.JRebel的使用为开发人员带来了极大的帮助,且挺高了开发便捷.而在SpringBoot开发生态环境中,SpringBoot热部署常用插件是:sprin ...
- SpringBoot开发案例之多任务并行+线程池处理
前言 前几篇文章着重介绍了后端服务数据库和多线程并行处理优化,并示例了改造前后的伪代码逻辑.当然了,优化是无止境的,前人栽树后人乘凉.作为我们开发者来说,既然站在了巨人的肩膀上,就要写出更加优化的程序 ...
- SpringBoot开发案例从0到1构建分布式秒杀系统
前言 最近,被推送了不少秒杀架构的文章,忙里偷闲自己也总结了一下互联网平台秒杀架构设计,当然也借鉴了不少同学的思路.俗话说,脱离案例讲架构都是耍流氓,最终使用SpringBoot模拟实现了部分秒杀场 ...
- 记一次SpringBoot 开发中所遇到的坑和解决方法
记一次SpringBoot 开发中所遇到的坑和解决方法 mybatis返回Integer为0,自动转型出现空指针异常 当我们使用Integer去接受数据库中表的数据,如果返回的数据中为0,那么Inte ...
- 记账本微信小程序开发六
记账本微信小程序开发六 我的界面 主界面
- SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清 ...
- SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)
1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清楚的认识到以下的问题,在实际的项目开发之中,尤其是 Java 的 MVC ...
随机推荐
- zset如何解决内部链表查找效率低下
zset作为有序集合,内部基于跳表或者说索引的方式实现了数据的快速查找.解决了链表查询效率低下的痛点 前言 紧接前文我们学习了Redis中Hash结构.在里面我们梳理了字典这个重要的内部结构并分析了h ...
- Java:TreeMap中LinkedHashMap和Map中HashMap的区别
一般情况下,我们用的最多的是HashMap,在Map 中插入.删除和定位元素,HashMap 是最好的选择. 但如果您要bai按自然顺序或自定义顺序遍历键,那么TreeMap会更好.如果需要输出的顺序 ...
- Tomcat:启动tomcat服务报错没有权限
1.在linu上部署好tomcat后,准备启动时报错: Cannot find bin/catalina.sh The file is absent or does not have execute ...
- Mybatis学习(3)实现数据的增删改查
前面已经讲到用接口的方式编程.这种方式,要注意的一个地方就是.在User.xml 的配置文件中,mapper namespace="com.yihaomen.mybatis.inter.I ...
- elf文件结构解读以及plt节got节的理解
前言: 熟悉elf文件结构是一件很不错的事,因为安卓中的so加固以及修复都是需要这些知识的,包括pwn里面的rop之类的,也都是 和got节,plt节息息相关的,个人建议是在搞懂elf文件结构后,自己 ...
- java面试一日一题:字节java后端工程师面试题
今天来分享下字节一面面试题,各位小伙伴看看都能答上来吗,弄懂下面的问题你离字节又近了一步哦,加油吧 1.自我介绍: 2.问到项目中为什么选择hbase,如果有多个查询条件如何设置数据存储方案: 3.t ...
- centos安装svn,centos客户端运用svn
场景: 操作如下: 搭建svn服务器:192.168.43.130 1.安装subversion 2.创建本地仓库 mkdir /haha/svn/something svnadmin cre ...
- 序-WEB方向指南
WEB 这个方向其实是目前从业人员最多的方向,也是学习安全门槛最低的方向,当然也是最容易恰饭的方向. 我从入行到现在也依旧没有脱离它,毕竟在我这个小城市.小圈子里,不干这个好像就要没饭吃了,但是你说它 ...
- C语言:宏参数的字符串化和宏参数的连接
在宏定义中,有时还会用到#和##两个符号,它们能够对宏参数进行操作. # 的用法 #用来将宏参数转换为字符串,也就是在宏参数的开头和末尾添加引号.例如有如下宏定义: #define STR(s) #s ...
- bash shell 遍历一个数组
var[@] 数组的一个元素 var=("first" "second" "three") for str in ${var[@]}; d ...