I'm creating email message using Apache James. I add TextBody to message with text/html content:

for (int i = 0, bodyPartsSize = bodyParts.size(); i < bodyPartsSize; i++) {
    BodyPart bodyPart = (BodyPart) bodyParts.get(i);
    if ("text/html".equalsIgnoreCase(bodyPart.getMimeType()) {
        TextBody originalBody = (TextBody) bodyPart.getBody();
        byte[] bytes = IOUtils.toByteArray(originalBody.getInputStream());
        byte[] msgBytes = convert(bytes);

        String charset = bodyPart.getCharset();
        TextBody newBody = new StorageBodyFactory().textBody(new ByteArrayInputStream(msgBytes), charset);
        BodyPart bp = new BodyPart();
        bp.setBody(newBody, bodyPart.getMimeType());
        mp.replaceBodyPart(bp, i); 
    } 
}

regrettably, after i send message charset details are missing from output:

Original message has:

--Apple-Mail-7-654436364
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
    charset=iso-8859-2

converted:

--Apple-Mail-7-654436364
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;

I am unable to find-out why it's not adding charset information. Encoding is corect, however in email client you have to choose it by hand in certain client.

Any help?

Thanks ahead of time!

Konrad

So, after couple of hrs of attempting all posibilities, my prediction is this fact may well be a bug in James. However I found some workaround.

Although using factory method with charset specified:

TextBody newBody = new StorageBodyFactory().textBody(new ByteArrayInputStream(msgBytes), charset);

BodyPart bp = new BodyPart();
bp.setBody(newBody, bodyPart.getMimeType());
mp.replaceBodyPart(bp, i);

charset is missing from final message.

After I override header by hand:

TextBody newBody = new StorageBodyFactory().textBody(new ByteArrayInputStream(msgBytes), charset);

BodyPart bp = new BodyPart();
bp.setBody(newBody, bodyPart.getMimeType());
bp.getHeader().setField(ContentTypeFieldImpl.PARSER.parse(new RawField(FieldName.CONTENT_TYPE, "text/html; charset=" + charset), new DecodeMonitor()));
mp.replaceBodyPart(bp, i);

Charset is pesent.