Spring Boot入门——邮件发送
2024-08-26 05:46:04
1、引入依赖
<!-- mail依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2、参数配置
在application.properties中配置邮件相关的参数
spring.thymeleaf.cache=false spring.mail.host=smtp.qq.com
spring.mail.username=***@qq.com
spring.mail.password=ymwrdffauajebgde //此处的密码时qq邮箱的授权码
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.stattls.required=true
3、邮件Service代码
@Service
public class MailService { @Value("${spring.mail.username}")
private String from; @Autowired
private JavaMailSender sender; /*发送邮件的方法*/
public void sendSimple(String to, String title, String content){
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from); //发送者
message.setTo(to); //接受者
message.setSubject(title); //发送标题
message.setText(content); //发送内容
sender.send(message); System.out.println("邮件发送成功"); }
}
4、编写页面代码
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h1 th:inlines="text">邮件发送</h1>
<form action="sendMail" method="post">
<p>选择文件: <input type="text" name="title"/></p>
<p><input type="submit" value="提交"/></p>
</form>
</body>
</html>
5、邮件请求处理
@Controller
public class MailController {
@Autowired
private MailService mailService; private String to="***@qq.com"; @RequestMapping("mail")
public String mail(){
return "/mail";
} @RequestMapping("sendMail")
@ResponseBody
public String sendMail(@RequestParam("title")String title){
System.out.println("-----title: " + title);
mailService.sendSimple(to, title, title);
return "success";
}
}
6、测试
7、qq邮箱授权码
最新文章
- 《Linux常用命令》笔记
- [译]:Xamarin.Android平台功能——位置服务
- Leetcode 131. Palindrome Partitioning
- WPF 自定义Metro Style窗体
- Socket 通讯
- C#操作XML文档---基础
- Netsharp快速入门(之4) 基础档案(之C 实体建模 计量单位、商品、往来单位)
- Linux和Windows的换行符
- bootstrap初探
- 信号和槽有一个非常精炼的C++实现,作者是Sarah Thompson,该实现只有一个头文件sigslot.h,跨平台且线程安全
- 框架基础:ajax设计方案(三)--- 集成ajax上传技术 大文件/超大文件前端切割上传,后端进行重组
- Android 学习笔记之 Actionbar作为回到上一级
- 手淘的flexible.js解决手机适配问题
- Yii2整合AdminLTE后台主题
- TensorFlow官方文档
- Linux2:Linux目录结构
- [2017-7-26]Android Learning Day4
- linux环境下遇到的所有问题
- 产生10个随机数5-9之间 统计一个int类型的一维数组中有多少个在[min,max]之间的数
- MySQL Server and Server-Startup Programs
热门文章
- android菜鸟学习笔记28----Android中的Service生命周期及本地和远程服务绑定的实现
- JavaScript通过preventDefault()使input[type=text]禁止输入但保留光标
- 【22,23节】Django的GET和POST属性笔记
- centos7安装nodejs 和 yarn
- k8s 安装文档
- Java并发—线程池框架Executor总结(转载)
- 运行jupyter notebook 出错 Error executing Jupyter command &#39;notebook&#39;
- python数据之间的转换和关系
- Django 进阶篇之 Form验证
- css系列(6)css的运用(二)