Spring boot 中发送邮件
参考:https://blog.csdn.net/qq_39241443/article/details/81293939
添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
添加配置:邮箱不同配置不同
spring:
mail:
host: smtp.163.com
username: 15217742393@163.com
password: 授权码
default-encoding: UTF-8
发送简单文本:
package com.wct.send; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component; @Component
public class SendTextMail{ @Autowired
private JavaMailSender mailSender; @Value("${spring.mail.username}")
private String from; public void sendTextMail(String to,String subject,String content) throws Exception{
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from); message.setTo(to);
message.setSubject(subject);
message.setText(content); mailSender.send(message);
}
}
发送HTML :
package com.wct.send; import javax.mail.internet.MimeMessage; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component; @Component
public class SendHtmlMail { @Autowired
private JavaMailSender mailSender; @Value("${spring.mail.username}")
private String from; public void sendHtmlMail(String to,String subject,String content) throws Exception{
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper send = new MimeMessageHelper(message,true); send.setFrom(from);
send.setTo(to);
send.setSubject(subject);
send.setText(content,true); mailSender.send(message);
} }
测试类:
package com.wct.controllers; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import com.wct.send.SendHtmlMail;
import com.wct.send.SendTextMail; @RestController
public class SendController { @Autowired
private SendTextMail sendTextMail; @Autowired
private SendHtmlMail sendHtmlMail; private static String TO = "15217742393@163.com";
private static String SUBJECT = "主题文件";
private static String CONTENT = "this is a mail !";
private static String Html = "<a href=\"www.baidu.com\">超链接!</a>"; @GetMapping("/sendText")
public String send01() throws Exception{
sendTextMail.sendTextMail(TO,SUBJECT,CONTENT);
return "success";
} @GetMapping("/sendHtml")
public String send02() throws Exception{
sendHtmlMail.sendHtmlMail(TO,SUBJECT,Html);
return "success";
} }
Spring boot 中发送邮件的更多相关文章
- Spring Boot中使用JavaMailSender发送邮件
相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看 ...
- 56. spring boot中使用@Async实现异步调用【从零开始学Spring Boot】
什么是"异步调用"? "异步调用"对应的是"同步调用",同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执 ...
- 46. Spring Boot中使用AOP统一处理Web请求日志
在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...
- Spring Boot 2发送邮件手把手图文教程
原文:http://www.itmuch.com/spring-boot/send-email/ 本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本. 最近有 ...
- spring boot(三):Spring Boot中Redis的使用
spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...
- Spring Boot中的事务管理
原文 http://blog.didispace.com/springboottransactional/ 什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合 ...
- Spring Boot中的注解
文章来源:http://www.tuicool.com/articles/bQnMra 在Spring Boot中几乎可以完全弃用xml配置文件,本文的主题是分析常用的注解. Spring最开始是为了 ...
- 在Spring Boot中使用Https
本文介绍如何在Spring Boot中,使用Https提供服务,并将Http请求自动重定向到Https. Https证书 巧妇难为无米之炊,开始的开始,要先取得Https证书.你可以向证书机构申请证书 ...
- Spring Boot中使用Swagger2构建强大的RESTful API文档
由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...
随机推荐
- 基于Robot Framework的接口自动化测试
Robot Framework框架简介 Robot Framework框架是一个通用的验收测试和验收测试驱动开发的自动化测试框架(ATDD),使用的是关键字驱动的测试方法.它本身拥有强大的标准库,此外 ...
- app内区域截图利用html2Canvals保存到手机 截屏 (html2Canvals2版本1.0.0)
html2canvas($(], {scale:,logging:false,useCORS:true}).then(function(canvas) { var dataUrl = canvas.t ...
- 第二次写linux驱动总结
第一次写驱动是在去年,2019年十月份左右.当时是看着韦老师的视频一步步完成的.其中经历了很多error.搭建环境花费了很多精力.时间来到了2020年2月19日星期三,韦老师新视频出来了,我跟着再来了 ...
- Dart语言学习(三)Dart数值型
一.类型 数值型有 num,int, double num a = ; a = 12.5; print(a); print(a.runtimeType); ; // b = 20.5; print( ...
- 8.5-Day1T3--Asm.Def 的一秒
题目大意 略... (吐槽这题面...让我毫无阅读兴趣) 题解 首先要求出在以两条斜线为新坐标轴下,每个点的坐标 那么....按x先排序 再求y的最长上升子序列 复杂度O(nlogn)吧 记得开lon ...
- 【转】Vue.js 2.0 快速上手精华梳理
Vue.js 2.0 快速上手精华梳理 Sandy 发掘代码技巧:公众号:daimajiqiao 自从Vue2.0发布后,Vue就成了前端领域的热门话题,github也突破了三万的star,那么对于新 ...
- js的split() 方法和join()方法
定义和用法 split() 方法用于把一个字符串分割成字符串数组. String.split() 执行的操作与 Array.join 执行的操作是相反的. join() 方法用于把数组中的所有元素放入 ...
- python+树莓派实现IoT(物联网)数据上传到服务器
环境:raspbian-stretch(2018-06-27) 树莓派:3代B型 1.树莓派设备,需要在野外也能拥有独立联网能力,那必不可少的需要使用物联网模块. 这里使用的是微雪的SIM868通讯模 ...
- 排序算法大荟萃——希尔(Shell)排序算法
1.基本思想:先取一个小于n的整数d1作为第一个增量,把文件的全部记录分成d1个组.所有距离为d1的倍数的记录放在同一个组中.先再各族中进行直接插入排序,然后取第二个增量d2<d1重复上述的分组 ...
- 【Hibernate 多表查询】
HibernateManyTable public class HibernateManyTable { //演示hql左连接查询 @Test public void testSelect12() { ...