我正在为我的 Android 手机开发电子邮件客户端,我想获取多部分格式的消息内容。
我通过以下方式获得的文本/纯文本内容:
public String getContent(Part p) {
Object contentObject = null;
try {
if (p.isMimeType("text/plain")) {
if (!showStructure && !saveAttachments)
contentObject = p.getContent();
}
InputStream is = (InputStream) contentObject;
reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append(System.getProperty("line.separator"));
}
String everything = sb.toString();
System.out.println(everything);
return everything;
} catch (Exception e) {
return "Fehler: " + e;
}
}
如何获取多部分消息的内容文本?我已经尝试过了:
[...]
} else if (p.isMimeType("multipart/*")) {
Multipart mp = (Multipart) p.getContent();
level++;
int count = mp.getCount();
for (int i = 0; i < count; i++)
contentObject = getContent(mp.getBodyPart(i));
level--;
}
(...) 然后是 InputStream Reader,但后来我得到异常 IMAPInputStream cannot be cast to javax.mail.Multipart。