This week's giveaway is in the Java/Jakarta EE forum.
We're giving away four copies of Java EE 8 High Performance and have Romain Manni-Bucau on-line!
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.
Adewale Adebusoye
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give me any code to implement this? i'm a bit lost.
Charles Lyons
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!