Search code examples
javamemory-managementjakarta-mailmime-message

Best strategy to send mails with attachments, with a low memory footprint in the JVM


I would send an e-mail in Java with attached documents. Sadly my JVM server has few heap memory, can send large file at the same time. I would reduce the memory footprint while I send my e-mail with attached file.

My approach would be store the file in a temporary location and send it to the smtp server as a stream, without loading the file in the heap of the JVM.

Which implementation could guarantee me this approach ?


Solution

  • When using the JavaMail MimeBodyPart.attachFile method, or using a FileDataSource directly, JavaMail (well, actually JAF) will read the file using a FileInputStream into an 8K buffer, then write out the buffer, reusing the buffer to read the entire file. It definitely will not read the entire file into memory.