这个答案扩展了尤林的答案]。他提出的问题是a的内容
MimeMultipart本身可能是另一内容
MimeMultipart。
getTextFromMimeMultipart()在这种情况下,下面的方法将在内容上重复进行,直到完全解析了邮件正文为止。
private String getTextFromMessage(Message message) throws MessagingException, IOException { String result = ""; if (message.isMimeType("text/plain")) { result = message.getContent().toString(); } else if (message.isMimeType("multipart/*")) { MimeMultipart mimeMultipart = (MimeMultipart) message.getContent(); result = getTextFromMimeMultipart(mimeMultipart); } return result;}private String getTextFromMimeMultipart( MimeMultipart mimeMultipart) throws MessagingException, IOException{ String result = ""; int count = mimeMultipart.getCount(); for (int i = 0; i < count; i++) { BodyPart bodyPart = mimeMultipart.getBodyPart(i); if (bodyPart.isMimeType("text/plain")) { result = result + "n" + bodyPart.getContent(); break; // without break same text appears twice in my tests } else if (bodyPart.isMimeType("text/html")) { String html = (String) bodyPart.getContent(); result = result + "n" + org.jsoup.Jsoup.parse(html).text(); } else if (bodyPart.getContent() instanceof MimeMultipart){ result = result + getTextFromMimeMultipart((MimeMultipart)bodyPart.getContent()); } } return result;}


