1、pom.xml添加 spring-boot-starter-mail 依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2、application.properties中添加发送邮件的配置

spring.mail.host=smtp.163.com
spring.mail.port=25
spring.mail.username=发送邮件的账号@163.com
spring.mail.password=授权码
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true mail.fromMail.addr=发送邮件的账号@163.com

3、发送邮件的接口

package com.st.service;

public interface MailService {

    public void sendSimpleMail(String to, String subject, String content);

}

4、发送邮件的实现类

package com.st.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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;
import org.springframework.stereotype.Service; import com.st.service.MailService; @Service
@Component
public class MailServiceImpl implements MailService { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired
private JavaMailSender mailSender; @Value("${mail.fromMail.addr}")
private String from; @Override
public void sendSimpleMail(String to, String subject, String content) { SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content); try {
mailSender.send(message);
logger.info("发送成功。。。。。。");
} catch (Exception e) {
logger.error("发送失败!!!!!!", e);
}
} }

5、测试类

package com.st;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import com.st.service.MailService; @RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootMailApplicationTests { @Autowired
private MailService mailService; //生成6位随机验证码
int randNum = 1 + (int)(Math.random() * ((999999 - 1) + 1)); @Test
public void testSimpleMail() throws Exception {
mailService.sendSimpleMail("接收邮件的账号@qq.com", "开发邮件发送功能", "验证码:"+randNum);
} }

SpringBoot发送简单文本邮件的更多相关文章

  1. SpringBoot 发送简单邮件

    使用SpringBoot 发送简单邮件 1. 在pom.xml中导入依赖 <!--邮件依赖--> <dependency> <groupId>org.springf ...

  2. spring boot发简单文本邮件

    首先要去邮箱打开POP3/SMTP权限: 然后会提供个授权码,用来发送邮件.忘记了,可以点生成授权码再次生成. 1.引入spring boot自带的mail依赖,这里版本用的:<spring-b ...

  3. thunderbird发送纯文本邮件

    向邮件列表中发邮件时,要求邮件格式必须是纯文本格式的,在thunderbird中,邮件格式(plain text或者html格式)在[工具->账户设置->[账户名称]->通讯录]下的 ...

  4. 使用python发送简单的邮件

    from:http://blog.csdn.net/zhaoweikid/article/details/125898 前些时间,论坛上有人讨论怎么用python发送需要认证的邮件,我在我的FreeB ...

  5. (转)JavaMail邮件发送-发送一个文本邮件和一些问题说明

    需要下载的JAR包: JavaMail:http://www.oracle.com/technetwork/java/javamail/index.html JAF:http://www.oracle ...

  6. 使用SpringBoot发送mail邮件

    1.前言 发送邮件应该是网站的必备拓展功能之一,注册验证,忘记密码或者是给用户发送营销信息.正常我们会用JavaMail相关api来写发送邮件的相关代码,但现在springboot提供了一套更简易使用 ...

  7. Python通过yagmail和smtplib模块发送简单邮件

    SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件.python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是pytho ...

  8. springboot发送email邮件

    添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  9. SpringBoot系列—简单的邮件系统

    1. 效果发送效果图 2. 邮件开发准备工作 3. springboot引入mail服务 4. 启动应用,开始4种邮件发送测试 1. 效果发送效果图 连续发送了四封邮件:普通文本邮件,带附件的邮件,内 ...

随机推荐

  1. K-th Number(poj 2104)

    题意:静态第K大 #include<cstdio> #include<iostream> #include<cstring> #define N 200010 #d ...

  2. CPU 和内存虚拟化原理

    前面我们成功地把 KVM 跑起来了,有了些感性认识,这个对于初学者非常重要.不过还不够,我们多少得了解一些 KVM 的实现机制,这对以后的工作会有帮助. CPU 虚拟化 KVM 的虚拟化是需要 CPU ...

  3. 了解Chrome扩展程序开发--摘抄

    了解Chrome扩展程序开发 2018-01-11 边城到此莫若 鸡蛋君说前端 首先,我尝试来用简单几句话描述一下Chrome扩展程序: Chrome扩展主要用于对浏览器功能的增强,它强调与浏览器相结 ...

  4. Linux设置文件与Shell操作环境

    Shell设置文件读取流程 /etc/shells记录了Linux系统中支持的所有shell,默认使用bash.用户登入Linux系统时会获取到一个shell,具体获取到哪个shell与登录账号有关, ...

  5. sql 注入 及 in 注入

    原文地址: http://www.cnblogs.com/lzrabbit/archive/2012/04/22/2465313.html 除了校验参数内容,过滤长度和sql关键字. 解决in条件拼接 ...

  6. Mysql Binlog日志文件介绍

    一.Binlog简介 官方文档参考 https://dev.mysql.com/doc/refman/5.5/en/binary-log.html Binlog(Binary Log) 指数据库的表创 ...

  7. SGU 分类

    http://acm.sgu.ru/problemset.php?contest=0&volume=1 101 Domino 欧拉路 102 Coprime 枚举/数学方法 103 Traff ...

  8. python文件追加及时间获取

    一.python:文件的读取.创建.追加.删除.清空 2011-10-24 11:36:35|  分类: python |举报 |字号 订阅   一.用Python创建一个新文件,内容是从0到9的整数 ...

  9. 树莓派LED指示灯说明

    原文:http://shumeipai.nxez.com/2014/09/30/raspberry-pi-led-status-detail.html?variant=zh-cn LED亮灯状态 LE ...

  10. [WASM Rust] Create and Publish a NPM Package Containing Rust Generated WebAssembly using wasm-pack

    wasm-pack is a tool that seeks to be a one-stop shop for building and working with Rust generated We ...