Spring boot发送邮件
最近接到一个需求:分配任务给用户时,发送邮件提醒用户。
后端应该和Andorid一样有现成的api支持,浏览器里搜索了下,果不其然,很轻松就实现了这个功能,现在记录下。
首先添加Maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
其次增加Spring Boot的配置
spring.mail.host = smtp.163.com spring.mail.port = 25
spring.mail.username=用户名 //发送方的邮箱 spring.mail.password=密码 //对于163,qq邮箱而言 密码指的就是发送方的授权码
spring.mail.port=25
#注意:在spring.mail.password处的值是需要在邮箱设置里面生成的授权码,这个不是真实的密码。
代码实现
/**
* @className EmailServiceImpl
* @Description 发送邮件实现类
* @Author
* @Date 2019/4/17 18:34
**/
@Service
public class EmailServiceImpl implements EmailService { @Autowired
private JavaMailSender javaMailSender; @Override
public void sendSimpleMail() {
MimeMessage message;
try {
message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom("xxx@163.com");
helper.setTo("xxx@qq.com");
helper.setSubject("标题:有新的任务分配给您"); StringBuffer sb = new StringBuffer();
sb.append("<h1>大标题-h1</h1>")
.append("<p style='color:#F00'>哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈或或或或</p>")
.append("<p style='text-align:right'>右对齐</p>");
helper.setText(sb.toString(), true);
FileSystemResource fileSystemResource = new FileSystemResource(new File("F:\\360MoveData\\Users\\admin\\Desktop\\file1.jpg"));
helper.addAttachment("123.jpg", fileSystemResource);
javaMailSender.send(message);
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
测试下
/**
* @className EmailResourceTest
* @Description TODO
* @Author
* @Date 2019/4/18 11:34
**/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {SecurityBeanOverrideConfiguration.class, DataWoodManagerApp.class})
public class EmailResourceTest { @Autowired
EmailServiceImpl emailService; @Test
public void sendEmail(){
emailService.sendSimpleMail();
} }

我擦,咋回事,搜下550 User has no permission,原来新注册的163邮件默认是不开启客户端授权验证的(对自定的邮箱大师客户端默认开启),

然后再测试下,发送成功

轻松搞定~
Spring boot发送邮件的更多相关文章
- 47. Spring Boot发送邮件【从零开始学Spring Boot】
(提供源代码) Spring提供了非常好用的JavaMailSender接口实现邮件发送.在Spring Boot的Starter模块中也为此提供了自动化配置.下面通过实例看看如何在Spring Bo ...
- Spring Boot 发送邮件
需求 最近因为业务的变更,需要对老用户进行发送邮件处理.目前市面上也有很多代发邮件的接口,可以接入.由于量不是特别大,放弃了这个途径.改用我们自己通过 smtp 发送邮件来处理. 技术选择 Java ...
- Spring boot 发送邮件示例
最近的一个项目中用到了邮件发送,所以研究了一下.将其总结下来. 首先 登录邮箱 -->设置-->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务--> ...
- 000 基于Spring boot发送邮件
发送邮件的程序,使用QQ的服务器,经过测试,完全可行.可复现 一:准备工作 1.找到账号的授权码 这个是程序需要使用的. 在设置中查找. 2.新建项目的目录 二:完整的程序代码 1.pom.xml & ...
- Spring Boot 2发送邮件手把手图文教程
原文:http://www.itmuch.com/spring-boot/send-email/ 本文基于:Spring Boot 2.1.3,理论支持Spring Boot 2.x所有版本. 最近有 ...
- Spring Boot交流平台
可以关注微信公众号springboot或者可以加入 Spring Boot QQ交流群1:193341332 (群已满) Spring Boot QQ交流群2:193341364 微信公众号搜索spr ...
- 57. Spring 自定义properties升级篇【从零开始学Spring Boot】
之前在两篇文章中都有简单介绍或者提到过 自定义属性的用法: 25.Spring Boot使用自定义的properties[从零开始学Spring Boot] 51. spring boot属性文件之多 ...
- Spring Boot (十):邮件服务
Spring Boot 仍然在狂速发展,才几个多月没有关注,现在看官网已经到 2.1.0.RELEASE 版本了.准备慢慢在写写 Spring Boot 相关的文章,本篇文章使用 Spring Boo ...
- Spring Boot整合邮件发送
概述 Spring Boot下面整合了邮件服务器,使用Spring Boot能够轻松实现邮件发送:整理下最近使用Spring Boot发送邮件和注意事项: Maven包依赖 <dependenc ...
随机推荐
- ASP.NET MVC 手机短信验证
本文来自于stoneniqiu的文章,原文地址 http://www.cnblogs.com/stoneniqiu/p/6234002.html 1.注册一个应用 得到AppKey 和 App Sec ...
- 二值化方法:Kittler:Minimum Error Thresholding
Kittler二值化方法,是一种经典的基于直方图的二值化方法.由J. Kittler在1986年发表的论文“Minimum Error Thresholding”提出.论文是对贝叶斯最小错误阈值的准则 ...
- webpack4 + vue + vue-router + vuex
ps: 所有案例使用的 node 及 npm 版本如下 node版本: v8.4.0 npm: 5.3.0 下一个案例默认是接着上一个继续写的 建议先熟悉以下文档 vue vue-router vue ...
- 【scrapy】使用方法概要(四)(转)
[请初学者作为参考,不建议高手看这个浪费时间] 上一篇文章,我们抓取到了一大批代理ip,本篇文章介绍如何实现downloaderMiddleware,达到随即使用代理ip对目标网站进行抓取的. 抓取的 ...
- Latex Error cannot determine the size of graphic 报错的解决的方法
Latex Error cannot determine the size of graphic 报错的解决的方法 插入jpg文件老是会报错... 追究了半天,原来是编译的命令又问题,不应该使用 la ...
- Oracle 快速插入1000万条数据的实现方式
1.使用dual配合connect by level create table BigTable as select rownum as id from dual connect by level & ...
- pytest文档18-配置文件pytest.ini
前言 pytest配置文件可以改变pytest的运行方式,它是一个固定的文件pytest.ini文件,读取配置信息,按指定的方式去运行. ini配置文件 pytest里面有些文件是非test文件 py ...
- Selenium2+python自动化58-读取Excel数据(xlrd)
前言 当登录的账号有多个的时候,我们一般用excel存放测试数据,本节课介绍,python读取excel方法,并保存为字典格式. 一.环境准备 1.先安装xlrd模块,打开cmd,输入pip inst ...
- 《疯狂iOS讲义(下)——iPhone/iPad高级应用与手游开发(含CD光盘1张)》
<疯狂iOS讲义(下)——iPhone/iPad高级应用与手游开发(含CD光盘1张)> 基本信息 作者: 李刚 肖文吉 出版社:电子工业出版社 ISBN:9787121224379 ...
- Android组件之Service浅谈
Service是Android中的四大组件之一,和windows中的服务是类似,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序Service,手机中有的程序的 ...