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 ...
随机推荐
- jquery.uploadify 使用过程
HTML: <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"& ...
- php 利用fsockopen GET/POST 提交表单及上传文件
1.GET get.php <?php $host = 'demo.fdipzone.com'; $port = 80; $errno = ''; $errstr = ''; $timeout ...
- iOS开发里的Bundle是个啥玩意?!
初学iOS开发的同学,不管是自己写的,还是粘贴的代码,或多或少都写过下面的代码 [[NSBundle mainBundle] pathForResource:@"someFileName&q ...
- 33.NET对加密和解密的支持
散列运算 mscorlib.dll下的System.Security.Cryptography下: 抽象类HashAlgorithm 抽象类MD5 MD5CryptoSer ...
- 【elaseticsearch】elaseticsearch启动报错Caused by: org.elasticsearch.transport.BindTransportException: Failed to bind to [9300-9400]
elaseticsearch启动报错 [es1] uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupExce ...
- java jxl excel 导入导出的 总结(建立超链接,以及目录sheet的索引)
最近项目要一个批量导出功能,而且要生成一个单独的sheet页,最后后面所有sheet的索引,并且可以点击进入连接.网上搜索了一下,找到一个方法,同时把相关的excel导入导出操作记录一下!以便以后使用 ...
- coco游戏android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := game_shared LOCAL_MODULE_FILENAME ...
- Win8 下配置Java开发环境
背景: 大学期间学习过一段时间的JavaEE.不算很熟悉. 后来学习并在工作中很多其它是iOS开发,iOS的水平属于中上. 对技术已经有一定熟知程度. 近期为了写一些东西,须要用到Java写后台. 流 ...
- 服务信息块协议 SMB(Server Message Block protocol)
SMB(Server Message Block)是协议名,它能被用于Web连接和客户端与服务器之间的信息沟通. SMB协议 SMB最初是IBM的贝瑞·费根鲍姆(Barry Feigenbaum)研制 ...
- C/C++ 读取16进制文件
1.为什么有这种需求 因为有些情况需要避免出现乱码.不管什么编码都是二进制的,这样表示为16进制就可以啦. 2.如何读取16进制文件 最近编程用这一问题,网上查了一下,感觉还是自己写吧. 16进制数据 ...