UnsupportedEncodingException when invoking getContent() on a bodypart that contains text data

March 20th, 2009

Applies to:
NA

Description:
Occurs when invoking getContent() method on a bodypart that contains text data.

Cause:
Textual bodyparts (i.e., bodyparts whose type is “text/plain”, “text/html”, or “text/xml”) return Unicode String objects when getContent() is used. Typically, such bodyparts internally hold their textual data in some non Unicode charset. JavaMail (through the corresponding DataContentHandler) attempts to convert that data into a Unicode string. The underlying JDK’s charset converters are used to do this. If the JDK does not support a particular charset, then the UnsupportedEncodingException is thrown.

admin JAVA-Mail

  1. admin
    March 20th, 2009 at 10:28 | #1

    In this case, you can use the getInputStream() method to retrieve the content as a stream of bytes. For example:

    String s;
    if (part.isMimeType(“text/plain”)) {
    try {
    s = part.getContent();
    } catch (UnsupportedEncodingException uex) {
    InputStream is = part.getInputStream();
    /*
    * Read the input stream into a byte array.
    * Choose a charset in some heuristic manner, use
    * that charset in the java.lang.String constructor
    * to convert the byte array into a String.
    */
    s = convert_to_string(is);
    } catch (Exception ex) {
    // Handle other exceptions appropriately
    }
    }

    There are some commonly used charsets that the JDK does not yet support.
    You can also add an alias for an existing charset already supported by the JDK so that it will be known by an additional name. You can create a charset provider for the “bad” charset name that simply redirects to an existing charset provider; see the following code. Create an appropriate CharsetProvider subclass and include it along with the META-INF/services file and the JDK will find it. Obviously you could get significantly more clever and redirect all unknown charsets to “us-ascii”, for instance.

    ==> TestCharsetProvider.java < ==
    import java.nio.charset.*;
    import java.nio.charset.spi.*;
    import java.util.*;

    public class TestCharsetProvider extends CharsetProvider {
    private static final String badCharset = "cp-1252";
    private static final String goodCharset = "cp1252";

    public Charset charsetForName(String charset) {
    if (charset.equals(badCharset))
    return Charset.forName(goodCharset);
    return null;
    }

    public Iterator charsets() {
    return null;
    }
    }

    ==> META-INF/services/java.nio.charset.spi.CharsetProvider <==
    TestCharsetProvider

  1. No trackbacks yet.