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 ...
随机推荐
- SpringCloud:Zuul路由配置超时问题
测试访问时长 修改下业务类,增加sleep休眠时长,以此查看Zuul的熔断 @GetMapping("/test1") public Object test1() { try { ...
- XCTF 进阶区 CAT
这题脑洞是真的大,讲道理 看到这个,先尝试了一下命令拼接,发现字符被过滤了应该.fuzz一下看看,有哪些字符还没被过滤了 import requests dictory=["!", ...
- js定时器中引用的外部函数如何传递参数
问题:比如在一个点击事件中我需要将点击事件参数event传入到定时器中,如果只是单纯的在setTimeout(timer(e),1000)中写上括弧e,则该定时器不会等到1s才执行,而是会立即执行.那 ...
- 视图:DBA_TAB_PARTITIONS 分区表视图
Column Datatype 释义 Description TABLE_OWNER VARCHAR2(128) 表的owner Owner of the table TABLE_NAME VARCH ...
- 记两道最近做的pwn题(ciscn_2019)
这两题为什么要记录呢,一个是我发现网上很多教程没写清楚(也可能是我太菜了),二是细节点很多,不同的大佬方式不太一样,有很多细节需要注意 ciscn_2019_es_2 这题是栈迁移的题,先上exp 1 ...
- Selenium自动化测试框架Ride使用XLRD对于Excel测试数据的管理和操作
Python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库. 一.安装xlrd模块 到python官网下载http://pypi.pytho ...
- Python Unittest简明教程
1 概述 单元测试框架是一种软件测试方法,通过来测试源代码中的各个单元,例如类,方法等,以确定它们是否符合要求.直观上来说,可以将单元视为最小的可测试部分.单元测试是程序员在开发过程中创建的短代码片段 ...
- WLS中Linux与Windows间的环境共享
Reference 更多cmd.exe帮助参考 (cmd_helps)[https://ss64.com/nt/cmd.html] (WSL备份,windows Docker安装)[https://w ...
- Python基础之tabview
以前写过界面,但是没有记录下来,以至于现在得从头学习一次,论做好笔记的重要性. 现在学习的是怎么写一个tabview出来,也就是用tkinter做一个界面切换的效果.参考链接:https://blog ...
- dotnet部署出现Failed to load the dll from [ ... hostfxr.dll], HRESULT: 0x80070057
起因 最近看到.net core 3支持wpf了,尝试一下(如果可行,会特别利于脱离.net运行时) dotnet new wpf dotnet publish -c Release -r win-x ...