spring boot实现发送邮件功能
一、发送text邮件
1. 导入jar包
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId><version>2.6.2</version>
</dependency>
2. 邮箱参数配置(以新浪邮箱为例)
3. 使用JavaMailSender发送邮件
配置文件application.properties
# MailProperties
spring.mail.host=smtp.sina.com
spring.mail.port=465
spring.mail.username=xxxxxx@sina.com # 邮箱账号
spring.mail.password=xxxxxxxxxx # 开通pop3/smtp服务时所给的授权码
spring.mail..protocol=smtps
spring.mail.properties.mail.smtp.ssl.enable=true
MailClient.java
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.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.mail.javamail.JavaMailSender;import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;@Component
public class MailClient {private static final Logger logger = LoggerFactory.getLogger(MailClient.class);@Autowiredprivate JavaMailSender mailSender;@Value("${spring.mail.username}")private String from;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);helper.setText(content,true);mailSender.send(helper.getMimeMessage());} catch (MessagingException e) {logger.error("发送邮件失败"+e.getMessage());}}
}
测试MailTest.java
import com.zx.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;@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(classes = CommunityApplication.class)
public class MailTest {@Autowiredprivate MailClient mailClient;@Testpublic void testTextMail(){mailClient.sendMail("xxxxxx@sina.com","TEST","hello,xxxxxx.");}
}
二、利用thymeleaf模板发送html邮件
1. 新建模板demo.html
<!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>
2. 测试 MailTest.java
import com.zx.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 MailTest {@Autowiredprivate MailClient mailClient;@Autowiredprivate TemplateEngine templateEngine;@Testpublic void testHtmlMail(){Context context = new Context();context.setVariable("username","sunday");String content = templateEngine.process("/mail/demo",context); //模板存放的位置System.out.println(content);mailClient.sendMail("xxxxxq@sina.com","HTML",content);}
}
spring boot实现发送邮件功能相关推荐
- Spring Boot 之发送邮件
目录 Spring Boot 之发送邮件 简介 API 配置 实战 完整示例 引申和引用 Spring Boot 之发送邮件 简介 API 配置 实战 引入依赖 配置邮件属性 Java 代码 完整示例 ...
- spring boot实现发送邮件以及群发邮件
spring boot实现发送邮件以及群发邮件 2021年上班第一天,老板就让我写一个功能,要求将项目中的设备在线情况,发送邮件到老板和甲方老板的邮箱里,然后我花了正好一天的时间,将功能写了出来,如下 ...
- Spring Boot 2发送邮件手把手图文教程
本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本. 最近有童鞋问到笔者如何用Spring Boot发送邮件,故而整理下Spring Boot发送邮件的各种姿 ...
- Spring Boot实现验证码功能
✨Spring Boot实现验证码功能 验证码的作用 案例要求 前端页面准备 随机验证码工具类 后端控制器 测试
- 邮件发送类_10 分钟实现 Spring Boot 发生邮件功能
基础知识 什么是SMTP? 什么是IMAP? 什么是POP3? IMAP和POP3协议有什么不同呢? 进阶知识 什么是JavaMailSender和JavaMailSenderImpl? 如何通过Ja ...
- 关于最近很火的给对象的公众号之java spring boot 定时发送邮件教学
最近给女朋友的接口测试号很流行,但是需要商家或者企业资质,但是我们可以通过邮箱实现相同的功能.大致效果如下(源代码底部): 话不多说,直接上教程,首先新建spring boot项目,这个过程不复杂我就 ...
- Spring Boot使用缓存功能
缓存的好处不言而喻,比如查询商品的价格,如果可以放到缓存中,而不用每次都到数据库中查询,这将会大大提升系统性能,因为和缓存交互比访问数据库要快很多.或者在缓存中存放临时数据,而不用放到数据库中. 在学 ...
- 具有Spring Boot和数据功能的Java头优先弹性搜索
在本文中,我将为您提供有关如何在Java项目中使用Elastic Search的简单介绍. 由于Spring Boot是开始我们项目的最简单,最快的方法,因此我选择使用它. 此外,我们将大量使用心爱的 ...
- Spring Boot学习笔记:Spring Boot的Web功能
文章目录 一.Spring Boot的Web支持 二.Thymeleaf模板引擎 (一)Thymeleaf基础知识 1.引入Thymeleaf 2.访问Model数据 3.Model中的数据迭代 4. ...
最新文章
- 第三章:3.8 周期信号的傅里叶变换
- 数据结构--百度百科
- java中接口适配器实现,12.1.8 Java中的应用-AWT事件适配器(接口的适配器模式)...
- windows10如何删除文件时提示?(回收站--右键属性--显示删除确认对话框)
- big sur 关闭sip_青岛市第二批拟关闭退出化工生产企业公示
- 基于马克思哲学原理论外在美与内在美2017-12-31
- Java中List for循环的6种写法总结(推荐)(亲测)
- 如何删除第一张单页_单页网站-网站建设中独有的风景线
- java6虚拟机_Java 虚拟机之六:javap工具
- html强制文档模式ie8,html5 – IE9:强制IE9浏览器模式和文档模式
- 第一次面试总结--中国电子科学研究院
- 阶段5 3.微服务项目【学成在线】_day04 页面静态化_10-freemarker静态化测试-基于模板文件静态化...
- 使用 GNU CC 的预编译头文件加快编译速度
- 【图像融合】基于matlab GUI小波变换图像融合(带面板)【含Matlab源码 700期】
- 生化环材四大天坑劝退指南
- 微星B450mMortar迫击炮+AMD速龙3000GE安装Windows7 SP1并使用UEFI+Nvme启动
- 移动端兼容如: 背景图片、页面放大、卡顿、闪退、内阴影
- 一文搞定Centos7.x安装ELK的7.6.2版本以及Cerebro集群监控
- 如何检查MacBook 电池的循环使用次数及健康度?电池续航的方法
- go Benchmark的运行