dcsimg
Sending simple attachments — I mean, in real life
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Nic_Daniau
Posted On:   Tuesday, August 12, 2003 03:50 AM

OK, so I just spent 3 days on that. Maybe I'm a bit thick, but for the benefit of those as thick as I am, here it is. My mission was to send simple attachment of which I knew the name and the content-type. Books and FAQs — incl. jguru — always give the same boring example of sending an attachment from a file on the file system. In real life, sorry, but sending C: est.jpg won't do. The question is: how to send an attachment from a byte[] (or a similar type of data, held in memory or somewhere else, but NOT on the file system). The answer is simple but involves a class which does not exist in J2EE API... Sending emails involves javax.activation, javax.mail and javax.mail.internet. For what I wanted to do, I needed an implemen   More>>

OK, so I just spent 3 days on that. Maybe I'm a bit thick, but for the benefit of those as thick as I am, here it is.


My mission was to send simple attachment of which I knew the name and the content-type. Books and FAQs — incl. jguru — always give the same boring example of sending an attachment from a file on the file system. In real life, sorry, but sending C: est.jpg won't do. The question is: how to send an attachment from a byte[] (or a similar type of data, held in memory or somewhere else, but NOT on the file system).


The answer is simple but involves a class which does not exist in J2EE API... Sending emails involves javax.activation, javax.mail and javax.mail.internet. For what I wanted to do, I needed an implementation of java.activation.DataSource that could work with byte[], call it ByteArrayDataSource. Guess what? there's no such thing in javax.activation. Why? don't ask me. The only implementation in this package is FileDataSource that works from the file system, hence the book examples...


So, the game is to get hold of our ByteArrayDataSource. Once you know what you're looking for, invariably you find it. org.apache.soap.util.mime.ByteArrayDataSource is probably as good a choice as any.


The code below speaks for itself. It details how to create the BodyPart for the attachment. I start from a FormFile, which is a handy container for an uploaded file in the Struts framework. But the example will apply in all cases where you know the content of the attachment, its name and its content type.

			
org.apache.struts.upload.FormFile file;

byte[] data = file.getFileData();
String contentType = file.getContentType();
String name = file.getFileName();

javax.mail.internet.MimeBodyPart attachment = new javax.mail.internet.MimeBodyPart();
javax.activation.DataSource dataSource = new org.apache.soap.util.mime.ByteArrayDataSource(data, contentType);
javax.activation.DataHandler dataHandler = new javax.activation.DataHandler(dataSource));
attachment.setDataHandler(dataHandler);
attachment.setFileName(name);

HTH    <<Less

Re: Sending simple attachments &mdash; I mean, in real life

Posted By:   Anonymous  
Posted On:   Tuesday, August 12, 2003 04:02 AM

You could perhaps find a more simpler class (at least you don't need to use apache soap jar) at www-sor.inria.fr/~java/classes/javamail/demo/ByteArrayDataSource.java. This class is used in several projects, and we have been using it for about 2 years.



Bye,

David
About | Sitemap | Contact