参考: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. 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)

    题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...

  2. NMAP输出结果中CPE的含义【转】

    CPE全称是Common Platform Enumeration,意思是通用平台枚举项:它是NMAP对识别出来的软件.操作系统和硬件的一种命名方式:格式如下: cpe:/<part>:& ...

  3. CentOS7修改主机名的三种方法

    在CentOS7中,有三种定义的主机名: 静态的(Static hostname) “静态”主机名也称为内核主机名,是系统在启动时从/etc/hostname自动初始化的主机名. 瞬态的(Tansie ...

  4. confluence-工具安装

    wiki 企业级的应用知识库,个人感觉还是很不错的,所以自己本地也搭一个玩玩: 1.下载confluence wget https://downloads.atlassian.com/software ...

  5. “1+X”证书Web前端开发等级考试简介

    考试简介 “1+X”证书制度是为了深入贯彻党的十九大精神和全国教育大会精神,教育部会同国家发展改革委.财政部.市场监管总局制定了<关于在院校实施“学历证书+若干职业技能等级证书”制度试点方案&g ...

  6. 将linux上的项目传到github上

    在网友的帮助下,终于学会了这一招. 1.首先要确定你的linux上有安装了git. 2.到你的网页github上新建一个仓库,将其clone到linux上. 3.将你的项目放进这个空的仓库(文件夹). ...

  7. vue 实现简单的富文本编辑

    安装:npm install vue-quill-editor --save 安装Vue-Quill-Editor需要依赖: npm install quill --save 在入口文件main.js ...

  8. 吴裕雄 人工智能 java、javascript、HTML5、python、oracle ——智能医疗系统WEB端智能分诊代码简洁版实现

    <%-- Document : getInfo Created on : 2018-10-7, 21:36:37 Author : acer --%> <%@page import= ...

  9. robot用例执行常用命令

    执行命令 执行一个用例 robot -t “testcase_name“ data_test.robot 按用例文件执行 robot data_test.robot或者 robot --suite “ ...

  10. 使用node查询数据库(mysql)时,日期格式不对的问题。

    https://blog.csdn.net/chanlingmai5374/article/details/93190983 1.问题场景 数据库里存了 datetime.但 Node 查询出来是这样 ...