参考: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 中发送邮件的更多相关文章

  1. Spring Boot中使用JavaMailSender发送邮件

    相信使用过Spring的众多开发者都知道Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看 ...

  2. 56. spring boot中使用@Async实现异步调用【从零开始学Spring Boot】

    什么是"异步调用"? "异步调用"对应的是"同步调用",同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执 ...

  3. 46. Spring Boot中使用AOP统一处理Web请求日志

    在之前一系列的文章中都是提供了全部的代码,在之后的文章中就提供核心的代码进行讲解.有什么问题大家可以给我留言或者加我QQ,进行咨询. AOP为Aspect Oriented Programming的缩 ...

  4. Spring Boot 2发送邮件手把手图文教程

    原文:http://www.itmuch.com/spring-boot/send-email/ 本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本. 最近有 ...

  5. spring boot(三):Spring Boot中Redis的使用

    spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...

  6. Spring Boot中的事务管理

    原文  http://blog.didispace.com/springboottransactional/ 什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合 ...

  7. Spring Boot中的注解

    文章来源:http://www.tuicool.com/articles/bQnMra 在Spring Boot中几乎可以完全弃用xml配置文件,本文的主题是分析常用的注解. Spring最开始是为了 ...

  8. 在Spring Boot中使用Https

    本文介绍如何在Spring Boot中,使用Https提供服务,并将Http请求自动重定向到Https. Https证书 巧妇难为无米之炊,开始的开始,要先取得Https证书.你可以向证书机构申请证书 ...

  9. Spring Boot中使用Swagger2构建强大的RESTful API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

随机推荐

  1. Go_file操作

    1. FileInfo package main import ( "os" "fmt" ) func main() { /* FileInfo:文件信息 in ...

  2. 每天进步一点点------Sobel算子(2)

    转载  http://blog.csdn.net/tianhai110 索贝尔算子(Sobel operator)主要用作边缘检测,在技术上,它是一离散性差分算子,用来运算图像亮度函数的灰度之近似值. ...

  3. react脚手架和深入理解jsx语法

    react的mvc和vue的mvvm vue的mvvm属于双向绑定,view层,model数据层,vm实现双向绑定的控制层 此种模式,再某一类项目种很有优势:管理系统 ( OA, ERP , CRM ...

  4. awk函数实现将简化IPV6地址补全

    在用awk处理文本时,有些场景需要将简化的IPV6地址补充成完整的IPV6地址,下边函数可简单实现: IPV6地址补全函数 # ipv6地址补全函数 function compipv6(orig_ad ...

  5. 各种颜色空间之间的转换算法(XYZ → Standard-RGB ,Standard-RGB → XYZ)

    http://www.easyrgb.com/en/convert.php#Result http://www.easyrgb.com/en/math.php

  6. Vue - 让水平滚动条(scroll bar)固定在浏览器的底部

    效果 踩坑经历 TLDR; 在几个小时的google和stack overflow的苦苦搜索后,无果. 经过自我思考,想到了一种实现方法: 整个页面是一个盒子,要出现滚动条,必然里面的元素要溢出.也即 ...

  7. 6_12 油田(UVa572)<图的连通块DFS>

    有一家石油公司负责探勘某块地底下的石油含量,这块地是矩行的,并且为了探勘的方便被切割为许多小块.然后使用仪器对每个小块去探勘.含有石油的小块称为一个pocket.假如两个pocket相连,则这两个po ...

  8. Could not find result map com.youotech.tl_cons_credit_rating.entity.Result

    后端报错如下: java.lang.IllegalArgumentException: Result Maps collection does not contain value for com.yo ...

  9. 1010 Radix (25分)

    改了一天也没明白,第7个数据是怎么卡的 #include <bits/stdc++.h> using namespace std; const int maxn=1005; typedef ...

  10. VLAN配置Trunk接口

    实验二:配置Trunk接口. 实验原理: 实验内容: 本实验模拟某公司网络场景.公司规模较大,员工200余名,内部网络是-一个大的局域网.公司放置了多台接入交换机(如S1和S2)负责员工的网络接入.接 ...