引入maven

  <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

  

yml配置

spring:
mail:
host: smtp.163.com #邮件服务器地址,邮箱不一样,服务器地址不一样
username: #邮箱用户名
password: #一般使用授权码
properties:
'mail.smtp.ssl.enable': 'true' #设置安全连接

  

邮件发送附件实体类

MailAttachmentDto.java

import lombok.Data;
import lombok.experimental.Accessors; @Data
@Accessors(chain = true)
public class MailAttachmentDto { /**
* 附件文件名
*/
private String fileName; /**
* 附件路径(绝对路径)
*/
private String filePath;
}

邮件发送实体

MailDto.java

import lombok.Data;
import lombok.experimental.Accessors; import java.util.List; @Data
@Accessors(chain = true)
public class MailDto { /**
* 主题
*/
private String subject; /**
* 邮件内容
*/
private String text; /**
* 收件人邮箱地址(发送给谁)
*/
private String to; /**
* 发送人(谁发的)
*/
private String from; /**
* 附件列表
*/
private List<MailAttachmentDto> attachmentDtoList;
}

发送邮件工具类

MailUtis.java

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils; import javax.annotation.PostConstruct;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.List; @Component
public class MailUtil { @Autowired
private JavaMailSenderImpl javaMailSender; public static MailUtil mailUtil; @PostConstruct
public void init() {
mailUtil = this;
mailUtil.javaMailSender = this.javaMailSender;
} /**
* 发送邮件(含有附件)
*/
public static void sendMail(MailDto mailDto) throws MessagingException {
MimeMessage mimeMessage = mailUtil.javaMailSender.createMimeMessage();
MimeMessageHelper helper = null;
helper = new MimeMessageHelper(mimeMessage, true);
helper.setSubject(mailDto.getSubject());
helper.setText(mailDto.getText());
//发送含有html代码的内容
//helper.setText(mailDto.getText(), true);
helper.setTo(mailDto.getTo());
helper.setFrom(mailDto.getFrom()); if (!CollectionUtils.isEmpty(mailDto.getAttachmentDtoList())) {
List<MailAttachmentDto> attachmentDtoList = mailDto.getAttachmentDtoList();
for (MailAttachmentDto attachmentDto : attachmentDtoList) { helper.addAttachment(attachmentDto.getFileName(), new File(attachmentDto.getFilePath()));
}
} mailUtil.javaMailSender.send(mimeMessage);
}
}

使用

    /**
* 发送邮件 测试
* @return
*/
@PostMapping(value = "/sendMail")
public String sendMail(){
MailDto dto=new MailDto();
dto.setSubject("测试邮件主题")
.setText("我是测试邮件具体内容")
.setTo("223@qq.com")
.setFrom("我是发送人"); List<MailAttachmentDto> attachmentDtoList=new LinkedList<>();
MailAttachmentDto attachmentDto=new MailAttachmentDto();
attachmentDto.setFileName("1.jpg")
.setFilePath("C:\\Users\\Pictures\\1.jpg");
dto.setAttachmentDtoList(attachmentDtoList);
try {
MailUtil.sendMail(dto);
return "success";
} catch (MessagingException e) {
e.printStackTrace();
return "error";
} }

springboot发送邮件(含附件)的更多相关文章

  1. SpringBoot 发送邮件和附件

    作者:yizhiwaz 链接:www.jianshu.com/p/5eb000544dd7 源码:https://github.com/yizhiwazi/springboot-socks 其他文章: ...

  2. springboot 发送邮件+模板+附件

    package com.example.demo; import org.junit.Test;import org.junit.runner.RunWith;import org.springfra ...

  3. C# 发送电子邮件(含附件)用到的类 system.web.mail

    主要是用到了System.Web.Mail命名空间,用到了此空间的三个类,分别是: ●MailMessage类,用于构造电子邮件●MailAttachment类,用于构造电子邮件附件●SmtpMail ...

  4. java邮件发送(含附件)

    1. [代码]java邮件发送(含附件)疯狂的IT人站长整理的:利用Java发送邮件(含附件)的例子:1.邮件发送的配置propertity文件内容如下:(utils.properties文件放在sr ...

  5. SpringBoot 发送邮件功能实现

    背景 有个小伙伴问我你以前发邮件功能怎么弄的.然后我就给他找了个demo,正好在此也写一下,分享给大家. 理清痛点 发送邮件,大家可以想一下,坑的地方在哪? 我觉得是三个吧. 第一:邮件白名单问题. ...

  6. 1.使用javax.mail, spring的JavaMailSender,springboot发送邮件

    一.java发邮件 电子邮件服务器:这些邮件服务器就类似于邮局,它主要负责接收用户投递过来的邮件,并把邮件投递到邮件接收者的电子邮箱中,按功能划分有两种类型 SMTP邮件服务器:用户替用户发送邮件和接 ...

  7. 记录一次简单的springboot发送邮件功能

    场景:经常在我们系统中有通过邮件功能找回密码,或者发送生日祝福等功能,今天记录下springboot发送邮件的简单功能 1.引入maven <!-- 邮件开发--><dependen ...

  8. springboot发送邮件,以及携带邮件附件简单使用

    可以通过springboot官方文档中Sending Email,找到类似如下java mail的使用文档 https://docs.spring.io/spring/docs/5.1.9.RELEA ...

  9. 【python】发送邮件,含附件

    def send_mail(_user,_pwd,_to): # f = open(file_new,'rb') # mail_body = f.read() # f.close() # 读取最新测试 ...

随机推荐

  1. JSOI2021 游记

    Day 0 - 2021.4.9 写一波最近的事情吧( 3 月 20 号出头 cnblogs 抽风,说 25 号恢复来着,我就囤了一堆博客在本地准备 25 号发,结果到时候就懒得动了.干脆越屯越多,省 ...

  2. iTOL进化树调图细节记录

    目录 1. 注册 2. 去枝长 3. 加图例 4. 无根树颜色 5. 导出图片 iTOL基本用法已经会了,之前记录过一点:系统发育(进化)树绘制小结.最近重用,调图时又发现了些细节,记录下备忘. 1. ...

  3. MYSQL5.8----2

    一定要按照这个顺序,where group by having order by limit 可以进行一次排序之后再一次拍寻 #存储的时候,能存数字就村数字

  4. Identity Server 4 从入门到落地(四)—— 创建Web Api

    前面的部分: Identity Server 4 从入门到落地(一)-- 从IdentityServer4.Admin开始 Identity Server 4 从入门到落地(二)-- 理解授权码模式 ...

  5. Visual Studio Code常用操作整理

    Live Server插件可以在保存html文件后实时地刷新页面 在html文件中键入"! +Tap"会生成一个html模板 保存文件:Ctrl+S 文件跳转:Ctrl+P 文件内 ...

  6. Linux驱动实践:如何编写【 GPIO 】设备的驱动程序?

    作 者:道哥,10+年嵌入式开发老兵,专注于:C/C++.嵌入式.Linux. 关注下方公众号,回复[书籍],获取 Linux.嵌入式领域经典书籍:回复[PDF],获取所有原创文章( PDF 格式). ...

  7. C语言中的使用内存的三段

    1.正文段即代码和赋值数据段 一般存放二进制代码和常量 2.数据堆段 动态分配的存储区在数据堆段 3.数据栈段 临时使用的变量在数据栈段 典例 若一个进程实体由PCB.共享正文段.数据堆段和数据栈段组 ...

  8. python web框架学习笔记

    一.web框架本质 1.基于socket,自己处理请求 #!/usr/bin/env python3 #coding:utf8 import socket def handle_request(cli ...

  9. C语言编辑链接

    库函数(Library Files)库函数就是函数的仓库,它们都经过编译,重用性不错.通常,库函数相互合作,来完成特定的任务.比如操控屏幕的库函数(cursers和ncursers库函数),数据库读取 ...

  10. Linux:expr、let、for、while、until、shift、if、case、break、continue、函数、select

    1.expr计算整数变量值 格式 :expr arg 例子:计算(2+3)×4的值 1.分步计算,即先计算2+3,再对其和乘4 s=`expr 2 + 3` expr $s \* 4 2.一步完成计算 ...