我正在为我的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无法转换为javax.mail.Multipart。
答案 0 :(得分:-1)
您应该从请求开始,例如
ServletRequest request
在此之后,您接受请求并询问它是否是Multipart请求。我希望这有帮助
HttpServletRequest httpServletRequest = null;
if (request instanceof HttpServletRequest) {
httpServletRequest = (HttpServletRequest) request;
}
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
if (commonsMultipartResolver.isMultipart(httpServletRequest)) {
if (commonsMultipartResolver.isMultipart(null)) {
try {
MultipartHttpServletRequest multipartHttpServletRequest = commonsMultipartResolver.resolveMultipart(httpServletRequest);
Enumeration<String> enumKey = multipartHttpServletRequest.getParameterNames();
while(enumKey.hasMoreElements()) {
String key = enumKey.nextElement();
System.out.println("multipart " + key + ": " + multipartHttpServletRequest.getParameter(key));
}
} catch (Exception ex) {
System.out.println("Exception TEST: " + ex);
}
}
}