首页 > 解决方案 > 在 Spring Boot 中发送带有多个附件的电子邮件

问题描述

我正在创建一个用于在 Spring Boot 中发送电子邮件的 api。我可以使用以下 api 在电子邮件中成功发送附件

@PostMapping("/send")
public void sendMail(@RequestParam(value = "receiver") String receiver,
        @RequestParam(value = "subject") String subject, @RequestParam(value = "content") String content,
        @RequestParam(value = "file", required = false) MultipartFile file) {
    mailService.send(receiver, subject, content, file);
}

但是一封电子邮件可以有多个附件。因此,使用此链接作为参考,我将代码更新为

@PostMapping("/send")
public void sendMail(@RequestParam(value = "receiver") String receiver,
        @RequestParam(value = "subject") String subject, @RequestParam(value = "content") String content,
        @RequestParam(value = "files", required = false) MultipartFile[] files) {
    mailService.send(receiver, subject, content, files);
}

有了这个,我可以从 Swagger UI 添加多个图像

更新:我在 Swagger 中获得以下表格,我可以从中上传图片

大摇大摆的 UI 表单

但是当我提交表单时,我发现 files 中的值现在为 null 而不是文件数组。

我错过了什么?

标签: spring-bootswagger-uiemail-attachmentsmultipart

解决方案


正如@MebinJoe 提到的,这是招摇的问题。无法用 swagger 解决问题,但最终使用 Postman 来测试上面的代码。多个文件已成功附加并通过电子邮件发送。


推荐阅读