Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calling setText or setContent multiple times on BodyPart clears headers

843834May 25 2008 — edited May 26 2008
Are you supposed to be able to re-use a MimeMessage, changing the body parts and sending it multiple times?

When I try the following, the content-type and content-transfer-encoding headers are stripped from the html body part:
MimeMessage msg = new MimeMessage(Session.getDefaultInstance(System.getProperties()));
msg.setFrom(new InternetAddress("sam@example.com"));
msg.setSubject("TEST");
msg.setRecipient(Message.RecipientType.TO, new InternetAddress("ssb_TEST@example.com"));
final MimeMultipart multipart = new MimeMultipart();
final MimeBodyPart htmlPart = new MimeBodyPart();
htmlPart.setText("<html><body>This is an html part</body></html>", null, "html");
multipart.addBodyPart(htmlPart);
msg.setContent(multipart);
Transport.send(msg);
// now call setText on the html part and send the message again
htmlPart.setText("<html><body>This is another html part, overwriting the first</body></html>", null, "html");
Transport.send(msg);
I end up with a message that looks like this:
Content-Type: multipart/mixed; 
	boundary="----=_Part_0_10496062.1211756263858"
Date: Sun, 25 May 2008 18:59:42 -0400

------=_Part_0_10496062.1211756263858
X-TESTHEADER: test

<html><body>This is another html part, overwriting the first</body></html>
------=_Part_0_10496062.1211756263858--
Which renders as plain-text in my browser. I know I can re-create the entire MimeMessage and body parts, but my real-world example is significantly more complicated. Another way to get it to work is to explicitly set the content-type and content-transfer-encoding headers on the HTML part after changing the content. I'm not sure what to set the content-transfer-encoding to, however (I'd like this to work for multiple encodings)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 23 2008
Added on May 25 2008
2 comments
628 views