2

What is the easiest way to convert a MIME e-mail containing raw 8bit parts to a RFC822 compliant message containing only 7bit parts ?
The parts have to be automatically converted to "Content-Transfer-Encoding: quoted-printable".

My app is developed in Java. But a command-line tool would be great. I tried reformime but this tool seems buggy and doesn't rewrite message properly :-(

Thanks for any help,
Olivier

Antares
  • 177
  • 2
  • 12

1 Answers1

2

JavaMail seems like a good solution. Create a MimeMessage from your file, find the body parts whose content transfer encodings you want to change, call MimeBodyPart.setHeader("Content-Transfer-Encoding", "quoted-printable"), and write the resulting message out via MimeMessage.writeTo().

Something along the lines of this:

Session session = Session.getInstance(new Properties());
MimeMessage mm = new MimeMessage(new FileInputStream(msgfile));
// assuming we know that it's a multipart; otherwise, check Content-Type first...
MimeMultipart multi = (MimeMultipart) mm.getContent();
for (int i = 0; i < multi.getCount(); i++) {
    MimeBodyPart mbp = (MimeBodyPart) multi.getBodyPart(i);
    mbp.setHeader("Content-Transfer-Encoding", "quoted-printable");
}
mm.saveChanges();
mm.writeTo(new FileOutputStream(outfile));

Note that MimeMessage by default will reset the Message-ID header when you've made changes to the message. If you don't want this, override MimeMessage.updateMessageID() to a no-op.

dkarp
  • 14,483
  • 6
  • 58
  • 65
  • Thank you for your response. Modifying a JavaMail's MimeMessage object doesn't work properly with nested Multiparts. With your solution, some parts are updated but some are not. I think it's a well-known problem with JavaMail (see: http://labs.consol.de/lang/en/blog/java/java-mail/removing-attachments-with-javamail/). – Antares Jan 22 '11 at 11:27
  • 1
    My sample code doesn't handle nested multiparts; it's just a guideline to show you the functions you might want to use. Have you actually written a recursive program that exhibits the issues mentioned in the article? Because messages parsed from a stream shouldn't have that issue... – dkarp Jan 22 '11 at 12:08