怎么用Springboot实现发送邮件
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要介绍"怎么用Springboot实现发送邮件",在日常操作中,相信很多人在怎么用Springboot实现发送邮件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答
千家信息网最后更新 2025年11月07日怎么用Springboot实现发送邮件
这篇文章主要介绍"怎么用Springboot实现发送邮件",在日常操作中,相信很多人在怎么用Springboot实现发送邮件问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"怎么用Springboot实现发送邮件"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
具体内容如下
第一章 背景介绍
1.1 使用场景
1、注册验证;
2、网站营销;
3、安全的最后一道防线;
4、提醒、监控警告;
5、触发机制。
1.2 邮件发送原理
1.邮件传输协议:SMTP协议和POP3协议
2.内容不断发展:IMAP和Mme协议
1.3 邮件发送流程
第二章 使用SpringBoot完成邮件发送
2.1 开发流程
2.2 开发简单文本邮件
2.2.1 引入相关jar包
在pom.xml中添加依赖
org.springframework.boot spring-boot-starter-mail
2.2.2 配置邮箱参数
在配置文件里面配置:这里的密码是授权码,不是网页上的密码
spring.mail.host=smtp.163.comspring.mail.username=XXX@163.comspring.mail.password=XXXspring.mail.default-encoding=utf-8
2.2.3 封装SimpleMailMessage
SimpleMailMessage message = new SimpleMailMessage();
2.2.4 JavaMailSender进行发送
@Autowiredprivate JavaMailSender mailSender;//使用JavaMailSender发送邮件mailSender.send(message);
具体的实现:
/** * @Description: 发送邮件 * @Author: yzy * @Date: 2021/10/19 14:01 **/@Servicepublic class MailService { @Value("${spring.mail.username}") private String sendPeople; @Autowired private JavaMailSender mailSender; /** * @Description: 发送文本文件 * @author: yzy * @date: 2021/10/19 14:01 * @Param: * @return: */ public void sendSimpleMail(String to,String subject,String content) { SimpleMailMessage message = new SimpleMailMessage(); //接收方 message.setTo(to); //发送邮件的主题 message.setSubject(subject); //发送邮件内容 message.setText(content); //发送人 message.setFrom(sendPeople); //使用JavaMailSender发送邮件 mailSender.send(message); }}测试:
package com.yzy.restaurant.mapper;import com.yzy.restaurant.MailService;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.junit4.SpringRunner;@RunWith(SpringRunner.class)@SpringBootTestpublic class MailTest { @Autowired private MailService mailService; @Test public void sendSimpleMailTest() { mailService.sendSimpleMail("yzy20162362@163.com","这是一个简单的demo","哈哈哈,发送成功了!"); }}启动:
效果:

2.3 开发HTML邮件
上代码,在MailService 和MailTest 加
/** * @Description: 发送html邮寄 * @author: yzy * @date: 2021/10/19 14:58 * @Param: * @return: */ public void sendMailHtml(String to,String subject,String content) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message,true); helper.setTo(to); helper.setSubject(subject); helper.setText(content,true); helper.setFrom(sendPeople); mailSender.send(message); }/** * @Description: 发送html邮寄 * @author: yzy * @date: 2021/10/19 14:58 * @Param: * @return: */ public void sendMailHtml(String to,String subject,String content) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message,true); helper.setTo(to); helper.setSubject(subject); helper.setText(content,true); helper.setFrom(sendPeople); mailSender.send(message); }2.3 开发附件邮件
上代码,在MailService 和MailTest 加
/** * @Description: 发送附件邮件 * @author: yzy * @date: 2021/10/19 15:12 * @Param: * @return: */ public void sendAttachmentsMail(String to,String subject,String content,String filePath) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message,true); helper.setTo(to); helper.setSubject(subject); helper.setText(content,true); helper.setFrom(sendPeople); //读取 FileSystemResource file = new FileSystemResource(new File(filePath)); //获取文件名 String filename = file.getFilename(); //设置附件 helper.addAttachment(filename,file); //发送 mailSender.send(message); } @Test public void sendAttachmentsMailTest() throws MessagingException { String filePath = "D:/玖佳智能 2020年3月第3周周工作汇总(3月16-3月20日)(1).xlsx"; mailService.sendAttachmentsMail("yzy20162362@163.com","这是一封附件邮件","哈哈哈,附件邮件发送成功了",filePath); }2.4 图片邮件
上代码,在MailService 和MailTest 加
/** * @Description: 带图片邮件 * @author: yzy * @date: 2021/10/19 15:35 * @Param: * @return: */ public void sendPhotoMail(String to,String subject,String content,String rscPath, String rscId) throws MessagingException { MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message,true); helper.setTo(to); helper.setSubject(subject); helper.setText(content,true); helper.setFrom(sendPeople); //读取 FileSystemResource rec = new FileSystemResource(new File(rscPath)); helper.addInline(rscId,rec); //发送 mailSender.send(message);}@Test public void sendPhotoMailTest () throws MessagingException { String imgPath = "C:\\Users\\yzy\\Desktop\\微信图片_20210917201828.jpg"; String rsc = "0001"; mailService.sendPhotoMail("yzy20162362@163.com","这是一封图片邮件","哈哈哈,图片邮件发送成功了",imgPath,rsc); }2.5 邮件模板
模板邮件特别适用于:
1.用户注册的邮件;2.忘记密码的邮件
我用的是thymeleaf,前提是themleaf已经配置好,上代码
新建emailTemplate的页面:
邮件模板 你好,感谢您的注册,这是一封验证邮件,请点击下面的连接完成注册,感谢您的支持!
激活账户>
测试代码:
@Test public void sendTemplateMailTest () throws MessagingException { Context content = new Context(); content.setVariable("id","111"); String emailContent = templateEngine.process("emailTemplate", content); mailService.sendMailHtml("yzy20162362@163.com","这是一封模板邮件",emailContent); }到此,关于"怎么用Springboot实现发送邮件"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
邮件
图片
这是
附件
上代
模板
学习
开发
配置
成功
内容
密码
文件
文本
更多
流程
网站
帮助
测试
邮寄
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
windows邮件服务器搭建
多人服务器怎么进服
熟读网络安全管理办法并遵守
衢州多媒体视频系统服务器
全国poi信息数据库
软件开发编程招聘兼职
旁路监听 数据库
gRPC中心服务器
数据库表 限制存储数目
网络安全局保卫局
服务器管理员密码丢失
人力资源管理系统数据库实验报告
云服务器图形化管理工具
数据库技术学什么内容
linux主机服务器
ip网络技术命令range
SEO和软件开发哪个好
合肥轨道交通软件开发
amd服务器管理软件
广东视频会议服务器设备
jsp图片从数据库查询出来
护苗青少年网络安全教学视频
旅游商务软件开发
ios矢量软件开发
云锁网络安全
nba2k19ps4服务器关闭
ff14 飞机 数据库
国家医保数据库动态维护系统
服务器安全狗程序守护
太仓网络技术咨询服务电话