Search code examples
javacharacter-encodingjakarta-mail

Javax mail decoding special characters


 BodyPart b = mp.getBodyPart(j);
        String mimeType2 = b.getContentType();

        Object o2 = b.getContent();

        System.out.println("O2 is " + o2);
        if ((o2 instanceof String))
        {
        return (String) o2;
        }

I am using this code to read the body from the email from a multi-part message; it works fine but when it shows ??? for special characters in subject and message.

I have browsed stackoverflow and googled and I do not see a straight forward way to decode in utf-8 format.

I used decodeText in mimeutility but it still shows ???

Whats the right way?


Solution

  • One sees ? when a (Unicode) character cannot be written to bytes having a limited encoding.

    Under Windows System.out typically uses a single byte Windows ANSI encoding.

    In general check how the code outputs text (String/Reader) as binary data (byte[]/OuputStream).

    For testing write it to a file, as you can check that with a programmers editor like JEdit or NotePad++ that can handle encodings.

    Check that you do not use overloaded methods where a version with encoding exists too. Refrain from FileWriter.

    void dumpString(String s) {
        for (int i = 0; i < s.length(); ++i) {
            System.out.printf("%04x ", 0xFFFF & (int)s.charAt(i));
        }
        System.out.println(s);
    }