JackTheKnife
JackTheKnife

Reputation: 4144

MimeMultipart message dump with boundaries

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: [email protected]
To: [email protected]
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?

Upvotes: 1

Views: 1223

Answers (2)

muasif80
muasif80

Reputation: 6006

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.

Upvotes: 0

Bill Shannon
Bill Shannon

Reputation: 29971

Just use msg.writeTo(outStream);

Upvotes: 1

Related Questions