views:

336

answers:

1

I have a javax.mail.Part and need to modify the content, so I have code like this:

System.out.println(part.getContentType());
String content = (String) part.getContent();
content = content.replace("a", "b");
part.setContent(content, part.getContentType());
System.out.println(part.getContentType());

This prints out text/html then text/plain. I've also tried creating a javax.activation.DataHandler with type text/html and calling part.setDataHandler(dh), but part.getContentType() still returns text/plain after that.

I can set the content and then call part.setHeader("Content-Type", "text/html"). After this part.getContentType() returns "text/html", but this seems like a hack.

Has anyone seen this? What's the best way to handle it?

A: 

Part is an Interface, so the implementation of the concrete classes would dictate what is actually happening. Knowing what sort of message you are sending might help if you would like to know what is going on under the covers.

That said, calling addHeader("Content-Type", "text/html") on your Part instance is acceptable.

akf