引入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. Codeforces 429E - Points and Segments(欧拉回路)

    Codeforces 题面传送门 & 洛谷题面传送门 果然我不具备融会贯通的能力/ll 看到这样的设问我们可以很自然地联想到这道题,具体来说我们可以通过某种方式建出一张图,然后根据" ...

  2. 洛谷 P7116 - [NOIP2020] 微信步数(拉格朗日插值)

    洛谷题面传送门 我竟然独立切掉了这道题!incredible! 纪念我逝去的一上午(NOIP 总时长 4.5h,这题做了我整整 4.5h) 首先讲一下现场我想的 80 分的做法,虽然最后挂成了 65 ...

  3. 1D RKDG to shallow water equations

    RKDG to shallow water equations 1.Governing Equations \[\frac{\partial U}{\partial t} + \frac{\parti ...

  4. Nginx 编译 echo 模块

    Nginx  编译 echo 模块 echo模块下载地址:https://github.com/openresty/echo-nginx-module 查看nginx已经编译的模块, nginx -V ...

  5. 重测序(RADseq)做群体遗传分析套路

    实验材料 构建的群体,或自然群体,如各地方品种. RAD文库构建 提取DNA后,构建文库,简要步骤如下: ① 限制性内切酶TaqI酶切: ② 连接P1接头: ③ DNA随机打断片断化: ④ 目的片段回 ...

  6. linux 线程函数小结

    由于主线程已经开始跑了,次线程还在使用串口打印需要一点时间,因此打印的都是重复的. #include "pthread.h" #include "stdio.h" ...

  7. ab命令执行压力测试

    ab是Apache超文本传输协议(HTTP)的性能测试工具:设计意图是描绘当前所安装的Apache的执行性能,主要是显示你安装的Apache每秒可以处理多少个请求:ab不仅仅能进行基于apache服务 ...

  8. java输入代码

    import java.util.Scanner; public class Demo59 {    public static void main(String[] args) {        / ...

  9. iBatis查询时报"列名无效"或"找不到栏位名称"无列名的错误原因及解决方法

    iBatis会自动缓存每条查询语句的列名映射,对于动态查询字段或分页查询等queryForPage, queryForList,就可能产生"列名无效".rs.getObject(o ...

  10. python web框架学习笔记

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