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入门——邮件发送的更多相关文章

  1. Spring Boot整合邮件发送

    概述 Spring Boot下面整合了邮件服务器,使用Spring Boot能够轻松实现邮件发送:整理下最近使用Spring Boot发送邮件和注意事项: Maven包依赖 <dependenc ...

  2. spring boot 入门(一)

    转自构建微服务:Spring boot 入门篇 什么是spring boot Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程. ...

  3. Spring boot学习1 构建微服务:Spring boot 入门篇

    Spring boot学习1 构建微服务:Spring boot 入门篇 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框 ...

  4. Spring boot 入门(四):集成 Shiro 实现登陆认证和权限管理

    本文是接着上篇博客写的:Spring boot 入门(三):SpringBoot 集成结合 AdminLTE(Freemarker),利用 generate 自动生成代码,利用 DataTable 和 ...

  5. Springboot 系列(一)Spring Boot 入门篇

    注意:本 Spring Boot 系列文章基于 Spring Boot 版本 v2.1.1.RELEASE 进行学习分析,版本不同可能会有细微差别. 前言 由于 J2EE 的开发变得笨重,繁多的配置, ...

  6. Spring Boot入门 and Spring Boot与ActiveMQ整合

    1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...

  7. Spring Boot 入门案例与配置说明

    一.Spring Boot简介 官网地址:http://spring.io/projects/spring-boot Spring Boot可以轻松创建可以运行的独立的,生产级的基于Spring的应用 ...

  8. Spring Boot 入门之消息中间件篇(五)

    原文地址:Spring Boot 入门之消息中间件篇(五) 博客地址:http://www.extlight.com 一.前言 在消息中间件中有 2 个重要的概念:消息代理和目的地.当消息发送者发送消 ...

  9. Spring Boot 入门之 Web 篇(二)

    原文地址:Spring Boot 入门之 Web 篇(二) 博客地址:http://www.extlight.com 一.前言 上一篇<Spring Boot 入门之基础篇(一)>介绍了 ...

随机推荐

  1. RedHat6/Centos6.5安装mongodb php driver

    条件: 安装apache 安装php 1.下载mongodb phh driver 下载地址:pecl wget http://pecl.php.net/get/mongo-1.5.8.tgz 2.解 ...

  2. 在VM虚拟机中安装Centos7操作系统(三)

    首先我们要下载  Centos https://www.centos.org/ 这个是Centos官方 最新版本 7 https://www.centos.org/download/ 提供有 DVD安 ...

  3. php安装pcntl扩展

    1.下载一个同样版本的php(我的是php5.6.27,我下的是php5.6.28) [root@CentOS6 lamp]# wget http://hk1.php.net/get/php-5.6. ...

  4. Android系统移植与调试之------->如何使用PhotoShop转换24位的bmp图片为16位bmp图片

    使用Android移植时候,很多图片都需要16为的bmp格式,所以研究了一下如何从24位转换成16位,供大家参阅 step1:查看bmp图片的属性,如下图所示,是24位的 step2:用PhotoSh ...

  5. win10笔记本触摸板手势大全

  6. python常用模块——random模块

    参考博客:http://www.360doc.com/content/14/0430/11/16044571_373443266.shtml 今天突然想起python该怎么生成随机数?查了一下,贴出实 ...

  7. loadrunder之脚本篇——Run-time Settings之Pacing

      As soon as the previous iteration ends 前一个迭代一结束就尽可能快的开始新一轮的迭代   After the previous iteration ends ...

  8. C# XMLHttpRequest对象—Ajax实例

    Get: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> ...

  9. React routerV4 笔记

    React routerV4 笔记     一.基础路由示例 import React from 'react' import { BrowserRouter as Router, Route, Li ...

  10. UVA11297 Census

    题目 UVA11297 Census 做法 二维线段树,单点修改,矩阵查询,树套树(\(x,y\)),维护最大值最小值废话 有一点要注意的是:\(x\)树传到\(y\)树里面修改的时候,如果\(x\) ...