Domanda

I am 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); 
    } 
}

unfortunately, when I send message charset information is 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 cannot find-out why it is not adding charset information. Encoding is corect, but in email client you must select it manually in some client.

Any help?

Thanks in advance!

Konrad

È stato utile?

Soluzione

So, after few hours of trying all posibilities, my guess is that this might be a bug in James. But 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.

When I override header manually:

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top