参考: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. HashSet HashMap 源码阅读笔记

    hashcode() 与 equals() 应一起重写,在HashMap 会先调用hash(key.hashcode()) 找到对应的entry数组位置 (一般初始是16,2^x,rehash后会翻倍 ...

  2. jmeter实现服务器端后台接口性能测试

    实现目的 在进行服务器端后台接口性能测试时,需要连接到Linux服务器端,然后通过命令调用socket接口,这个过程就需要用到jmeter的SSH Command取样器实现了. 脚本实现 设置CSV ...

  3. django 项目发布(centos 6.5 + python 3.5 + django1.9.8 + paramiko 2.0.2 + gunicorn )

    环境 os centos 6.5 64bit python 3.5 django 1.9.8 paramiko 2.0.2 gunicorn 19.6.0 安装 centos install pyth ...

  4. toSum

    Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...

  5. Spring Boot 操作 Excel

    Excel 在日常操作中经常使用到,Spring Boot 中使用 POI 操作 Excel 本项目源码 github 下载 1 新建 Spring Boot Maven 示例工程项目 注意:本示例是 ...

  6. Oracle中trunc()函数用法

    SQL表达式 注释 SELECT SYSDATE FROM dual --当前系统时间,24小时制 SELECT TO_CHAR(SYSDATE,'yyyy-mm-dd hh24:mi:ss') FR ...

  7. Jmeter_选项_函数助手_RandomString的用法

    1.用处:测试账户注册可以通过随机生成数实现,而不需要Excel手动输入, 缺点:随机生成数可能会重复 优点:不需要使用CSV config 或者excel ,txt格式 2.举例:之前我们通过CSV ...

  8. 【PAT甲级】1095 Cars on Campus (30 分)

    题意:输入两个正整数N和K(N<=1e4,K<=8e4),接着输入N行数据每行包括三个字符串表示车牌号,当前时间,进入或离开的状态.接着输入K次询问,输出当下停留在学校里的车辆数量.最后一 ...

  9. Scrapy模拟登陆

    1. 为什么需要模拟登陆? #获取cookie,能够爬取登陆后的页面 2. 回顾: requests是如何模拟登陆的? #1.直接携带cookies请求页面 #2.找接口发送post请求存储cooki ...

  10. 【代码学习】PYTHON 面向对象

    一.方法重新 #!/usr/bin/python # -*- coding: UTF-8 -*- class Parent: # 定义父类 def myMethod(self): print '调用父 ...