spring-boot 速成(10) -【个人邮箱/企业邮箱】发送邮件
发邮件是一个很常见的功能,代码本身并不复杂,有坑的地方主要在于各家邮件厂家的设置,下面以qq个人邮箱以及腾讯企业邮箱为例,讲解如何用spring-boot发送邮件:
一、添加依赖项
compile 'org.springframework.boot:spring-boot-starter-mail'
二、application.yml配置
2.1 QQ个人邮箱
spring:
application:
name: mail-demo
mail:
host: smtp.qq.com
username: xxxx@qq.com # 这里填写个人的qq邮箱
password: ***** # 注:这里不是qq邮箱的密码,而是授权码
properties:
mail.smtp.auth: true
mail.smtp.starttls.enable: true
mail.smtp.starttls.required: true
生成授权码的方法参考下图:


2.3 QQ企业邮箱
spring:
application:
name: mail-demo
mail:
host: smtp.exmail.qq.com
username: xxxx@puscene.com # 这里填写企业邮箱
password: **************** # 这里填写企业邮箱登录密码
properties:
mail.smtp.auth: true
mail.smtp.socketFactory.class: javax.net.ssl.SSLSocketFactory
mail.smtp.socketFactory.fallback: false
mail.smtp.socketFactory.port: 465
企业邮箱如果未开启安全登录,就不需要授权码了,直接填写登录密码即可。如果开启了安全登录,参考下图:

则password这里,需要填写客户端专用密码
三、 发送代码示例
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.redis.RedisRepositoriesAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender; /**
* Created by 菩提树下的杨过 on 12/08/2017.
*/
@SpringBootApplication
public class MailDemo { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(MailDemo.class, args);
JavaMailSender mailSender = context.getBean(JavaMailSender.class); SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("菩提树下的杨过<xxxxxxxx@xxx.com>"); //注意这里的发送人邮箱,要与yml配置中的username相同,否则验证不通过 message.setTo("xxx@126.com");
String[] ccList = new String[]{"xxxx@126.com", "yang.junming@xxxx.com"};//这里添加抄送人名称列表
message.setCc(ccList);
String[] bccList = new String[]{"yyyy@126.com", "yjmyzz@xxxx.com"};//这里添加密送人名称列表
message.setBcc(bccList);
message.setSubject("主题:简单邮件(QQ个人邮件)-抄送,密送测试");
message.setText("测试邮件内容");
mailSender.send(message);
System.out.println("发送成功!");
}
}
spring-boot 速成(10) -【个人邮箱/企业邮箱】发送邮件的更多相关文章
- Spring Boot 整合 Redis 和 JavaMailSender 实现邮箱注册功能
Spring Boot 整合 Redis 和 JavaMailSender 实现邮箱注册功能 开篇 现在的网站基本都有邮件注册功能,毕竟可以通过邮件定期的给用户发送一些 垃圾邮件 精选推荐
- Spring Boot 的 10 个核心模块
学习 Spring Boot 必须得了解它的核心模块,和 Spring 框架一样,Spring Boot 也是一个庞大的项目,也是由许多核心子模块组成的. 你所需具备的基础 告诉你,Spring Bo ...
- Spring Boot 中 10 行代码构建 RESTful 风格应用
RESTful ,到现在相信已经没人不知道这个东西了吧!关于 RESTful 的概念,我这里就不做过多介绍了,传统的 Struts 对 RESTful 支持不够友好 ,但是 SpringMVC 对于 ...
- Spring Boot入门学习必知道企业常用的Starter
SpringBoot企业常用的 starter SpringBoot简介 SpringBoot运行 SpringBoot目录结构 整合JdbcTemplate @RestController 整合JS ...
- spring boot 学习10 定义springboot的两种方法
使用spring boot的两种方法: A:继承spring-boot-starter-parent项目 这种方式很简单,只需要在POM里面添加parent父工程即可. B: 如果你不喜欢继承spri ...
- 【spring boot】10.spring boot下的单元测试
spring boot下的单元测试,思前想后还是需要单独用一章篇幅来看看. 然后在看了介绍和使用时候,我感觉并不想多去看了. 但是还是给后来人留下参考的路径: 官网说明:https://spring. ...
- myEclipse+Spring boot+Hbuilder+jwt Token+mongoDB+企业微信H5开发
企业微信应用的H5开发 1.参考文档:weUI:http://jqweui.com/ 2.企业微信接口文档:https://work.weixin.qq.com/api/doc#10029 3.百度地 ...
- Java实现网易企业邮箱发送邮件
最近项目需要用网易企业邮箱发送邮件,特意来将实现过程记录一下: maven导入jar包 <!-- javax.mai 核心包 --> <dependency> <grou ...
- Spring Boot Admin实现服务健康预警
Over View 上一篇文章主要介绍了Spring Boot Admin的概况以及我们如何在系统中引入和使用Spring Boot Admin,以此来帮助我们更加了解自己的系统,做到能快速发现.排查 ...
随机推荐
- html5 canvas裁剪区域
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Flex 经验笔记一
Module页面嵌套子Module页面直接用标签嵌入是不行的,无法显示出来,需要用到 ModuleManager 使用ModuleInfo 的 addEventListener 判断当子Module ...
- Java使用WebSocket
网页端的消息推送,一般有以下方式: 轮询方式:客户端定时向服务端发送ajax请求,服务器接收到请求后马上返回消息并关闭连接. 优点:后端程序编写比较容易. 缺点:TCP的建立和关闭操作浪费时间和带宽, ...
- [BZOJ 1879][SDOI 2009]Bill的挑战 题解(状压DP)
[BZOJ 1879][SDOI 2009]Bill的挑战 Description Solution 1.考虑状压的方式. 方案1:如果我们把每一个字符串压起来,用一个布尔数组表示与每一个字母的匹配关 ...
- perl6 HTTP::UserAgent (2)
http://www.cnblogs.com/perl6/p/6911166.html 之前这里有个小小例子, 这里只要是总结一下. HTTP::UserAgent包含了以下模块: --------- ...
- 解决windows10 里vs2017 直接开始执行提示“此任务要求应用程序有提升的权限”1.
1.打开vs的安装路径,我的是 C:\Program Files (x86)\Common Files\Microsoft Shared\MSEnv\ ,找到 VSLauncher.exe 右击该文 ...
- redis 配置文件翻译
2014年6月24日 17:29:11 include 如果有其它配置文件,可以使用 include 指令 ####通用配置 daemonize 默认的redis不会以守护进程运行,需要这样的话可 ...
- react-native 报错
报错信息: java.lang.RuntimeException: Unable to load script from assets 'index.android.bundle'. Make sur ...
- SysV和BSD启动风格的比较
Slackware启动脚本与System V启动脚本的区别何在? Slackware 使用BSD风格的init脚本,而很多别的发行版使用System V风格的init脚本.SysV和BSD脚本都是能让 ...
- 洛谷P3383线性筛素数
传送门 代码中体现在那个 $ break $ $ prime $ 数组 中的素数是递增的,当 $ i $ 能整除 $ prime[j ] $ ,那么 $ iprime[j+1] $ 这个合数肯定被 $ ...