javamail

    /**  

      * 解析邮件中的嵌入资源

      */  

   public List<RelatedMap> getMailRelated(Part part, String realpath, String path, Long id) throws Exception { 

  List<RelatedMap> list = new ArrayList<RelatedMap>();

//       String contentType = part.getContentType().toLowerCase(); 

//       String ct = null;

 

       if ((part.isMimeType("application/octet-stream") || part.isMimeType("image/*")) 

       && part instanceof MimeBodyPart) { 

       String cid = ((MimeBodyPart)part).getContentID();

       cid = cid.replace("<", "").replace(">", "");

//       System.out.println("---cid:" + cid);

           InputStream is = part.getInputStream();

           BASE64DecoderStream bds = new BASE64DecoderStream(is);

 

           int buffer = bds.available();

           //文件名称=邮件id+cid

           String filename = id+cid;

           String filepath = "related"+"/"+filename;

           File dir = new File(realpath+"related");

           if (!dir.exists())dir.mkdirs();

           String fullpath = realpath + filepath;

           FileOutputStream fos = new FileOutputStream(fullpath);

           BufferedOutputStream dest = new BufferedOutputStream(fos, buffer);

           int count = 0;

           byte[] bt = new byte[part.getSize()];

           while((count = is.read(bt, 0, buffer)) != -1)

           {

             bds.decode(bt);

             dest.write(bt,0,buffer);

           }

           dest.flush();

           dest.close();

           is.close();

           fos.close();

           cid = "cid:"+cid;

           RelatedMap related = new RelatedMap(cid, filepath);

           list.add(related);

       } else if (part.isMimeType("multipart/*")) {   

           Multipart multipart = (Multipart) part.getContent();   

           int counts = multipart.getCount();   

           for (int i = 0; i < counts; i++) {   

           list.addAll(getMailRelated(multipart.getBodyPart(i), realpath, path, id));

           }   

       }

       return list;

   }    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值