springboot发送邮件(含附件)
引入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"; } }
-----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------
(蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
分类:
JAVA
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了