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 ...
随机推荐
- thymeleaf 语法
一.语法: 1. 简单表达式 (simple expressions) ${...} 变量表达式 *{...} 选择变量表达式 #{...} 消息表达式 @{...} 链接url表达式 2.字 ...
- Angular 常用命令行
1. ng -v 查看angular-cli是否安装成功.angular-cli的版本号 2. ng new 项目名称 新建angular项目 3. ng g class 类名 动态生成类文件: 4. ...
- Angular 文件上传、下载
1. 文件上传 本地可同时选择多个文件 将本地所选择的文件列出来 单个文件上传至服务器: 删除本地选择的文件 样式使用了bootstrap的样式 1. html - file.component.ht ...
- .NET Core 3.0 可卸载程序集原理简析
因为最近在群里被问到如何理解 .NET Core 3.0 可卸载程序集,所以就写了这篇简单的分析. 因为时间实在很少,这篇文章只简单的罗列了相关的代码,请配合官方说明文档理解. 另外,书籍<.N ...
- Scala 学习笔记之函数(3)
class student{ def sayHello(name: => String){ println(s"Hello, $name, welcome $name") } ...
- bat脚本自动安装Jmeter&Jdk
一句话能解决的事情,绝对不要写一篇文章:一篇文章能解决的事情,绝对不要使用各种工具:一个工具能解决的事情,绝对不要跑东跑西…… 文章主要介绍脚本如何下载.安装.配置Jmeter&Jdk. 不多 ...
- 从输入URL到页面渲染完成 -戈多编程
1.输入URL地址 2.浏览器根据域名查询IP地址 3.浏览器发送HTTP请求到web服务器 4.服务器返回一个永久重定向响应 5.浏览器会跟踪重定向地址 6.服务器处理请求 7.服务器返回一个HTM ...
- pod setup 不顺利
JerryMacBook:~ jerry$ pod setup Setting up CocoaPods master repo $ /usr/bin/git clone https://github ...
- CentOS 7 的 systemctl 命令
Centos 7.* 使用 Systemd 进行系统初始化,因此,Centos 7.* 中我们可以使用 systemctl 管理系统中的服务. systemctl 管理的服务均包含了一个以 .serv ...
- Faker——生成测试数据的PHP类库
工作上用的是TP框架,每次测试功能的时候都要手动添加测试数据,词穷起名总是起一些test1.test2这种low到爆炸的用户名,这让我很难受.稍微翻阅了一些资料,发现laravel有一个生成测试数据的 ...