1

I'm trying to log MimeMultipart message with the code which looks like

MimeMultipart mimeMultipart = null;
try {
    mimeMultipart = (MimeMultipart) msg.getContent();
} catch (IOException e) {
    e.printStackTrace();
} catch (MessagingException e) {
    e.printStackTrace();
}
ByteArrayOutputStream outStream = new ByteArrayOutputStream();

for (int i = 0; i < mimeMultipart.getCount(); i++) {
    BodyPart bodyPart = mimeMultipart.getBodyPart(i);
    String contentType = bodyPart.getContentType();
    bodyPart.writeTo(outStream);
}

outStream.flush();
String content = new String(outStream.toByteArray());
LOGGER.info("Raw message: \r\n" + content);

but that looks like only a content of MimeMultipart message not a raw message (missing boundaries and headers).

Also I have tried just

msg.writeTo(outStream);
String content = outStream.toString();

but for some reason it just dump main message header but no body parts with headers for multiparts and looks like

Date: Fri, 31 May 2019 14:19:36 -0400 (EDT)
From: postmaster@dev.box.local
To: user@dev.box.local
Message-ID: <1293434275.167.1559326776862.JavaMail@devbox>
In-Reply-To: <984954674.27.1559326769277.JavaMail@devbox>
Subject: Re:
MIME-Version: 1.0
Content-Type: multipart/report;
  boundary="----=_Part_166_602016356.1559326776861";
  report-type=delivery-status

What I have done incorrectly?

JackTheKnife
  • 3,795
  • 8
  • 57
  • 117

2 Answers2

1

Just use msg.writeTo(outStream);

Bill Shannon
  • 29,579
  • 6
  • 38
  • 40
  • I have done that before but for some reason it just dump main message header but no body parts with headers for multiparts. – JackTheKnife Jun 04 '19 at 17:00
  • Show me. What version of JavaMail are you using? – Bill Shannon Jun 04 '19 at 20:05
  • JavaMail 1.4.3 and I have updated OP with the raw message dump – JackTheKnife Jun 05 '19 at 17:49
  • 1.4.3 is almost 10 years old, although I don't think that's the source of your problem. Still, you might want to upgrade to 1.6.3. Can you include a small program that reproduces the problem, along with the [JavaMail debug output](https://javaee.github.io/javamail/FAQ#debug)? Thanks. – Bill Shannon Jun 05 '19 at 22:02
  • To reproduce it will require to run Apache James 3.0.0 and trigger DSN bounce message. – JackTheKnife Jun 06 '19 at 12:42
  • 1
    Mostly I want to see the code you're using to write out the message and the debug output. It could be that the Apache James IMAP server is not returning the correct data. – Bill Shannon Jun 06 '19 at 17:27
0

Please have a look at this https://stackoverflow.com/a/34689614/578855

It seems like the MimeMultiPart can have bodyparts those are itself MimeMultiPart so you have to recursively read the content.

muasif80
  • 5,586
  • 4
  • 32
  • 45