Skip to Main Content

Java SE (Java Platform, Standard Edition)

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!

How to embed image in html when using JTextPane, HTMLEditorKit, JavaMail

843806Nov 1 2007
I am developing a specialized e-mail client that has to be able to send e-mail messages (in html format with images and attachments) that can be read by standard e-mail clients, and receive similar messages from standard e-mail clients.

I have everything working except for embedding images.

Images are to appear in the document mixed in with the text, but that's not what I mean by "embed". By "embed" I mean that the image itself is encoded within the html.

Here's a bit of code to set the context.

JTextPane bodyPane = new JTextPane();
bodyPane.setEditorKit(htmlEditorKit);
bodyPane.setTransferHandler(new DropHandler());
bodyPane.setDocument(new HTMLDocument());

In the DropHandler I have the following:

HTMLDocument htmlDoc = (HTMLDocument) bodyPane.getDocument();
HTMLEditorKit htmlKit = (HTMLEditorKit) bodyPane.getEditorKit();
int caretPos = bodyPane.getCaretPosition();

At this point get "filename", the full path to an image file that has been dropped onto bodyPane.

String htmlString = "<img src=\"file:///" + filename.replace("\\","/") + "\">";
htmlKit.insertHTML(htmlDoc, caretPos, htmlString, 0, 0, HTML.Tag.IMG);

This works just fine. The image is displayed in the document at the point of insertion.

However, the image itself is an external file. I need to send an e-mail with this image and could send the image file as an attachment, but I obviously can't assume that the recipient of the email is going to save the attachment into the proper location.

What is the best way to do this?

There is a technique for embedding the actual image in the html, using this syntax:

String htmlString = "<a href=\"data:image/png;base64,---mimed png image here--\" alt=\"Red dot\"></a>";

I can't get this to work in JTextPane, so perhaps it's not supported. Also, there may be limitations on the size of an image. And apparently Internet Explorer doesn't support this.

From what I read, using a "content identifier", that is, "cid:" and adding the image as another part of the e-mail message is probably the thing to do. I haven't taken the time to explore this yet.

Does JTextPane support cid? Is that a nonsense question? I suspect what I need to do when the message is being composed is use absolute paths to local files with the images, and then when assembling the e-mail, attach the files and rewrite the html to build in the cid linkage. Similarly, when reading a message with cid's, the thing to do is save the body parts as files and rewrite the cid to point to the actual file.

I have seen others ask similar questions, one as recently as last December, and the reply was to refer to a Sun tutorial. But that tutorial doesn't address the full problem of 1) inserting an image into a document (instead of attaching an image to a component) and 2) sending it as an e-mail message.

I have yet to see an example that shows the complete package.

Am I right that the data option I mentioned is not going to work?

Am I right that cid is the best approach, and do I understand how to use it?

Is there something else?

Thanks

John
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 29 2007
Added on Nov 1 2007
0 comments
766 views