如何通过电子邮件发送文件而不先使用 spring 引导将其存储在存储器中?

How to send a file via email without storing it in storage first using spring boot?

我正在开发 spring 启动应用程序。在我的项目中,我正在创建一个 .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);
   }

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

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

正在创建 Excel 文件

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

将其写入字节数组
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
    workbook.write(bos);
} finally {
    bos.close();
}
byte[] excelFileAsBytes = bos.toByteArray();

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

在我的 e-mailer 代码中(与您的几乎相同,但我不想 copy/paste 您的 e-mail 地址不小心),我替换了这些行:

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

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

ByteArrayResource resource = new ByteArrayResource(excelFileAsBytes);
helper.addAttachment("Invoice.xlsx", resource);

这给了我一个 e-mail,其中包含 Excel 文件作为附件。