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 ...
随机推荐
- 免费申请 HTTPS 证书,开启全站 HTTPS
作者:HelloGitHub-追梦人物 文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 HTTP 报文以明文形式传输,如果你的网站只支持 HTTP 协议,那么就有可能遭受到安 ...
- Java 学习笔记之 Stop停止线程
Stop停止线程: 使用stop()方法停止线程是非常暴力的,会抛出java.lang.ThreadDeath Error,但是我们无需显示捕捉, 以下捕捉只是为了看得更清晰. public clas ...
- 02-21 决策树ID3算法
目录 决策树ID3算法 一.决策树ID3算法学习目标 二.决策树引入 三.决策树ID3算法详解 3.1 if-else和决策树 3.2 信息增益 四.决策树ID3算法流程 4.1 输入 4.2 输出 ...
- spring Bean的三种配置方式
Spring Bean有三种配置方式: 传统的XML配置方式 基于注解的配置 基于类的Java Config 添加spring的maven repository <dependency> ...
- 【CJOJ】为了博多
Description 做了个噩梦,梦见我的 n 把刀到60级会二次变身,变成一个 对推6图有xi点贡献,刷大阪城有yi点贡献 的刀,于是要把刀分成两队一队刷大阪城另一队推6图 . 但是有m对兄弟刀在 ...
- git的下载及简单使用一
git 是世界上最先进的分布式版本控制系统 常用的git网站 GitHub gitee(码云) git的下载地址 https://git-scm.com/downloads 而后根据计算机的系统选择相 ...
- 使用ipython %matplotlib inline
首先讲讲这句话的作用,matplotlib是最著名的Python图表绘制扩展库,它支持输出多种格式的图形图像,并且可以使用多种GUI界面库交互式地显示图表.使用%matplotlib命令可以将matp ...
- PHP call_user_func的一些用法和注意点
版本:PHP 5.6.28 在call_user_func的调用中: 1.参数的传递过程,并不是引用传值. 1 error_reporting(E_ERROR); // 此处不是E_ALL 2 $cu ...
- React Diff算法一览
前言 diff算法一直是React系统最核心的部分,并且由于演化自传统diff,使得比较方式从O(n^3)降级到O(n),然后又改成了链表方式,可谓是变化万千. 传统Diff算法 传统diff算法需要 ...
- django 后端开发-01 配置工作:
# Django 配置工作 1: 配置好数据库 2: 配置好模板文件路径 3: 配置好静态文件路径 4: 配置好时区 5: 配置好模板的static标签 1: 配置好数据库 1.1 配置django ...