Spring Boot入门——邮件发送
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邮箱授权码


Spring Boot入门——邮件发送的更多相关文章
- Spring Boot整合邮件发送
概述 Spring Boot下面整合了邮件服务器,使用Spring Boot能够轻松实现邮件发送:整理下最近使用Spring Boot发送邮件和注意事项: Maven包依赖 <dependenc ...
- spring boot 入门(一)
转自构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程. ...
- Spring boot学习1 构建微服务:Spring boot 入门篇
Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...
- Spring boot 入门(四):集成 Shiro 实现登陆认证和权限管理
本文是接着上篇博客写的:Spring boot 入门(三):SpringBoot 集成结合 AdminLTE(Freemarker),利用 generate 自动生成代码,利用 DataTable 和 ...
- Springboot 系列(一)Spring Boot 入门篇
注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 由于 J2EE 的开发变得笨重,繁多的配置, ...
- Spring Boot入门 and Spring Boot与ActiveMQ整合
1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...
- Spring Boot 入门案例与配置说明
一.Spring Boot简介 官网地址:http://spring.io/projects/spring-boot Spring Boot可以轻松创建可以运行的独立的,生产级的基于Spring的应用 ...
- Spring Boot 入门之消息中间件篇(五)
原文地址:Spring Boot 入门之消息中间件篇(五) 博客地址:http://www.extlight.com 一.前言 在消息中间件中有 2 个重要的概念:消息代理和目的地.当消息发送者发送消 ...
- Spring Boot 入门之 Web 篇(二)
原文地址:Spring Boot 入门之 Web 篇(二) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Boot 入门之基础篇(一)>介绍了 ...
随机推荐
- RTLabel 富文本
本节关于RTLable基本介绍,原文来自 https://github.com/honcheng/RTLabel RTLabel 基于富文本的格式,适用于iOS,类似HTML的标记. RTLabel ...
- python系列十一:python3数据结构
#!/usr/bin/python #Python3 数据结构'''Python中列表是可变的,这是它区别于字符串和元组的最重要的特点,一句话概括即:列表可以修改,而字符串和元组不能.''' '''将 ...
- 理解CSS3属性transition
一.说明 1.1 定义和用法 transition 属性是一个简写属性,用于设置四个过渡属性: transition-property:规定设置过渡效果的CSS属性的名称. transition-du ...
- MySQL中InnoDB存储引擎中的哈希算法
InnoDB存储引擎使用哈希算法来对字典进行查找,其冲突机制采用链表方式,哈希函数采用除法散列方式.对于缓冲池页的哈希表来说,在缓冲池中的Page页都有一个chain指针.它指向相同哈希函数值的页的. ...
- CentOS 7.4系统优化/安装软件
源改为国内源 阿里云yum源 https://opsx.alibaba.com/mirror 清华yum源 https://mirrors.tuna.tsinghua.edu.cn/ 网易163yum ...
- A SELECT statement that assigns a value to a variable must ... (向变量赋值的 SELECT 语句不能与数据检索操作结合使用 )
A SELECT statement that assigns a value to a variable must ... (向变量赋值的 SELECT 语句不能与数据检索操作结合使用 ) 总结一句 ...
- win10笔记本触摸板手势大全
- Python基础-文件的基本操作
测试文件fansik内容如下:This is line 1This is line 2This is line 3This is line 4This is line 5This is line 6 ...
- shell方式切割tomcat日志
#!/bin/bash while true do cd /usr/local/tomcat/logs d=`date +%Y%m%d` d15=`date -d'15 day ago' +%Y%m% ...
- selenium之坑:点击后页面刷新重新获取刷新前的页面(StaleElementReferenceException:Message:Element not found in the cache...)
循环点击一列链接,但只能点到第一个,第二个失败,这是为什么,因为第二个已经是新页面,当然找不到之前页面的元素,虽然元素没变,甚至是后退回来,页面都没有变,为什么是新页面,页面长的一样不代表是同一张页面 ...