springboot---发送邮件

1、pom.xml配置
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!-- SpringBoot 发送邮件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
2、在application.properties中添加邮箱配置
spring.mail.default-encoding=UTF-8
spring.mail.host=smtp.qq.com
#发送者的邮箱密码
spring.mail.password=xxx
#端口
spring.mail.port=25
#协议
spring.mail.protocol=smtp
#发送者的邮箱账号
spring.mail.username=xxx@qq.com
server.port=8081
邮箱密码:
QQ邮箱 --> 邮箱设置 --> 账号

3、项目整体结构

4、业务层service接口
package org.zyu.springboot_email.service;
public interface EmailService {
/**
* 发送简单邮件
* @param sendTo 接收人
* @param title 邮件标题
* @param content 邮件内容
*/
void sendSimpleMail(String sendTo,String title,String content);
/**
* 发送带静态资源的邮件
* @param sendTo 接收人
* @param title 邮件标题
* @param content 邮件内容
* @param path 资源路径
*/
void sendInlineMail(String sendTo,String title,String content,String path);
/**
* 发送带附件的邮件
* @param sendTo 接收人
* @param title 邮件标题
* @param content 邮件内容
* @param filePath 附件路径
*/
void sendAttachmentsMail(String sendTo,String title,String content,String filePath);
}
5、serviceImpl实现service接口
package org.zyu.springboot_email.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service; import javax.mail.internet.MimeMessage;
import java.io.File; @Service
public class EmailServiceImpl implements EmailService { @Autowired
JavaMailSender mailSender; //发送人
@Value("${spring.mail.username}")
private String sendFrom; @Override
public void sendSimpleMail(String sendTo, String title, String content) {
SimpleMailMessage mainMessage = new SimpleMailMessage();
mainMessage.setFrom(sendFrom);
//接收者
mainMessage.setTo(sendTo);
//发送的标题
mainMessage.setSubject(title);
//发送的内容
mainMessage.setText(content);
mailSender.send(mainMessage);
} @Override
public void sendInlineMail(String sendTo, String title, String content,String path) {
MimeMessage mimeMessage = mailSender.createMimeMessage(); try {
FileSystemResource file = new FileSystemResource(new File(path));
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setFrom(sendFrom);
helper.setTo(sendTo);
helper.setSubject(title);
helper.setText(content, true);
helper.addInline("weixin", file);
} catch (Exception e) {
System.out.println("发送带静态资源的邮件失败");
} mailSender.send(mimeMessage);
} @Override
public void sendAttachmentsMail(String sendTo, String title, String content, String filePath) {
MimeMessage message=mailSender.createMimeMessage();
try {
MimeMessageHelper helper=new MimeMessageHelper(message,true);
helper.setFrom(sendFrom);
helper.setTo(sendTo);
helper.setSubject(title);
helper.setText(content);
FileSystemResource file=new FileSystemResource(new File(filePath));
String fileName=filePath.substring(filePath.lastIndexOf(File.separator));
//添加多个附件可以使用多条
//helper.addAttachment(fileName,file);
helper.addAttachment(fileName,file);
mailSender.send(message);
System.out.println("带附件的邮件发送成功");
}catch (Exception e){
e.printStackTrace();
System.out.println("发送带附件的邮件失败");
}
}
}
5、业务层Controller
package org.zyu.springboot_email.controller; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.zyu.springboot_email.service.EmailService; import java.nio.file.Path; @RestController
public class MailController { @Autowired
private EmailService emailService; /**
* 简单发送邮件
* @return
*/
@GetMapping("/send")
public String send(){
emailService.sendSimpleMail( "393488908@qq.com", "测试标题", "hello world");
return "发送成功";
} /**
* 发送静态资源邮件
* @return
*/
@GetMapping("/sendInlineMail")
public String sendInlineMail(){
emailService.sendInlineMail("393488908@qq.com", "主题:嵌入静态资源", "<html><body><img src=\"cid:weixin\" ></body></html>","C:\\Users\\Administrator\\Desktop\\timg.jpg");
return "发送成功";
} /**
* 发送带附件的邮件
* @return
*/
@GetMapping("/sendAttachmentsMail")
public String sendAttachmentsMail(){
emailService.sendAttachmentsMail("393488908@qq.com", "主题:携带附件邮件", "hello world", "D:\\谷歌下载\\注册补贴明细列表.xlsx");
return "发送成功";
} }
springboot---发送邮件的更多相关文章
- SpringBoot 发送邮件功能实现
背景 有个小伙伴问我你以前发邮件功能怎么弄的.然后我就给他找了个demo,正好在此也写一下,分享给大家. 理清痛点 发送邮件,大家可以想一下,坑的地方在哪? 我觉得是三个吧. 第一:邮件白名单问题. ...
- 记录一次简单的springboot发送邮件功能
场景:经常在我们系统中有通过邮件功能找回密码,或者发送生日祝福等功能,今天记录下springboot发送邮件的简单功能 1.引入maven <!-- 邮件开发--><dependen ...
- 1.使用javax.mail, spring的JavaMailSender,springboot发送邮件
一.java发邮件 电子邮件服务器:这些邮件服务器就类似于邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中,按功能划分有两种类型 SMTP邮件服务器:用户替用户发送邮件和接 ...
- 3.4 SpringBoot发送邮件
spring官方提供了spring-boot-starter-mail来整合邮件发送功能,本质上还是利用了JavaMailSender类. 首先我们要在项目中引入相关依赖 <parent & ...
- (入门SpringBoot)SpringBoot发送邮件(十一)
SpringBoot配置邮件服务: 1.引入jar <!-- 邮件 --> <dependency> <groupId>org.springframework ...
- springboot发送邮件,以及携带邮件附件简单使用
可以通过springboot官方文档中Sending Email,找到类似如下java mail的使用文档 https://docs.spring.io/spring/docs/5.1.9.RELEA ...
- 【快学springboot】使用springboot发送邮件
前言 在实际项目中,经常需要用到邮件通知功能.比如,用户通过邮件注册,通过邮件找回密码等:又比如通过邮件发送系统情况,通过邮件发送报表信息等等,实际应用场景很多.这篇文章,就教大家通过springbo ...
- 使用 FreeMarker模板 Springboot 发送邮件
四.使用 FreeMarker模板 HTML 标签的字符串拼接是一件很棘手的事.因为在你的大脑中解析HTML标签并想象它在渲染时会是什么样子是挺困难的.而将HTML混合在Java代码中又会使得这个问题 ...
- 最简单的 springboot 发送邮件,使用thymeleaf模板
1,导入需要的包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- springboot发送邮件
1.在发送端邮箱平台开通SMTP服务 1)以163邮箱为例: step 1: step 2: 2.编写代码: 1)添加发送邮箱maven依赖 <dependency> <groupI ...
随机推荐
- js校验对象是否全部为空
function judgeIsNotBlank(obj) { var bool = true; var arr = Object.keys(obj); ; for(var key in obj){ ...
- 使用Docker安装FastDFS(分布式文件系统)
1. 获取镜像 可以利用已有的FastDFS Docker镜像来运行FastDFS. 获取镜像可以通过下载 docker image pull delron/fastdfs 也可是直接使用提前下载的镜 ...
- 异步Promise及Async/Await最完整入门攻略
一.为什么有Async/Await? 我们都知道已经有了Promise的解决方案了,为什么还要ES7提出新的Async/Await标准呢? 答案其实也显而易见:Promise虽然跳出了异步嵌套的怪圈, ...
- 体验Code::Blocks下的Windows GUI编程(32 bit and 64 bit)
0. 前言 不知道为什么,几乎所有的C++编程教程都是从命令行版(控制台应用程序)的Hello World开始的.然而,对于现在的年轻人来说,从小就用鼠标点击窗口或图标操作电脑,对于那个神秘黑色的窗口 ...
- JavaScript 面向对象编程 · 理解对象
前言: 在我们深入 面向对象编程之前 ,让我们先理解一下Javascript的 对象(Object),我们可以把ECMAScript对象想象成散列表,其值无非就是一组名值对,其中值可以是数据 ...
- ng-cli新建项目
tip:所有的命令是红色标签 , 链接为蓝色标签 使用ng-cli创建新的项目一般需要安装一些新的东西后才可以进行创建成功 1.需要先安装node.js 链接: https://nodejs.org ...
- Scala 学习笔记之隐式参数和隐式转换并用
隐式转换条件: 1. 当表达式类型与预期的类型不同时 2.当对象访问一个不存在的成员时 3.当对象调用某个方法,而该方法的参数声明与传入参数不相匹时. 隐式转换搜索范围: 1. 位于源火目标类型伴生对 ...
- 【Tomcat】tomcat7 设置成系统服务启动
1.启动cmd 2.cd C:\Program Files\tomcat7\bin 3.service.bat install 4.打开tomcat7w.exe可以启动管理服务
- python编程基础之八
位运算符 内存: 1 bit ----b 1Byte----B 1Byte = 8bit 1K = 1024Byte 1M = 1024KB 1G = 1024M 1T = 1024G 二级 ...
- C# 常见面试问题汇总
1.c#垃圾回收机制 从以下方面入手展开: 1.压缩合并算法 2.代的机制 3.GC调用终结器 Garbage Collector . NET采用了和Java类似的方法由CLR(Common ...