3、SpringBoot------邮件发送(1)


开发工具:STS
代码下载链接:https://github.com/theIndoorTrain/Springboot/tree/8878e8e89ce01ceb967ef8c1193ac740a6f7dd40
前言:
每当你生日那天,腾讯官方都会给你致上一封精美的生日祝福邮件......
当你在某个网站注册账号时,往往需要去邮箱里激活验证......
我们今天就来探讨这个技术,邮件的发送。
我们往常发邮件的步骤为:
1.登录邮箱网站或者客户端
2.输入账号、密码
3.填写收件人
4.撰写邮件内容
5.发送
在我们的web项目中,我们要发送邮件给指定用户,步骤为:
1.绑定邮箱服务器
2.验证账号、密码(授权码)
3.建立邮件
4.填写接收者、邮件内容
5.发送
下面我们来实现邮件的发送。
一、简单邮件的发送
1.在pom.xml中添加mail依赖
<!--添加mail依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
2.在application中配置mail
#配置邮箱
spring:
mail:
host: smtp.qq.com
username: 1373572467@qq.com
password: 邮箱密码(qq邮箱填写授权码)
default-encoding: UTF-8 #配置邮件发送人
mail:
from:
addr: 1373572467@qq.com
3.定义邮件发送业务接口:
package com.xm.service; /**
* 邮件发送业务
* @author xm
*
*/
public interface EmailService { /**
* 发送简单邮件
* @param to :收件人
* @param subject : 标题
* @param content :邮件内容
*/
void sendSimpleEmail(String to,String subject,String content); }
4.实现邮件发送业务:
package com.xm.service.impl; 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.Service; import com.xm.service.EmailService;
/**
* 邮件发送业务实现
* @author xm
*
*/
@Service
public class EmailServiceImpl implements EmailService { //获取发送者信息
@Value("${mail.from.addr}")
private String from; //定义邮件发送者
@Autowired
private JavaMailSender sender; @Override
public void sendSimpleEmail(String to, String subject,String content) {
//定义简单邮件
SimpleMailMessage message = new SimpleMailMessage();
//把发送者、收件人、标题、邮件内容封装入简单邮件中
System.out.println("from: "+from+",to:"+to+",subject:"+subject);
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
//交给邮件发送者进行转发
sender.send(message);
System.out.println("发送");
} }
5.定义邮件测试类:
package com.xm; 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.xm.service.EmailService; @RunWith(SpringRunner.class)
@SpringBootTest
public class EmailTest { @Autowired
private EmailService emailService; @Test
/**
* 测试简单邮件的发送
*/
public void sendSimpleMassage() {
emailService.sendSimpleEmail("1373572467@qq.com", "122", "Hello Mail!");
} }
6.运行结果截图:

二、带附件的邮件发送
1.定义发送带附件的邮件接口:
/**
* 发送带附件的邮件
* @param to:收件人
* @param subject : 标题
* @param content:邮件内容
* @param attachment:附件
*/
void sendAttachmentEmail(String to,String subject,String content,File attachment);
2.实现此业务:
@Override
public void sendAttachmentEmail(String to, String subject, String content, File attachment) {
//创建多用途互联网邮件
MimeMessage message = sender.createMimeMessage(); try {
//封装多用途互联网邮件
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(content);
helper.addAttachment("附件", attachment);
} catch (Exception e) {
e.printStackTrace();
}
sender.send(message);
}
3.定义测试:
@Test
/**
* 多用途互联网邮件
*/
public void sendAttachmentEmail() {
File attachment = new File("src/main/resources/static/1.txt");
emailService.sendAttachmentEmail("1373572467@qq.com", "122", "Hello Mail!",attachment);
}
4.运行结果截图:

2018-07-16
3、SpringBoot------邮件发送(1)的更多相关文章
- SpringBoot邮件发送
这篇文章介绍springboot的邮件发送. 由于很简单就没有分出server和imp之类,只是在controller简单写个方法进行测试. 首先pom文件加入spring-boot-starter- ...
- Springboot邮件发送思路分析
毕业设计里需要邮件发送,所以学习,总的来讲,我考虑以下几点, 代码量少,代码简单.配置少,一看就懂,使用 JavaMail 太麻烦了. 异步执行,添加员工之后会发送入职邮件, 多线程处理,设计里有一个 ...
- springboot邮件发送与接收读取
发送邮件 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...
- SpringBoot邮件发送功能
快速入门 在Spring Boot的工程中的pom.xml中引入spring-boot-starter-mail依赖: <dependency> <groupId>org.sp ...
- SpringBoot项目实现文件上传和邮件发送
前言 本篇文章主要介绍的是SpringBoot项目实现文件上传和邮件发送的功能. SpringBoot 文件上传 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 开发准备 环境要 ...
- 补习系列(12)-springboot 与邮件发送
目录 一.邮件协议 关于数据传输 二.SpringBoot 与邮件 A. 添加依赖 B. 配置文件 C. 发送文本邮件 D.发送附件 E. 发送Html邮件 三.CID与图片 参考文档 一.邮件协议 ...
- springboot添加邮件发送及压缩功能
springboot添加邮件发送及文件压缩功能 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190233.html 先来一段诗 ``` 就这样吧 忍受折磨 ...
- IntelliJ IDEA 2017版 spring-boot 2.0.5 邮件发送简单实例 (三)
一.搭建SpringBoot项目 详见此文:https://www.cnblogs.com/liuyangfirst/p/8298588.html 注意: 需要添加mail依赖的包,同时还添加了lom ...
- IntelliJ IDEA 2017版 spring-boot 2.0.3 邮件发送搭建,概念梳理 (二)
第二部分 邮件发送历史 一.第一封邮件 1.1969年10月,世界上的第一封电子邮件 1969年10月世界上的第一封电子邮件是由计算机科学家Leonard K.教授发给他的同事的一条简短 ...
- springboot利用mail配置包,实现邮件发送
了解邮件发送与接收的过程: A->S1->S2->B 1.计算机A通过SMTP协议将邮件发送到服务器S1上: 2.服务器S1再发送到服务器S2: 3.计算机B通过POP3协议接收服务 ...
随机推荐
- 数据库版本管理工具flyway
引入flyway_core jar包 java 代码实现 public class FlywayMigration { @Resource private DataSource dataSource ...
- listview适配器中的控件的点击事件并传值
@Override public View getView(final int position, View convertView, ViewGroup parent) { // TODO Auto ...
- UGUI RectTransform 矩形变换
UGUI游戏对象基本都有这个组件. float radius; radius = GetComponent<RectTransform>().sizeDelta.x; radius = ( ...
- LoadScene场景异步加载
LoadScene场景异步加载 using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; usin ...
- 牛客网Java刷题知识点之字节流练习之从A处复制文本文件到B处(FileReader、FileWriter )、复制文本文件的原理图解
不多说,直接上干货! CopyTextTest.java package zhouls.bigdata.DataFeatureSelection.test; import java.io.FileRe ...
- C# WinForm拖入文件到窗体,得到文件路径
private void textBox1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataForma ...
- 顺序链表(C++)
顺序表结构 struct Sq_list { ]; int length; }; 创建并初始化顺序表 int Init_list(Sq_list *L) { L->length = ; ; } ...
- 部分易被忽视的css3属性
1.-webkit-tap-highlight-color 移动端页面点击按钮时会发现按钮上会出现一块阴影,设置-webkit-tap-highlight-color:rgba(0,0,0,0);就可 ...
- Maven 私有库和本地库的安装与配置 Sonatype Nexus + Maven
环境:CentOS 7.0 Final.JDK8.Sonatype Nexus.Maven 虚拟机模拟IP:192.168.16.167 备注:root权限用户操作 前提:已安装 JDK8 并配置好了 ...
- redis笔记(三)
redis配置文件 配置文件对单位大小写不敏感 tcp-backlog 511 高并发环境连接数 tcp-keepalive 单位为秒 0 表示不会进行keepalive检测,,,建议设置 ...