Springboot 自动发送邮件
完成Springboot配置发件邮箱,自动给其他邮箱发送邮件功能
一、创建springboot基础项目,引入依赖
<!-- Spring Boot 邮件依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- Spring Boot 模板依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
二、配置文件添加必要的配置:
#qq邮箱为例
spring.mail.host=smtp.qq.com spring.mail.port=465 spring.mail.username=89666XXX@qq.com spring.mail.password=XXXXXX #不是邮件密码而是授权码(邮箱设置里获取) spring.mail.protocol=smtp spring.mail.test-connection=true spring.mail.default-encoding=UTF-8 spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.starttls.required=true spring.mail.properties.mail.smtp.ssl.enable=true spring.mail.properties.mail.display.sendmail=spring-boot-demo
三、工程结构

四、MailService接口
package com.example.mail.demo;
import javax.mail.MessagingException;
public interface MailService {
/**
* 发送文本邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param cc 抄送地址
*/
void sendSimpleMail(String to, String subject, String content, String... cc);
/**
* 发送HTML邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param cc 抄送地址
* @throws MessagingException 邮件发送异常
*/
void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException;
/**
* 发送带附件的邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param filePath 附件地址
* @param cc 抄送地址
* @throws MessagingException 邮件发送异常
*/
void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException;
}
五、MailServiceImpl实现
package com.example.mail.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File; @Service
public class MailServiceImpl implements MailService { @Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from; /**
* 发送文本邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param cc 抄送地址
*/
@Override
public void sendSimpleMail(String to, String subject, String content, String... cc) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
if (cc != null) {
message.setCc(cc);
}
mailSender.send(message);
} /**
* 发送HTML邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param cc 抄送地址
* @throws MessagingException 邮件发送异常
*/
@Override
public void sendHtmlMail(String to, String subject, String content, String... cc) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
if (cc != null) {
helper.setCc(cc);
}
mailSender.send(message);
} /**
* 发送带附件的邮件
*
* @param to 收件人地址
* @param subject 邮件主题
* @param content 邮件内容
* @param filePath 附件地址
* @param cc 抄送地址
* @throws MessagingException 邮件发送异常
*/
@Override
public void sendAttachmentsMail(String to, String subject, String content, String filePath, String... cc) throws MessagingException {
MimeMessage message = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content, true);
if (cc != null) {
helper.setCc(cc);
}
FileSystemResource file = new FileSystemResource(new File(filePath)); String fileName = new File(filePath).getName();
System.out.println("fileName:"+fileName);
helper.addAttachment(fileName, file); mailSender.send(message);
} }
六、EmailController控制类
package com.example.mail.demo; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
import javax.mail.MessagingException;
import java.io.File;
import java.net.URL; @Controller
public class EmailController { @Autowired
private MailService mailService; /**
* 测试发送简单邮件
*/
@GetMapping("/testSendSimpleMail")
@ResponseBody
public String sendSimpleMail() {
mailService.sendSimpleMail("2431886055@qq.com", "这是一封简单邮件", "这是一封普通的SpringBoot测试邮件");
return "ok";
} @Autowired
private TemplateEngine templateEngine; /**
* 发送html邮件
* @return
* @throws MessagingException
*/
@GetMapping("/sendHtmlMail")
@ResponseBody
public String sendHtmlMail() throws MessagingException {
Context context = new Context();
context.setVariable("project", "Spring Boot email");
context.setVariable("author", "万笑佛");
context.setVariable("url", "https://www.cnblogs.com/yclh/");
String emailTemplate = templateEngine.process("welcome", context);
mailService.sendHtmlMail("2431886055@qq.com", "这是一封模板HTML邮件", emailTemplate);
return "ok";
} /**
* 发送带附件的邮件
* @throws MessagingException
*/
@GetMapping("/sendAttachmentsMail")
@ResponseBody
public String sendAttachmentsMail() throws MessagingException {
URL url = null;
try {
// 创建一个文件对象
File file = new File("D:\\IDEANew\\45Mail\\src\\main\\resources\\static\\index.html");
// 将文件对象转换为URI
url = file.toURI().toURL();
// 输出URL
System.out.println("文件的URL: " + url);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(url.getPath());
mailService.sendAttachmentsMail("2431886055@qq.com", "这是一封带附件的邮件", "邮件中有附件,请注意查收!", url.getPath());
return "ok";
} }
七、postman测试
(1)第一个简单邮件

效果

(2)第二个带模板的邮件

效果

(3)第三个带附件的邮件

效果

Springboot 自动发送邮件的更多相关文章
- 【C#】新建服务自动发送邮件
---windows服务,---自动发送邮件 邮件发送code #region 发送邮件函数 public void SendMailUseZj() { System.Net.Mail.MailMes ...
- Jenkins配置自动发送邮件,成功!
Jenkins自动发送邮件配置: 打开"系统管理"--"系统设置" 在"Jenkins Location"设置系统管理员地址(重要:不能省略 ...
- python+selenium生成测试报告后自动发送邮件
标签(空格分隔): 自动化测试 运行自动化脚本后,会产生测试报告,而将测试报告自动发送给相关人员,能够让对方及时的了解测试情况,查看测试结果. 整个脚本包括三个部分: 生成测试报告 获取最新的测试报告 ...
- VBA控制outlook自动发送邮件(转)
使用Excel VBA实现Outlook自动发送邮件 | 在工作上我们都会遇到批量发送邮件的情况,面对重复而规律性的工作,可以使用Excel的VBA实现自动批量化发送邮件.大大减小工作时间,提升工作效 ...
- Jenkins进阶之自动发送邮件的Default Content设置模板
分享一个简洁实用的Jenkins项目邮件管理系统的"Default Content"设置模板 配置如下: <h1><center><font colo ...
- VBS 自动发送邮件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- python☞自动发送邮件
一.SMTP 协议 SMTP(Simple Mail Transfer Protocol)是简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式 二.smtplib ...
- jenkins配置自动发送邮件,抄送
1.安装插件.系统管理-安装插件:可选插件:搜索Email Extension 2.设置全局变量.系统管理-系统设置:a.Jenkins Location 设置发送方邮件--- b.Extended ...
- 【转】2、Jenkins构建完成自动发送邮件
1.开通163邮箱的授权码服务,和SMTP服务.百度找教程.2.安装 Email Extension Plugin 插件,已安装或版本自带可跳过此步骤.3.进入系统管理–系统设置首先配置 Jenkin ...
- SpringBoot自动配置源码调试
之前对SpringBoot的自动配置原理进行了较为详细的介绍(https://www.cnblogs.com/stm32stm32/p/10560933.html),接下来就对自动配置进行源码调试,探 ...
随机推荐
- pandas 删除重复项
使用如下函数: drop_duplicates 具体示例如下: import pandas as pd # 建立一个dataframe数据 df = pd.DataFrame({'k1':['one' ...
- pywintypes.com_error: (-2147418111, '被呼叫方拒绝接收呼叫。', None, None)
将打开的excel全部关闭,即可解决问题.
- 渗透-02:HTTPS主干-分支和HTTPS传输过程
一.HTTPS主干-分支 第一层 第一层,是主干的主干,加密通信就是双方都持有一个对称加密的秘钥,然后就可以安全通信了. 问题就是,无论这个最初的秘钥是由客户端传给服务端,还是服务端传给客户端,都是明 ...
- .NET5从零基础到精通:全面掌握.NET5开发技能【第二章】
章节: 第一章:https://www.cnblogs.com/kimiliucn/p/17613434.html 第二章:https://www.cnblogs.com/kimiliucn/p/17 ...
- 11、Spring之基于注解的AOP
11.1.环境搭建 创建名为spring_aop_annotation的新module,过程参考9.1节 11.1.1.配置打包方式和依赖 注意:AOP需要在IOC的基础上实现,因此需要导入IOC的依 ...
- 一个.NET 7 + DDD + CQRS +React+Vite的实战项目
项目简介 基于SignalR实现聊天通信,支持横向扩展,可支撑上万用户同时在线聊天 后端架构 后端技术栈采用 .NET 7 PostgreSQL (业务数据库) Redis(用于存放热点数据,和支持S ...
- 构建iOS交叉编译环境
要进行高级的iOS编程,我们需要很多工具链来帮我们完成这一目的 构建iOS交叉编译环境: 1.新建一个iphone交叉编译虚拟机 2. 为我们的虚拟机添加第二个网卡,设为host-only来达到能与宿 ...
- B2C在线教育商城--前后端分离部署
博客地址:https://www.cnblogs.com/zylyehuo/ 技术栈:vue + nginx + uwsgi + django + mariadb + redis 基本流程 vue打包 ...
- 1-MySQL数据库的安装和基础语法介绍
1.MySQL是什么? MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,属于 Oracle 旗下产品.它是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最 ...
- python爬虫——爬取天气预报信息
在本文中,我们将学习如何使用代理IP爬取天气预报信息.我们将使用 Python 编写程序,并使用 requests 和 BeautifulSoup 库来获取和解析 HTML.此外,我们还将使用代理服务 ...