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 ...
随机推荐
- 【转载】Nginx多服务绑定80端口及映射域名
多服务绑定80端口及映射域名 说明:业务需要配置的样例模板,如需深入了解,请查看官方文档 1.Nginx配置文件nginx.conf(可拆分多台机器部署) worker_processes 1; e ...
- java中使用for循环删除List集合的陷阱
一直以为是数据库的数据取错了,导致后面for循环出错.慢慢调试之后,发现这原来是一个坑.回到正题 (错误示范:使用for循环删除list集合) for(int i=0;i<list.size() ...
- Thymeleaf模板引擎语法
th:text 用于显示值 th:object 接收后台传来的对象 th:action 提交表单 th:value 绑定值 th:field 绑定 ...
- CURL 实战下载
#include <string> #include <stdio.h> #include <iostream> #include<fstream> # ...
- ADB 关闭指定应用 并打开
import subprocess,time sjh="192.168.1.102:5555" aa1="adb -s {0} shell pm clear com.ku ...
- Linux从入门到进阶全集——【第十四集:Shell编程-export命令】
参考: https://www.cnblogs.com/guojun-junguo/p/9855356.html 功能说明:设置或显示环境变量. 语 法:export [-fnp][变量名称]=[变量 ...
- Java的标准日志
虽然开源社区有很多优秀的日志框架,但我们学习标准的java日志框架是为了更好的理解其他框架啊(近期项目要用ELK) 看自己以前写的Log4J简直不忍直视啊啊啊啊,那时还感觉自我良好 1. 为什么要使用 ...
- 程序向informix数据库插入text类型的中文乱码问题
部署应用的Linux服务器的编码格式为utf-8.但是数据库的编码格式为Gbk.所以导致的乱码.但是其他类型的数据中文并不乱码. 通过图片中修改编码格式,可以解决,导致的后果就是tomcat的日志输出 ...
- 字符串匹配算法(二)-BM算法详解
我们在字符串匹配算法(一)学习了BF算法和RK算法,那有没更加高效的字符串匹配算法呢.我们今天就来聊一聊BM算法. BM算法 我们把模式串和主串的匹配过程,可以看做是固定主串,然后模式串不断在往后滑动 ...
- Java中9种常见的CMS GC问题分析与解决
1. 写在前面 | 本文主要针对 Hotspot VM 中"CMS + ParNew"组合的一些使用场景进行总结.重点通过部分源码对根因进行分析以及对排查方法进行总结,排查过程会省 ...