title: Sending a mail with attachment in Java without needing a physical attachment file
body: <p>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, <a href="http://example.com/technical_guide.pdf" rel="nofollow">http://example.com/technical_guide.pdf</a>)</p>
  
  <p>The first naive implementation was as follows:</p>
  
  <ol>
  <li>Fetch the bytes of the file from the url and create an inputstream</li>
  <li>Create a file from that inputstream</li>
  <li>add the file as an attachment to a mailmessage</li>
  <li>send the message</li>
  </ol>
  
  <p>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:</p>
  
  <ol>
  <li>Create an URLDataSource from the file URL</li>
  <li>add the data source as attachment to a mailmessage</li>
  <li>send the message</li>
  </ol>
  
  <p>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.</p>
  
  <p>Can anybody confirm that no physcial files are created in this process (not even under hidden folders like <code>users/appdata/</code>, <code>/catalina_home/</code>, <code>windows/tmp/</code> 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?</p>