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 ...
随机推荐
- 推荐几个我近期排查线上http接口偶发415时用到的工具
导读:近期有一个业务部门的同学反馈说他负责的C工程在小概率情况下SpringMvc会返回415,通过输出的日志可以确定是SpringMvc找不到content-type这个头了,具体为什么找不到了呢? ...
- Spark 学习笔记之 Spark history Server 搭建
在hdfs上建立文件夹/directory hadoop fs -mkdir /directory 进入conf目录 spark-env.sh 增加以下配置 export SPARK_HISTORY ...
- MySQL系统表的利用姿势(浅探)
MySQL数据库文件读写 权限要求: 具备读写权限并且目标文件为可读内容 目标内容具有完整路径且目录可访问 目标内容是否具备文件读写操作权限 查看是否有文件读写权限 show variables li ...
- web前端之浏览器: 知识汇总
一.URL到页面 准备阶段: 输入URL,Enter进入查找 浏览器在本地查找host文件,匹配对应的IP: 找到返回浏览器并缓存 没有,则进入路由查找: 找到返回浏览器并缓存 再没有,再进入公网DN ...
- Laravel 5 中文文档 CHM 版
使用 Microsoft HTML Help Workshop 做了一个 Laravel 5.4 中文文档的 CHM 版本. 百度网盘下载地址:http://pan.baidu.com/s/1dFN2 ...
- bootstrap-table 页脚总计(自定义统计总数)
•首先给table添加属性: showFooter: footer js代码如下: //初始化bootstrapTableinitBootstrapTable: function () { var o ...
- 500行代码,教你用python写个微信飞机大战
这几天在重温微信小游戏的飞机大战,玩着玩着就在思考人生了,这飞机大战怎么就可以做的那么好,操作简单,简单上手. 帮助蹲厕族.YP族.饭圈女孩在无聊之余可以有一样东西让他们振作起来!让他们的左手 / 右 ...
- Spring Boot 2.X(八):Spring AOP 实现简单的日志切面
AOP 1.什么是 AOP ? AOP 的全称为 Aspect Oriented Programming,译为面向切面编程,是通过预编译方式和运行期动态代理实现核心业务逻辑之外的横切行为的统一维护的一 ...
- Warfare And Logistics UVA - 1416
题目链接:https://vjudge.net/problem/UVA-1416 题解: 这是一个最短路的好题,首先我们考虑如果暴力弗洛伊德,显然时间复杂度不对,如果做n次spfa好像复杂度也不对,所 ...
- 为什么不同命名空间的docker容器可以相互通信?
一.什么是容器网络栈 所谓容器能看见的"网络栈",被隔离在自己的Network Namespace当中 1.网卡(network interface) 2.回环设备(loopbac ...