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的目的通常都是由于多终端的原因,这 ...
随机推荐
- Go作用域
package main import "fmt" //全局变量的定义 //num3 := 1000//不支持简短定义的写法 var num3 = 1000 func main() ...
- 南京邮电大学网络攻防训练平台(NCTF)-异性相吸-Writeup
南京邮电大学网络攻防训练平台(NCTF)-异性相吸-Writeup 题目描述 文件下载地址 很明显,文件之间进行亦或就可得到flag,不再多说,直接上脚本 #coding:utf-8 file_a = ...
- Python格式化字符串知多少
字符串格式化相当于字符串模板.也就是说,如果一个字符串有一部分是固定的,而另一部分是动态变化的,那么就可以将固定的部分做成模板,然后那些动态变化的部分使用字符串格式化操作符(%) 替换.如一句问候语: ...
- 安卓基础(Navigation)
今天学习了简单的Navigation:页面导航. 页面导航的简单例子: MainAcitivity: package com.example.navigation; import android.su ...
- Ubuntu16 nginx 配置 Let's Encrypt 免费ssl
每篇一句 Some of us get dipped in flat, some in satin, some in gloss. But every once in a while you find ...
- python-turtle-画雪花-2种方法及效果的详解
1.方法一: 代码: #python3.8 #xuguojun #2020.1.30 #导出模块 import turtle as t import random as r #定义画雪 def dra ...
- Ubuntu Rabbitmq 安装与配置
原文链接:http://blog.csdn.net/rickey17/article/details/72756766 添加源 新增公钥(不加会有警告) 更新源 安装rabbitmq-server e ...
- 「题解」「JOISC 2014 Day1」历史研究
目录 题目 考场思考 思路分析及标程 题目 点这里 考场思考 大概是标准的莫队吧,离散之后来一个线段树加莫队就可以了. 时间复杂度 \(\mathcal O(n\sqrt n\log n)\) . 然 ...
- MDC 输出线程信息帮助定位问题
log4j中的%x ---NDC,%X---MDC 即%x NDC.clear();NDC.push(this.toString());%X{first} %X{last}MDC.put(" ...
- linux开机启动项问题。6版本与7版本不同之处 。
参考 网址:https://blog.csdn.net/weixin_41909810/article/details/82775247