The moose likes Servlets and the fly likes problem with images in web app Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "problem with images in web app" Watch "problem with images in web app" New topic

problem with images in web app

Adewale Adebusoye
Ranch Hand

Joined: Sep 28, 2005
Posts: 118 doing this web based email application which works to a large extent. Problem is that i can only read emails that are text based only.
I cant read mails that have images or graphics in them, i can only read pure text.
But i can read everything in something like yahoo or gmail.
Is there any way of rectifying this?
Thank you

"Human beings can alter their lives by altering their state of mind" William James
Charles Lyons
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
You would need to add HTML support into your application - this will give you support for basic formatting and image inclusion for all e-mails of MIME type "text/html" (which is the vast majority of formatted e-mails; currently I suspect you only support "text/plain").

Once you've done that, some e-mail clients will insist on embedding the image within a MIME part of the e-mail itself (rather than referring to an external URL). So you'll need to use the JavaMail API to extract the appropriate MIME part, cache the image on the web server and then use the cached location as the value of an HTML <img src="..." /> element.

It's a bit more work supporting HTML for obvious reasons - but less work for you than building a client application simply because you can use the browser's HTML formatting capabilities already!

Good luck, if you've any further questions please ask.

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Adewale Adebusoye
Ranch Hand

Joined: Sep 28, 2005
Posts: 118
Can you give me any code to implement this? i'm a bit lost.
Charles Lyons
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
To view an HTML formatted e-mail in your browser, all you need to do is first set the MIME type of the response to "text/html" (using ServletResponse setContentType()), then copy the main body part of the e-mail to the response. That will use your browser's HTML processor to display the formatted contents of the e-mail.

Dealing with embedded (rather than linked) images is somewhat more complex, and I don't have the time to go through it all at the moment or provide a working app. However, a rough sketch of what you'd need to do is:

(1) Check in the HTML e-mail for <img src="..." /> tags that have weird source values. These are the boundary delimiters for the multipart message (open up any HTML e-mail and you'll see what I mean). Before copying the e-mail body to the client, you'll need to replace the multipart boundary with a publicly-accessible URL on your server, for example /images/cached/{username}_{unique id}.gif (or .jpg etc.) where {username} is the username of the current client logged in and {unique id} is a unique identifier for that particular image.

(2) Now have your application remember what name it stored for the image, hunt out the MimeMultipart which contains that image (see the JavaMail API JavaDoc for more on the API details) and copy that image data to a new file in your cache, with the same name as in (1).

(3) Now your client's browser will see <img src="/images/cached/me_12.gif" /> for example, and will be able to load this image from the saved location.

There may be alternative ways to do this, but this is the one I would probably take. You might want to use a servlet to dynamically return the images in step (2) - caching will however increase performance if the images are to be viewed several times (in this case, consider a process which removes unread images from the cache to save disk space).

The API details you'll really need to look at are:

the basic message handling class
most likely "text/html", "text/plain" or "multipart"
returns an Object (String, Multipart etc.) for the e-mail content.
MimeMultipart.getBodyPart(String contentID)
returns the body part corresponding the specified "contentID". This is the multipart boundary separator used in the src attribute of an <img /> HTML tag when an embedded image is being used.

That's probably enough to get you started, aside from what you already know about how to extract text data from an e-mail. I suggest sending yourself an HTML e-mail with an embedded image (anything send from Outlook/Thunderbird should do it) and extracting only the text body part... read through it and you'll see it's just HTML with boundary separators in the <img /> tags. You should even be able to view that image straight-away in the browser (provided you use response.setContentType("text/html") where 'response' is your ServletResponse object).

Feel free to post back for further explanation, preferably with an example e-mail you would like to render. Modelling discussion around an example makes life simpler!
[ May 15, 2006: Message edited by: Charles Lyons ]
I agree. Here's the link:
subject: problem with images in web app
It's not a secret anymore!