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,以此来帮助我们更加了解自己的系统,做到能快速发现.排查 ...
随机推荐
- OnContextMenu事件(转)
用oncontextmenu事件单禁用右键菜单 一个页面中,BODY中用oncontextmenu='return false'来取消鼠标右键:在JS中设置oncontextmenu='return ...
- npm 更换阿里镜像
使用NPM(Node.js包管理工具)安装依赖时速度特别慢,只需要使用–registry参数指定镜像服务器地址, npm install your-need-model --registry=http ...
- js深复制
一般来讲深复制主要是为了复制js对象中的引用类型,引用类型在普通的赋值操作下相当于是引用,修改复制对象也会影响原对象,简单的方法的话可以使用JSON.parse(JSON.stringify(obj) ...
- 第10月第10天 git
1. 已经用 git commit 提交了代码. 此命令可以用来回退到任意版本:git reset --hard commitid https://www.cnblogs.com/qufanblo ...
- Servlet笔记8--乱码解决方案
乱码解决方案: 代码详解: package com.bjpowernode.javaweb.servlet; import java.io.IOException; import javax.serv ...
- JQuery效果隐藏/显示
hide() 方法 语法 $(selector).hide(speed,callback) show() 方法 语法 $(selector).show(speed,callback) 参数 描述 sp ...
- 【Pyhon】利用BurpSuite到SQLMap批量测试SQL注入
前言 通过Python脚本把Burp的HTTP请求提取出来交给SQLMap批量测试,提升找大门户网站SQL注入点的效率. 导出Burp的请求包 配置到Burp的代理后浏览门户站点,Burp会将URL纪 ...
- Route Between Two Nodes in Graph
Given a directed graph, design an algorithm to find out whether there is a route between two nodes. ...
- 移动端调试利器之vconsole
说明 由于移动端项目在手机中调试时不能使用chrome的控制台,而vconsole是对pc端console的改写 使用方法 使用 npm 安装: npm install vconsole 使用webp ...
- 2018-2019-2 网络对抗技术 20165301 Exp1 PC平台逆向破解
任务一 直接修改程序机器指令,改变程序执行流程 1.输入指令objdump -d pwn5301 | more反汇编pwn1文件. 通过反汇编,可以看到main函数中的call 804891,其机器码 ...