我正在尝试使用如下代码记录MimeMultipart消息

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);


但这看起来仅是MimeMultipart消息的内容,而不是原始消息(缺少边界和标头)。

我也尝试过

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


但由于某种原因,它只是转储主要消息头,但没有带多部分头的正文部分,看起来像

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


我做错了什么?

最佳答案

只需使用msg.writeTo(outStream);

09-10 06:03