专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > 人工智能

javamail遍历的模式拿到邮件的正文和所有附件

发布时间:2011-07-03 09:15:55 文章来源:www.iduyao.cn 采编人员:星星草
javamail遍历的方式拿到邮件的正文和所有附件
                        try {
                            Object content = message.getContent();
                            if (content instanceof String) {
                                Log.v("Content", content.toString());
                            }else {
                                Multipart mp = (Multipart) content;
                                for (int i = 0; i < mp.getCount(); i++) {
                                    BodyPart bodyPart = mp.getBodyPart(i);
                                    iteratorAttachment(bodyPart);
                                }
                            }
                        } catch (IOException e) {
                            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                        }


   private void iteratorAttachment(Part p) throws
            MessagingException, IOException {
        if (p.isMimeType("text/*")) {
            String s = (String)p.getContent();
            boolean textIsHtml = p.isMimeType("text/html");
            //正文
            return ;
        }
        String disposition = p.getDisposition();
        if ((disposition != null) &&
                ((disposition.equals(Part.ATTACHMENT) ||
                        (disposition.equals(Part.INLINE))))) {
            //附件
            // part.getFileName(), part.getInputStream()
            return ;
        }

        if (p.isMimeType("multipart/alternative")) {
            // prefer html text over plain text
            Multipart mp = (Multipart)p.getContent();

            for (int i = 0; i < mp.getCount(); i++) {
                Part bp = mp.getBodyPart(i);
                iteratorAttachment(bp);
            }
        } else if (p.isMimeType("multipart/*")) {
            Multipart mp = (Multipart)p.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
                iteratorAttachment(mp.getBodyPart(i));
            }
        }
    }
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: