I spent an hour chasing down the solution to this issue, so I figured I’d post it (or at least what worked for me). Basically, I have a multi-part message that can have different content encodings for each text part. I want to send this message via javamail. Now, there’s support for setting content as type ‘text/plain’ with a different character set, but if you want to add a part that is a different subtype of text to your message, there is no convenience method. However, this mail message had an example of how to specify html content and a character set:

MimeBodyPart htmltext = new MimeBodyPart();
htmltext.setContent(someDanishChars, "text/html; charset=\"ISO-8859-1\"");

(The author had some issues with this method in different app servers; it works fine for me in a stand alone java program.)

These additional parameters to the ‘Content-Type’ header are laid out, for text documents, in section 4.1 of RFC 2046. Here’s a collection of helpful email related RFCs. Additionally, section 5.1 of RFC 2045 outlines the way to add parameters and gives examples of the charset parameters.

5 thoughts on “Setting the content encoding for HTML message parts with Javamail

  1. Jubz says:

    Very helpful. U just saved me a whole hour …

  2. Aleh says:

    Thank you for this post

  3. JWebuser says:

    Thanks for this post!
    This helped me a lot.

  4. Souad says:

    thank you for this post it saved my time

  5. Isra says:

    Thanks for this post, it’s a life-and-time saver!!

Comments are closed.


© Moore Consulting, 2003-2019