I've seen the code for javax.mail
library where you add attachments to the email doing this:
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource("C:/text.txt");
attachmentPart.setDataHandler(new DataHandler(fds));
attachmentPart.setFileName("text.txt");
multipart.addBodyPart(attachmentPart);
But this requires that the file reside somewhere on this disk.
I would like to grab an OutputStream
right from the email library and stream file contents into it directly from another place where I write to that OutputStream
.
Is this possible?
Try using ByteArrayDataSource, like this
ByteArrayOutputStream baos = //Read the output stream
DataSource aAttachment = new ByteArrayDataSource(baos.toByteArray(),"application/octet-stream");
MimeBodyPart attachmentPart = new MimeBodyPart();
attachmentPart.setDataHandler(new DataHandler(aAttachment));