我正在尝试使用看起来像这样的代码记录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: 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
我做错了什么?
答案 0 :(得分:0)
只需使用msg.writeTo(outStream);
答案 1 :(得分:0)
请看看这个https://stackoverflow.com/a/34689614/578855
MimeMultiPart似乎可以包含那些本身就是MimeMultiPart的主体部分,因此您必须递归地读取内容。