Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Retrieve images from DB and attach them to email through javamail

843834Jun 8 2009 — edited Jun 11 2009
Hi,

I'm trying to use javamail to send emails containing text and images (HTML mail), if I use a physical file in my computer as the image source and use FileDataSource everything works just fine. The problem is that the images in my app are stored into a DB (I'm using MySQL, java struts & DAO), so in order to retrieve & display them I use a jsp that returns a byte array as shown in the example bellow
       

<img  src='getImage.jsp?itemId=<bean:write name="item" property="itemId"/>&heigth=180&width=140&imageIndex=1'/>
getImage.jsp reads the image from the DB for the specified item and returns an image of size "heigth*witdh", this works fine when invoked from any jsp or java action class, but I can't get it to work to insert an image into an email as attachment

I've tried to use ByteArrayDataSource and the mail is sent correctly and can be read but the image won't show

bds = new ByteArrayDataSource("/getImage.jsp?itemId="+itemId+"&heigth=180&width=140&imageIndex=1", "image/jpeg");
imageMime.setDataHandler(new DataHandler(bds));
imageMime.setHeader("Content-ID", "<\" + itemId + \">"); // backslashes aren't there in the real code, but if I remove them '+ itemId +' won't show 
mailParts.addBodyPart(imageMime);
I've also tried with URLDataSource but can't get it to work either, can somebody plz point me in the right direction?

thanks in advance

Edited by: informacionCubica on Jun 8, 2009 3:46 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 9 2009
Added on Jun 8 2009
4 comments
444 views