title: Add an Apache POI WorkBook as attachment to a Javamail's MimeMessage without writing in the filesystem
body: <p>I'm trying to figure out how to pass a <code>WorkBook</code> (from the Apache POI library) "directly" to a <code>MimeMessage</code> object (from the Javamail library) as an attachment, without having to write it directly in the filesystem.</p>
  
  <p>The simplest way to do this seems the following:</p>
  
  <pre><code>File attachmentSource = new File("tmpsource.xls");
  
  WorkBook tmpWorkbook = new HSSFWorkBook();
  //Do stuff with workbook
  tmpWorkBook.write(new FileOutputStream(attachmentSource));
  
  //Create all the Session, MimeMessage and MimeMultipart
  MimeBodyPart attachment = new MimeBodyPart();
  attachment.setDataHandler(new DataHandler(new FileDataSource(attachmentSource)));
  attachment.setFileName(attachmentSource.getName());
  
  //Do stuff with the message and send it
  </code></pre>
  
  <p>This way it works but I'm forced to write down the file into the FS.</p>
  
  <p>While reading the related questions I found out about <code>ByteArrayInputStream</code> and <code>ByteArrayOutputStream</code> and seemed to solve my problem (Unless the file swells up to <code>2GB</code> which seems very unlikely).</p>
  
  <p>I hope I explained myself, I think the ByteArray streams will do the trick, by the way any help or advice are appreciated!</p>
  
  <p><strong>[09/29/2011]</strong>
  I created a very simple <code>DataSource</code> implementation called (guess what) <code>ByteArrayDataSource</code>, so I have the automatic headers setup and Base64 encoding.</p>