需求介绍—发送邮件

首先要进行邮箱设置,要启用客户端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开发六-发送邮件的更多相关文章

  1. SpringBoot开发mockserver及生成swagger接口文档

    通过springboot开发mock server,包含get及post接口,用于练习接口自动化及jmeter很方便 当然,也为后面jenkins持续集成做基础(开发push代码后  → jenkin ...

  2. 基于SpringBoot开发一个Restful服务,实现增删改查功能

    前言 在去年的时候,在各种渠道中略微的了解了SpringBoot,在开发web项目的时候是如何的方便.快捷.但是当时并没有认真的去学习下,毕竟感觉自己在Struts和SpringMVC都用得不太熟练. ...

  3. SpringBoot(六):springboot热部署

    在j2ee项目开发中,热部署插件是JRebel.JRebel的使用为开发人员带来了极大的帮助,且挺高了开发便捷.而在SpringBoot开发生态环境中,SpringBoot热部署常用插件是:sprin ...

  4. SpringBoot开发案例之多任务并行+线程池处理

    前言 前几篇文章着重介绍了后端服务数据库和多线程并行处理优化,并示例了改造前后的伪代码逻辑.当然了,优化是无止境的,前人栽树后人乘凉.作为我们开发者来说,既然站在了巨人的肩膀上,就要写出更加优化的程序 ...

  5. SpringBoot开发案例从0到1构建分布式秒杀系统

    前言 ​最近,被推送了不少秒杀架构的文章,忙里偷闲自己也总结了一下互联网平台秒杀架构设计,当然也借鉴了不少同学的思路.俗话说,脱离案例讲架构都是耍流氓,最终使用SpringBoot模拟实现了部分秒杀场 ...

  6. 记一次SpringBoot 开发中所遇到的坑和解决方法

    记一次SpringBoot 开发中所遇到的坑和解决方法 mybatis返回Integer为0,自动转型出现空指针异常 当我们使用Integer去接受数据库中表的数据,如果返回的数据中为0,那么Inte ...

  7. 记账本微信小程序开发六

    记账本微信小程序开发六 我的界面 主界面

  8. SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清 ...

  9. SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清楚的认识到以下的问题,在实际的项目开发之中,尤其是 Java 的 MVC ...

随机推荐

  1. 6-x1 read命令:从键盘读取数据

    1.read的用法 read从 STDIN 读取一行数据并将其赋给一个变量,如果没有进行重定向,默认就是从键盘读取用户输入的数据:如果进行了重定向,那么可以从文件中读取数据. read 命令的用法为: ...

  2. Acunetix在SDLC中的安全性测试

    DevOps只是害怕尝试新事物.它们用于Selenium测试,这些测试占用了管道并提供了难以解释的结果,但是与此同时,它们经常避开了DAST测试,这远没有那么麻烦. 由于他们的应用程序是完全用Java ...

  3. Java | 循环的控制语句

    循环的控制语句 循环的控制语句有两种:break.continue 两种. braak可以用于强制限出循环. continue可以用于强制结束本次循环. break braak可以用于强制限出循环. ...

  4. Nginx的安装和部署

    Nginx简介 Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行.其特点是占有内存少,并发能力强,事实上nginx的并发 ...

  5. vmware使用U盘安装系统

    创建好系统 创建一个新的硬盘,选择"physicalDrive1" 如果识别不到physicalDrive 1,使用下面的方法. 1.在本机的服务里面启用下面的服务. 2.重启 V ...

  6. SHELL 变量一

    SHELL变量分为三类:本地变量.环境变量和位置参数 变量存在三种基本结构:变量名.操作符(个人定义).变量值 比如:var=blue 变量名:var 操作符:= 变量值:blue 变量的设置规则: ...

  7. PYTHON 解决ModuleNotFoundError: No module named 'win32com'

    d:\python37\scripts\>pip install pypiwin32

  8. C语言:FILE p和FILE *p

    FILE p和FILE *p大概可以这么理解:1 . 前一个p指文件型变量,后一个p指文件地址型变量.2 . 前一个p的内存地址已定,后一个p内存地址未定. 前一个是声明类对象,后一个是声明一个可指向 ...

  9. DEV-C++ 5.11调试设置方法

    DEV-C++调试设置方法:默认不能调试,打开调试的方法: 1.点击"工具"菜单--编译选项--"代码生成/优化"--连接器--"产生调试信息&quo ...

  10. MapReduce学习总结之Combiner、Partitioner、Jobhistory

    一.Combiner 在MapReduce编程模型中,在Mapper和Reducer之间有一个非常重要的组件,主要用于解决MR性能瓶颈问题 combiner其实属于优化方案,由于带宽限制,应该尽量ma ...