一、搭建SpringBoot项目

详见此文:https://www.cnblogs.com/liuyangfirst/p/8298588.html

注意:

需要添加mail依赖的包,同时还添加了lombock,方便日志打印。如图所示

二、启动Application,测试项目搭建是否成功

三、配置properties文档

 #########邮箱协议
spring.mail.host=smtp.163.com ####还可以是smtp.126.com 等
##########发送邮件的用户名
spring.mail.username= 你的邮箱
########移动端客户授权码(需要开通POP3授权)
spring.mail.password= 授权密码
#######配置邮件发送默认编码utf-8
spring.mail.default-encoding=UTF-8

注意:
        需要授权码,163为例,163官网授权码开通如下

https://help.mail.163.com/faqDetail.do?code=d7a5dc8471cd0c0e8b4b8f4f8e49998b374173cfe9171305fa1ce630d7f67ac2cda80145a1742516

四、业务逻辑

1、需要发件人邮箱地址

2、需要调用自带邮箱封装的类,JavaMailSender

3、需要将收件人,主题,内容填入到方法内,最后,调用JavaMailSender的send方法发送

 package com.baidu.mailtest.service;

 import lombok.extern.slf4j.Slf4j;
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.Service; /******************************
* @author : liuyang
* <p>ProjectName:mail </p>
* @ClassName : MailService
* @date : 2018/9/22 0022
* @time : 22:57
* @createTime 2018-09-22 22:57
* @version : 2.0
* @description :
*
*
*
*******************************/ @Service
@Slf4j
public class MailService { /**
* 发件人邮箱地址
*/
@Value("${spring.mail.username}")
private String fromUserName; @Autowired
private JavaMailSender javaMailSender; /**
* 一般发送邮件方法
*
* @param to 发送给某人
* @param subject 邮件主题
* @param context 邮件内容
*/
public void sendSimpleMail(String to, String subject, String context) { SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(context);
simpleMailMessage.setFrom(fromUserName); javaMailSender.send(simpleMailMessage);
} }

五、编写测试类

 package com.baidu.mailtest;

 import com.baidu.mailtest.service.MailService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import javax.annotation.Resource; @RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class MailServiceApplicationTests { @Resource
MailService mailService; @Test
public void sayMail() { mailService.sendSimpleMail("收件人邮箱", "SpringBoot邮件测试", "今天需要很多美女陪我"); log.info("发送成功");
} }

六、观察测试结果

注意发送时候,测试类比较慢,如图位置会卡一会儿。

发送成功如图:

七、源码分享

git@github.com:liushaoye/mailtest.git

IntelliJ IDEA 2017版 spring-boot 2.0.5 邮件发送简单实例 (三)的更多相关文章

  1. Spring Boot 2.0 版的开源项目云收藏来了!

    给大家聊一聊云收藏从 Spring Boot 1.0 升级到 2.0 所踩的坑 先给大家晒一下云收藏的几个数据,作为一个 Spring Boot 的开源项目(https://github.com/cl ...

  2. Spring Boot 2.0 Intellij Idea 中图文详解打包成可执行Jar

    我们使用Spring Boot 2.0 创建好我们的项目后,我们一般需要打包,然后部署到服务器上. 打包步骤: 1. 选中项目,右键——> Open Module Settings. 2. 切换 ...

  3. Spring Boot 2.0正式发布,新特性解读

    作者|翟永超 Spring Boot 2.0 来啦,有哪些新特性?升级吗? 写在前面 北京时间 3 月 1 日,经过漫长的等待之后,Spring Boot 2.0 正式发布.作为 Spring 生态中 ...

  4. Spring Boot 2.0 的快速入门(图文教程)

    摘要: 原创出处 https://www.bysocket.com 「公众号:泥瓦匠BYSocket 」欢迎关注和转载,保留摘要,谢谢! Spring Boot 2.0 的快速入门(图文教程) 大家都 ...

  5. Spring Boot 2.0 入门指南

    0x01 什么是Spring Boot? Spring Boot是用来简化Spring应用初始搭建以及开发过程的全新框架,被认为是Spring MVC的“接班人”,和微服务紧密联系在一起. 0x02 ...

  6. Spring Boot 2.0 返回JSP页面实战

    1. 模板引擎JSP的限制 在开始之前呢,我觉得我们有必要先去了解下 Spring Boot 2.0 官方文档中提到的如下内容: 模板引擎 除了REST Web服务之外,还可以使用Spring MVC ...

  7. Spring Boot 2.0 热部署指南

    Spring Boot 2.0 支持热部署,实现方法很简单 Spring Boot 2.0 有几种热重载的选项. 推荐的方法是使用spring-boot-devtools 因为它提供了额外的开发时间功 ...

  8. spring boot 2.0.3+spring cloud (Finchley)7、服务链路追踪Spring Cloud Sleuth

    参考:Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin[Finchley 版] Spring Cloud Sleuth 是Spring Cloud的一个组件,主要功能是 ...

  9. Spring Boot 2.0 迁移指南

    ![img](https://mmbiz.qpic.cn/mmbiz_jpg/1flHOHZw6Rs7yEJ6ItV43JZMS7AJWoMSZtxicnG0iaE0AvpUHI8oM7lxz1rRs ...

随机推荐

  1. django的模板语言中一些之前没有用过的小点

    1.模板语言中的for循环的最后一个的表示方式 {% for auther in auther_list %} {% if forloop.last %} {# 这里的意思是for循环的最后一个的意思 ...

  2. 不通过调用__Init__来创建实例

    老样子,抛出个问题,我们想要创建一个实例,但是由于某些原因想绕过__init__方法,用别的方式来进行创建. 举个栗子 小贱贱反序列化数据,或者说实现一个类方法将其作为备选的构造函数,都属于这种情况. ...

  3. [leetcode]210. Course Schedule II课程表II

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  4. iOS工程中创建pch文件

    1.新建pch类文件 2.在工程配置中,Build Setting 下搜索"pre"寻找Apple LLVM6.1 - Language下的 Preflx Header 3.点开P ...

  5. python学习-(__new__方法和单例模式)

    class Dog(object): __instance = None __init_flag = False def __new__(cls, name): if cls.__instance = ...

  6. build.js

    // https://github.com/shelljs/shelljs// 检查NodeJS和npm的版本require('./check-versions')() process.env.NOD ...

  7. golang 实现延迟消息原理与方法

    实现延迟消息具体思路我是看的下面这篇文章 https://mp.weixin.qq.com/s/eDMV25YqCPYjxQG-dvqSqQ 实现延迟消息最主要的两个结构: 环形队列:通过golang ...

  8. .net core和.net 4.7区别和联系笔记

    1. 简单说,都是.net standard所定义的接口的实现,都是 .net standard的儿子. 3down voteaccepted C# is a programming language ...

  9. VSCode一直弹框错误Linter pylint is not installed

    确保已经安装Python编译环境 点击下图位置(这个是我已经安装过后的文字,原本显示“搜索Python”字样) 点击后显示如下,点击安装 然后出现一大坨命令 最终出现“Successfully ins ...

  10. 搭建FTP服务器 window7

    1.安装IIS组件,打开控制面板-->程序和功能,点击打开或关闭windows功能 找到Internet信息服务,勾选FTP服务器和Web管理工具下的IIS管理控制台进行安装ftp,如图所示 2 ...