Пример #1
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()));
      }
    }
  }
Пример #2
0
  public static String getBody(Message message) throws Exception {
    try {
      message.getContent(); // This might throw an exception...
    } catch (Throwable ex) {
      try {
        // Content could not be resolved for some reason - just get the raw stuff
        return ConvertUtil.inputStreamToString(message.getInputStream());
      } catch (Throwable ex2) {
        // bodystructure is corrupt - get the really raw stuff
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        message.writeTo(out);
        return out.toString();
      }
    }

    // check if message is a normal message or a Multipart message
    if (message.getContent() instanceof MimeMultipart) {
      MultipartResult res = new MailUtils.MultipartResult();
      MailUtils.readMultiPart(res, (MimeMultipart) message.getContent());
      return res.body.isEmpty() ? res.bodyHtml : res.body;
    } else {
      if (message.getContent().toString() != null) {
        return message.getContent().toString();
      }
    }
    return null;
  }