SpringBoot发送简单文本邮件
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发送简单文本邮件的更多相关文章
- SpringBoot 发送简单邮件
使用SpringBoot 发送简单邮件 1. 在pom.xml中导入依赖 <!--邮件依赖--> <dependency> <groupId>org.springf ...
- spring boot发简单文本邮件
首先要去邮箱打开POP3/SMTP权限: 然后会提供个授权码,用来发送邮件.忘记了,可以点生成授权码再次生成. 1.引入spring boot自带的mail依赖,这里版本用的:<spring-b ...
- thunderbird发送纯文本邮件
向邮件列表中发邮件时,要求邮件格式必须是纯文本格式的,在thunderbird中,邮件格式(plain text或者html格式)在[工具->账户设置->[账户名称]->通讯录]下的 ...
- 使用python发送简单的邮件
from:http://blog.csdn.net/zhaoweikid/article/details/125898 前些时间,论坛上有人讨论怎么用python发送需要认证的邮件,我在我的FreeB ...
- (转)JavaMail邮件发送-发送一个文本邮件和一些问题说明
需要下载的JAR包: JavaMail:http://www.oracle.com/technetwork/java/javamail/index.html JAF:http://www.oracle ...
- 使用SpringBoot发送mail邮件
1.前言 发送邮件应该是网站的必备拓展功能之一,注册验证,忘记密码或者是给用户发送营销信息.正常我们会用JavaMail相关api来写发送邮件的相关代码,但现在springboot提供了一套更简易使用 ...
- Python通过yagmail和smtplib模块发送简单邮件
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件.python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是pytho ...
- springboot发送email邮件
添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- SpringBoot系列—简单的邮件系统
1. 效果发送效果图 2. 邮件开发准备工作 3. springboot引入mail服务 4. 启动应用,开始4种邮件发送测试 1. 效果发送效果图 连续发送了四封邮件:普通文本邮件,带附件的邮件,内 ...
随机推荐
- LA 并查集路径压缩
题目大意:有n个节点,初始时每个节点的父亲节点都不存在.有两种操作 I u v:把点节点u的父亲节点设为v,距离为|u-v|除以1000的余数.输入保证执行指令前u没有父亲节点. E u:询问u到根节 ...
- ElasticSearch 多索引
1.用逗号将索引隔开,如: $ curl -XPOST http://localhost:9200/aaa,website/_search/ { "took": 1, " ...
- My97DatePicker 时间控件
<td> <input type="text" id="sendDate" name="sendDate" class=& ...
- sqlplus 命令 错误
SP2-1503: 无法初始化 Oracle 调用界面 用管理员运行就可以了
- Lighttpd 服务器的安装
https://www.cnblogs.com/rongfengliang/articles/3503228.html
- Java 新手的通病
一:对算法和数据结构不熟悉 为什么我先拿“数据结构和算法”说事捏?这玩意是写程序最最基本的东东.不管你使用 Java 还是其它的什么语言,都离不开它.而且这玩意是跨语言的,学好之后不管在哪门语言中都能 ...
- eclispe集成web插件
最近公司需要使用开源框架开发,所有下载了最新版本的eclispe工具,但是在官网下载的eclispe是不包含web插件的,无法创建web项目,需要自行集成web插件 eclipse官网下载地址:htt ...
- 如何使用KeyChain保存和获取UDID - Flex/AS Programmer
原文 http://www.cnblogs.com/yssgyw/p/3364370.html 本文是iOS7系列文章第一篇文章,主要介绍使用KeyChain保存和获取APP数据,解决iOS7上获取不 ...
- 正則表達式--js使用案例
前言:在前端页面使用中.遇到日期格式的验证.開始使用了一款表单控件验证.可是不兼容!!并且使用起来还受到非常大约束.所以就决定自己写原生js. 为了完毕日期格式的验证.第一步,当然是学会使用正則表達式 ...
- Python基础语法04-数据结构
Python Number(数字) Python Number 数据类型用于存储数值. 数据类型是不允许改变的,这就意味着如果改变 Number 数据类型的值,将重新分配内存空间. Python 支持 ...