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!

Correct way to copy Multipart and prevent Missing start boundary Exception

843834Jan 18 2008 — edited Jan 24 2008
Hi there

I've worked through the FAQ's and forum and based on the info available tried to construct code to deal with the whole "javax.mail.MessagingException: Missing start boundary" problem. I keep on getting a ClassCastException though and was hoping you could scan through the code and let me know if you see anything. I must be missing something ... simplified extract of code below:
Multipart mp = (Multipart)part.getContent();
try {
	for (int i = 0; i < mp.getCount(); i++) {
		processBodyPart(wrapper, mp.getBodyPart(i), i);
	}
} catch (MessagingException e) {
	// Converting Msg to a Copy (to fix boundary)
	ByteArrayOutputStream bos = new ByteArrayOutputStream();
	part.writeTo(bos);
	byte[] bytes = bos.toByteArray();
	MimeMessage newMsg = new MimeMessage(emailAccount.getSession(), new ByteArrayInputStream(bytes));
	String ct = newMsg.getContentType();
	newMsg.removeHeader("Content-Type");
	ct = ct.substring(0, ct.indexOf(";")); // Strip out the rest, only retain the content-type 
	newMsg.setDataHandler(new DataHandler(newMsg.getContent(), ct));
	Multipart testMp = (Multipart)newMsg.getContent(); // <<-------- ClassCastException here!!
	for (int i = 0; i < testMp.getCount(); i++) {
		Part bp = testMp.getBodyPart(i);
		processBodyPart(bp);
	}
}
Much appreciated.

Marius
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 21 2008
Added on Jan 18 2008
12 comments
3,043 views