0

我正在为我的 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。

4

1 回答 1

-1

你应该从一个请求开始,比如

ServletRequest request

在此之后,您接受请求并询问它是否是多部分请求。我希望这有帮助

 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);
            }
        }
    }
于 2014-05-28T14:36:01.093 回答