*/
private static Message transform(Message original) throws IOException, ParseException {
// Create a copy of the template. The copy can be modified without
// affecting the original.
MessageBuilder builder = new DefaultMessageBuilder();
Message message = builder.newMessage(original);
// In this example we know we have a multipart message. Use
// Message#isMultipart() if uncertain.
Multipart multipart = (Multipart) message.getBody();
// Insert a new text/plain body part after every body part of the
// template.
final int count = multipart.getCount();
for (int i = 0; i < count; i++) {
String text = "Text inserted after part " + (i + 1);
BodyPart bodyPart = createTextPart(text);
multipart.addBodyPart(bodyPart, 2 * i + 1);
}
// For no particular reason remove the second binary body part (now
// at index four).
Entity removed = multipart.removeBodyPart(4);
// The removed body part no longer has a parent entity it belongs to so
// it should be disposed of.
removed.dispose();
// Set some headers on the transformed message
message.createMessageId(HOSTNAME);
message.setSubject("Transformed message");
message.setDate(new Date());
message.setFrom(AddressBuilder.DEFAULT.parseMailbox("John Doe <jdoe@machine.example>"));
return message;
}