예제 #1
0
  /**
   * 对复杂邮件的解析
   *
   * @param multipart
   * @throws MessagingException
   * @throws IOException
   */
  public static void parseMultipart(Multipart multipart) throws MessagingException, IOException {
    int count = multipart.getCount();
    System.out.println("couont =  " + count);
    for (int idx = 0; idx < count; idx++) {
      BodyPart bodyPart = multipart.getBodyPart(idx);
      System.out.println(bodyPart.getContentType());
      if (bodyPart.isMimeType("text/plain")) {
        System.out.println("plain................." + bodyPart.getContent());
      } else if (bodyPart.isMimeType("text/html")) {
        System.out.println("html..................." + bodyPart.getContent());
      } else if (bodyPart.isMimeType("multipart/*")) {
        Multipart mpart = (Multipart) bodyPart.getContent();
        parseMultipart(mpart);

      } else if (bodyPart.isMimeType("application/octet-stream")) {
        String disposition = bodyPart.getDisposition();
        System.out.println(disposition);
        if (disposition.equalsIgnoreCase(BodyPart.ATTACHMENT)) {
          String fileName = bodyPart.getFileName();
          InputStream is = bodyPart.getInputStream();
          copy(is, new FileOutputStream("D:\\" + fileName));
        }
      }
    }
  }
예제 #2
0
  private static void readMultiPart(MultipartResult res, MimeMultipart multipart)
      throws MessagingException {
    for (int i = 0; i < multipart.getCount(); i++) {
      BodyPart part = multipart.getBodyPart(i);

      try {
        if (part.isMimeType("image/*") || part.isMimeType("application/*")) continue;
        Object content = null;
        try {
          content = part.getContent();
        } catch (UnsupportedEncodingException ex) {
          String body = ConvertUtil.inputStreamToString(part.getInputStream());
          res.body = body;
          continue;
        }

        if (part.isMimeType("text/plain")) {
          res.body = content.toString();
        } else if (part.isMimeType("text/*")) {
          res.bodyHtml = content.toString();
        } else if (content instanceof MimeMultipart) {
          readMultiPart(res, (MimeMultipart) content);
        } else if (content instanceof IMAPNestedMessage) {
          res.body = getBody((IMAPNestedMessage) content);
          return;
        } else if (content instanceof InputStream) {
          if (content instanceof IMAPInputStream) {
            String body = ConvertUtil.inputStreamToString(part.getInputStream());
            res.body = body;
          } else
            System.out.println(
                String.format(
                    "Ignoring binary content in mail: %s [%s]",
                    part.getContentType(), content.getClass()));
        } else if (part.isMimeType("message/*")) {
          res.body = content.toString();
        } else {
          System.out.println(
              String.format(
                  "Unknown content type in mail: %s [%s]",
                  part.getContentType(), content.getClass()));
        }

      } catch (IllegalStateException ex) {
        System.out.println(
            String.format("Could not read contents in mail: %s", part.getContentType()));
      } catch (UnsupportedDataTypeException ex) {
        System.out.println(
            String.format("Could not read contents in mail: %s", part.getContentType()));
      } catch (FolderClosedException ex) {
        throw ex;
      } catch (Throwable ex) {
        System.out.println(
            String.format("Error while reading mail part: %s", part.getClass().toString()));
      }
    }
  }
    /**
     * Handel multipart messages recursive until we find the first text/html message. Or text/plain
     * if html is not available.
     *
     * @param multipart the multipart portion
     * @param message the message
     * @param builder the email value builder
     * @throws MessagingException
     * @throws IOException
     */
    private void handleMultipart(
        Multipart multipart, Message message, ValueBuilder<EmailValue> builder)
        throws MessagingException, IOException {
      String body = "";

      String contentType = cleanContentType(multipart.getContentType());

      for (int i = 0, n = multipart.getCount(); i < n; i++) {
        BodyPart part = multipart.getBodyPart(i);

        String disposition = part.getDisposition();

        if ((disposition != null)
            && ((disposition.equalsIgnoreCase(Part.ATTACHMENT)
                || (disposition.equalsIgnoreCase(Part.INLINE))))) {
          builder
              .prototype()
              .attachments()
              .get()
              .add(createAttachedFileValue(message.getSentDate(), part));

        } else {
          if (part.isMimeType(Translator.PLAIN)) {
            // if contents is multipart mixed concatenate text plain parts
            if ("multipart/mixed".equalsIgnoreCase(contentType)) {
              body += (String) part.getContent() + "\n\r";
            } else {
              body = (String) part.getContent();
            }
            builder.prototype().content().set(body);
            builder.prototype().contentType().set(Translator.PLAIN);

          } else if (part.isMimeType(Translator.HTML)) {
            body = (String) part.getContent();
            builder.prototype().contentHtml().set(body);
            builder.prototype().contentType().set(Translator.HTML);

          } else if (part.isMimeType("image/*")) {
            builder
                .prototype()
                .attachments()
                .get()
                .add(createAttachedFileValue(message.getSentDate(), part));

          } else if (part.getContent() instanceof Multipart) {
            handleMultipart((Multipart) part.getContent(), message, builder);
          }
        }
      }
      // if contentHtml is not empty set the content type to text/html
      if (!Strings.empty(builder.prototype().contentHtml().get())) {
        builder.prototype().content().set(builder.prototype().contentHtml().get());
        builder.prototype().contentType().set(Translator.HTML);
      }
    }
예제 #4
0
 public String parseMultipart(BodyPart msg) throws MessagingException, IOException {
   if (msg.isMimeType("text/*")) {
     return msg.getContent().toString();
   } else {
     Multipart mpart = (Multipart) msg.getContent();
     return parseMultipart(mpart.getBodyPart(0));
   }
 }
예제 #5
0
  private static int getPoint(BodyPart p, String[] preferences) throws MessagingException {
    if (p == null) {
      return 0;
    }

    for (int i = 0; i < preferences.length; i++) {
      if (p.isMimeType(preferences[i])) {
        return preferences.length + 1 - i;
      }
    }

    return 1;
  }
 public void saveEmailFiles(HttpServletRequest request, HttpServletResponse response, Part part)
     throws Exception {
   String fileName = "";
   if (part.isMimeType("multipart/*")) {
     Multipart mp = (Multipart) part.getContent();
     for (int i = 0; i < mp.getCount(); i++) {
       BodyPart mpart = mp.getBodyPart(i);
       String disposition = mpart.getDisposition();
       if ((disposition != null)
           && ((disposition.equals(Part.ATTACHMENT)) || (disposition.equals(Part.INLINE)))) {
         fileName = mpart.getFileName();
         if (fileName != null) {
           if (fileName.toLowerCase().indexOf("gb2312") != -1
               || fileName.toLowerCase().indexOf("gbk") != -1) {
             fileName = MimeUtility.decodeText(fileName);
           }
           if (request.getHeader("User-Agent").contains("Firefox")) {
             response.addHeader("content-disposition", "attachment;filename=" + fileName);
           } else if (request.getHeader("User-Agent").contains("MSIE")) {
             response.addHeader(
                 "content-disposition",
                 "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
           }
           System.out.println("saveFile1");
           saveFile(response, fileName, mpart.getInputStream());
         }
       } else if (mpart.isMimeType("multipart/*")) {
         saveEmailFiles(request, response, mpart);
       } else {
         fileName = mpart.getFileName();
         if (fileName != null) {
           if (fileName.toLowerCase().indexOf("gb2312") != -1
               || fileName.toLowerCase().indexOf("gbk") != -1) {
             fileName = MimeUtility.decodeText(fileName);
           }
           if (request.getHeader("User-Agent").contains("Firefox")) {
             response.addHeader("content-disposition", "attachment;filename=" + fileName);
           } else if (request.getHeader("User-Agent").contains("MSIE")) {
             response.addHeader(
                 "content-disposition",
                 "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
           }
           System.out.println("saveFile2");
           saveFile(response, fileName, mpart.getInputStream());
         }
       }
     }
   } else if (part.isMimeType("message/rfc822")) {
     saveEmailFiles(request, response, (Part) part.getContent());
   }
 }
예제 #7
0
 @Override
 public void processInputStream(InputStream is) throws Exception {
   try {
     MimeMultipart multiPart =
         new MimeMultipart(new ByteArrayDataSource(is, MULTI_PART_MIME_TYPE));
     int count = multiPart.getCount();
     for (int part = 0; part < count; part++) {
       BodyPart body = multiPart.getBodyPart(part);
       if (body.isMimeType(OUTPUT_MIME_TYPE)) {
         this.inputStream = body.getInputStream();
         break;
       }
     }
   } catch (Exception e) {
     this.inputStream = getErrorResponseStream();
   }
 }