如何使用 Spring Boot 通过电子邮件发送文件而不先将其存储在存储中?

Udh*_*ata 3 java apache-poi spring-boot

我正在开发一个 Spring Boot 应用程序。在我的项目中,我正在创建一个 .xlsx 文件,然后我必须使用 Spring Boot 通过电子邮件发送。

我可以使用 apache poi 创建该文件,但稍后通过邮件将其作为附件发送,我应该将该文件保存在本地的某个位置,然后将文件移回原处,同时将其作为附件发送。

有什么方法可以创建 .xlsx 文件并直接通过电子邮件发送它,而无需先将其保存在某处?

对于邮件,我使用'org.springframework.boot:spring-boot-starter-mail'

对于.xlsx,我使用“org.apache.poi”,名称:“poi”,版本:“4.1.2”“org.apache.poi”,名称:“poi-ooxml”,版本:“4.1.2” '

源代码

       XSSFWorkbook workbook = new XSSFWorkbook();
       List<String> s = Arrays.asList("person","animal");
       CellStyle style = workbook.createCellStyle();
       style.setWrapText(true);
       int n = 1;
       String source = s.get(0);
       Sheet sheet = workbook.createSheet(source);
       for (String i : s) {
           if(!source.equalsIgnoreCase(i)) {
               sheet = workbook.createSheet(i);
               Row header = sheet.createRow(0);

               CellStyle headerStyle = workbook.createCellStyle();

               XSSFFont font = workbook.createFont();
               font.setFontName("Arial");
               font.setBold(true);
               headerStyle.setFont(font);

               Cell headerCell = header.createCell(0);
               headerCell.setCellValue("Name");
               headerCell.setCellStyle(headerStyle);

               headerCell = header.createCell(1);
               headerCell.setCellValue("Age");
               headerCell.setCellStyle(headerStyle);
               source = i;
           }

           Row row = sheet.createRow(n);
           Cell cell = row.createCell(0);
           cell.setCellValue("Udhav Mohata");
           cell.setCellStyle(style);

           cell = row.createCell(1);
           cell.setCellValue(22);
           cell.setCellStyle(style);
           n++;
       }

       FileOutputStream outputStream = new FileOutputStream(Path_to_the_file);
       workbook.write(outputStream);
       workbook.close();
       sendMail();
   }


public void sendMail() throws MessagingException, IOException {
       MimeMessage message = mailSender.createMimeMessage();
       MimeMessageHelper helper = new MimeMessageHelper(message, true);
       helper.setFrom("spidercodie@gmail.com");
       helper.setTo("udhavmohata1@gmail.com");
       helper.setSubject("Test Mail");
       helper.setText("Hello world");
       FileSystemResource file = new FileSystemResource(path_to_the_file));
       helper.addAttachment("Invoice.xlsx", file);
       mailSender.send(message);
   }
Run Code Online (Sandbox Code Playgroud)

and*_*mes 6

这是我的建议,基于我提供的链接中的方法 - 这基本上也是 Gagravarr 在他的评论中提到的。

(这种方法对我来说效果很好,使用 gmail。)

创建 Excel 文件

我使用了你的代码,除了最后一部分。我没有将工作簿写入文件系统,而是通过java.io.ByteArrayOutputStream.

ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
    workbook.write(bos);
} finally {
    bos.close();
}
byte[] excelFileAsBytes = bos.toByteArray();
Run Code Online (Sandbox Code Playgroud)

使用我的字节数组作为附件

在我的电子邮件程序代码中(与您的代码几乎相同,但我不想意外复制/粘贴您的电子邮件地址),我替换了这些行:

FileSystemResource file = new FileSystemResource(path_to_the_file));
helper.addAttachment("Invoice.xlsx", file);

Run Code Online (Sandbox Code Playgroud)

对于这些行,使用org.springframework.core.io.ByteArrayResource

ByteArrayResource resource = new ByteArrayResource(excelFileAsBytes);
helper.addAttachment("Invoice.xlsx", resource);
Run Code Online (Sandbox Code Playgroud)

这给了我一封电子邮件,其中包含 Excel 文件作为附件。