Reputation: 3736
I use JavaMailSender in a Java application to send an email with an attachment. The attachment is a file located at a website (for example, http://example.com/technical_guide.pdf)
The first naive implementation was as follows:
This worked, but step 2 had the unfortunate side-effect of creating physical files on the filesystems. Rather than deleting them (programmatically), I found that I can also pass a datasource. So now I have the following implementation:
This also works great, and I no longer see the files under my servlet container's root directory. However - I'm concerned that javaMail might still makes some files somewhere under the hood, but I'm just not aware of it.
Can anybody confirm that no physcial files are created in this process (not even under hidden folders like users/appdata/
, /catalina_home/
, windows/tmp/
etc.) and if so - explain how java is able to send mails without needing any files? Is it because all it needs is the "bytes" to send to the mailserver and it doesn't care where does bytes come from?
Upvotes: 2
Views: 3546
Reputation: 26926
You can use the MimeMessageHelper.addAttachment(String attachmentFilename, InputStreamSource inputStreamSource)
method:
helper.addAttachment("attachement", yourStream);
Basically it is not needed to have a phisical file. You can also build it in memory and stream it to the helper.
Upvotes: 4