/** Return the primary text content of the message. */
  public String getContent(Part p) throws MessagingException, IOException {
    if (p.isMimeType("text/*")) {
      String s = (String) p.getContent();
      return s;
    }

    if (p.isMimeType("multipart/alternative")) {
      // prefer html text over plain text
      Multipart mp = (Multipart) p.getContent();
      String text = null;
      for (int i = 0; i < mp.getCount(); i++) {
        Part bp = mp.getBodyPart(i);
        if (bp.isMimeType("text/plain")) {
          if (text == null) text = getContent(bp);
          continue;
        } else if (bp.isMimeType("text/html")) {
          String s = getContent(bp);
          if (s != null) return s;
        } else {
          return getContent(bp);
        }
      }
      return text;
    } else if (p.isMimeType("multipart/*")) {
      Multipart mp = (Multipart) p.getContent();
      for (int i = 0; i < mp.getCount(); i++) {
        String s = getContent(mp.getBodyPart(i));
        if (s != null) return s;
      }
    }

    return null;
  }
Ejemplo n.º 2
0
  public static String getText(Part part) {

    try {
      ContentType contentType = new ContentType(part.getContentType());
      System.err.println(
          "contentType: "
              + part.getContentType()
              + ", class: "
              + part.getContent().getClass().getName());

      if (part.isMimeType("text/*")) {
        String charset = contentType.getParameter("charset");
        System.err.println("Charset: " + charset);

        return (String) part.getContent();

      } else if (part.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) part.getContent();
        for (int i = 0; i < mp.getCount(); i++) {
          String text = getText(mp.getBodyPart(i));

          if (text != null) {
            return text;
          }
        }
      }
      return null;
    } catch (ParseException e) {
      throw new RuntimeException(e);
    } catch (MessagingException e) {
      throw new RuntimeException(e);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
  /**
   * Process the MimeMessage from signed Mail to get attachments and save them to disk.
   *
   * @param mime
   * @return
   * @throws MessagingException
   * @throws IOException
   */
  private String processAttachmentsOfSignedMail(MimeMessage mime)
      throws IOException, MessagingException {
    List<Attachment> attachList = new ArrayList<Attachment>();

    // Get the content of the messsage, it's an Multipart object like a package including all the
    // email text and attachment.
    Multipart multi1 = (Multipart) mime.getContent();

    // process each part in order.
    for (int i = 0, n = multi1.getCount(); i < n; i++) {
      // unpack, get each part of Multipart, part 0 may email text and part 1 may attachment. Or it
      // is another embedded Multipart.
      Part part2 = multi1.getBodyPart(i);
      // determine Part is email text or Multipart.
      if (part2.getContent() instanceof Multipart) {
        Multipart multi2 = (Multipart) part2.getContent();
        // First, verify the quantity and size of attachments.
        boolean isValidMailMsg = this.isValidMailMsg(multi2);
        if (isValidMailMsg) {
          // process the content in multi2.
          for (int j = 0; j < multi2.getCount(); j++) {
            Part part3 = multi2.getBodyPart(j);
            if (part3.isMimeType("multipart/related")) {
              if (part3.getContent() instanceof Multipart) {
                Multipart multi3 = (Multipart) part3.getContent();
                for (int m = 0; m < multi3.getCount(); m++) {
                  Part part4 = multi3.getBodyPart(m);
                  if (!part4.isMimeType("multipart/alternative")) {
                    // This is an embedded picture, save it.
                    this.saveAttachment(part4, attachList);
                  }
                }
              }
            } else {
              // Save the attachment.
              String disposition = part3.getDisposition();
              if (disposition != null && Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
                this.saveAttachment(part3, attachList);
              }
            }
          }
        }
      } else {
        // Process the attachment.(This is a certificate file.)
        String disposition = part2.getDisposition();
        if (disposition != null && Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
          this.saveAttachment(part2, attachList);
        }
      }
    }
    return JSONArray.fromObject(attachList).toString();
  }
Ejemplo n.º 4
0
 public static Part getTextFromMultipart(Part p) throws MessagingException, IOException {
   Part text = null;
   if (p.isMimeType("multipart/*")) {
     System.out.println("rec");
     Multipart mp = (Multipart) p.getContent();
     int count = mp.getCount();
     for (int i = 0; i < count; ) return getTextFromMultipart(mp.getBodyPart(i));
   } else if (p.isMimeType("text/plain")) {
     System.out.println("text");
     text = p;
   }
   return text;
 }
 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());
   }
 }
  /** Return the primary text content of the message. */
  private static String getText(Part p) throws MessagingException, IOException {

    if (p.isMimeType("text/enriched")) {
      InputStream is = (InputStream) p.getContent();
      StringWriter writer = new StringWriter();
      IOUtils.copy(is, writer);
      return writer.toString();
    }

    if (p.isMimeType("text/*") && !p.isMimeType("text/enriched")) {
      p.getContentType();

      try {
        String s = (String) p.getContent();
        textIsHtml = p.isMimeType("text/html");
        return s;
      } catch (ClassCastException e) {
        InputStream is = (InputStream) p.getContent();
        StringWriter writer = new StringWriter();
        IOUtils.copy(is, writer);
        textIsHtml = p.isMimeType("text/html");
        return writer.toString();
      }
    }

    if (p.isMimeType("multipart/alternative")) {
      // prefer html text over plain text
      Multipart mp = (Multipart) p.getContent();
      String text = null;
      for (int i = 0; i < mp.getCount(); i++) {
        Part bp = mp.getBodyPart(i);
        if (bp.isMimeType("text/plain")) {
          if (text == null) text = getText(bp);
          return text;
          // continue;
        } else if (bp.isMimeType("text/html")) {
          String s = getText(bp);
          if (s != null) return s;
        } else {
          return getText(bp);
        }
      }
      return text;
    } else if (p.isMimeType("multipart/*")) {
      Multipart mp = (Multipart) p.getContent();
      for (int i = 0; i < mp.getCount(); i++) {
        String s = getText(mp.getBodyPart(i));
        if (s != null) return s;
      }
    }

    return null;
  }
Ejemplo n.º 7
0
  public void getPartContent(Part part, StringBuilder sb) throws Exception {
    if (part.isMimeType("text/*")) {
      String s = (String) part.getContent();
      if (s != null) {
        sb.append(s).append(" ");
      }
    } else if (part.isMimeType("multipart/*")) {
      Multipart mp = (Multipart) part.getContent();
      int count = mp.getCount();
      if (part.isMimeType("multipart/alternative")) {
        count = 1;
      }

      for (int i = 0; i < count; i++) {
        getPartContent(mp.getBodyPart(i), sb);
      }
    } else if (part.isMimeType("message/rfc822")) {
      getPartContent((Part) part.getContent(), sb);
    }
  }
  /**
   * Process the MimeMessage from simple Mail to get attachments and save them to disk.
   *
   * @param mime
   * @return
   * @throws IOException
   * @throws MessagingException
   */
  private String processAttachmentsOfSimpleMail(MimeMessage mime)
      throws IOException, MessagingException {
    List<Attachment> attachList = new ArrayList<Attachment>();

    if (mime.getContent() instanceof Multipart) {
      // Get the content of the messsage, it's an Multipart object like a package including all the
      // email text and attachment.
      Multipart multi = (Multipart) mime.getContent();
      // First, verify the quantity and size of attachments.
      boolean isValidMailMsg = this.isValidMailMsg(multi);
      if (isValidMailMsg) {
        // process each part in order.
        for (int i = 0, n = multi.getCount(); i < n; i++) {
          // unpack, get each part of Multipart, part 0 may email text and part 1 may attachment. Or
          // it is another embedded Multipart.
          Part part1 = multi.getBodyPart(i);
          if (part1.isMimeType("multipart/related")) {
            if (part1.getContent() instanceof Multipart) {
              Multipart multi1 = (Multipart) part1.getContent();
              for (int m = 0; m < multi1.getCount(); m++) {
                Part part2 = multi1.getBodyPart(m);
                if (!(part2.isMimeType("multipart/alternative")
                    || part2.isMimeType("text/plain")
                    || part2.isMimeType("text/html"))) {
                  // This is an embedded picture, set it as an attachment.
                  this.saveAttachment(part2, attachList);
                }
              }
            }
          } else {
            String disposition = part1.getDisposition();
            if (disposition != null && Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
              // Save the attachment if it is.
              this.saveAttachment(part1, attachList);
            }
          }
        }
      }
    }
    return JSONArray.fromObject(attachList).toString();
  }
Ejemplo n.º 9
0
  private void analizaParteDeMensaje(Part unaParte, Mensaje myMensaje) {
    try {
      // Si es multipart, se analiza cada una de sus partes recursivamente.
      if (unaParte.isMimeType("multipart/*")) {
        Multipart multi;
        multi = (Multipart) unaParte.getContent();

        for (int j = 0; j < multi.getCount(); j++) {
          analizaParteDeMensaje(multi.getBodyPart(j), myMensaje);
        }

      } else {
        // Si es texto, se escribe el texto.
        if (unaParte.isMimeType("text/*")) {
          myMensaje.agregarTextoPlano((String) unaParte.getContent());
        } else {
          salvaUnFichero(unaParte, myMensaje);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Ejemplo n.º 10
0
  // x参数来确定是以html 1 格式显示还是以plain 2
  // 调用时getPart(part,i,1);
  // 显示复杂邮件的正文内容
  public String getPart(Part part, int partNum, int x) throws MessagingException, IOException {

    String s = "";
    String s1 = "";
    String s2 = "";
    String s5 = "";
    String sct = part.getContentType();
    if (sct == null) {
      s = "part 无效";
      return s;
    }
    ContentType ct = new ContentType(sct);
    if (ct.match("text/html") || ct.match("text/plain")) {
      // display text/plain inline
      s1 = "" + (String) part.getContent() + "";
    } else if (partNum != 0) {
      String temp = "";
      if ((temp = part.getFileName()) != null) {
        s2 = "Filename: " + temp + "";
      }
    }
    if (part.isMimeType("multipart/alternative")) {
      String s6 = "";
      String s7 = "";
      Multipart mp = (Multipart) part.getContent();
      int count = mp.getCount();
      for (int i = 0; i < count; i++) {
        if (mp.getBodyPart(i).isMimeType("text/plain")) s7 = getPart(mp.getBodyPart(i), i, 2);
        else if (mp.getBodyPart(i).isMimeType("text/html")) s6 = getPart(mp.getBodyPart(i), i, 1);
      }
      if (x == 1) { // html格式的字符串
        s5 = s6;
      }
      if (x == 2) { // paint类型的字符串
        s5 = s7;
      }
      return s5;
    }
    s = s1 + s2;
    return s;
  }
Ejemplo n.º 11
0
  /**
   * @param parent
   * @return
   */
  private Part findImagePart(Part parent) {

    try {
      if (MessageUtils.isImagepart(parent)) {
        return parent;
      } else if (parent.isMimeType("multipart/*")) {
        Multipart mp;
        mp = (Multipart) parent.getContent();
        int count = mp.getCount();
        for (int i = 0; i < count; i++) {
          Part subPart = findImagePart(mp.getBodyPart(i));
          if (subPart != null) {
            return subPart;
          }
        }
      }
    } catch (MessagingException e) {
      log.error(e.getMessage(), e);
    } catch (IOException e) {
      log.error(e.getMessage(), e);
    }

    return null;
  }
  /**
   * Process the MimeMessage for signed mail with/without attachment.
   *
   * @param mime
   * @param mailMsg
   * @return
   * @throws MessagingException
   * @throws IOException
   * @throws ParseException
   */
  private String processSignedMail(MimeMessage mime, MailMessage mailMsg)
      throws IOException, MessagingException, ParseException {
    this.setMailBasicInfoForMailMsg(mime, mailMsg);

    String txtBody = null;
    String htmlBody = null;
    // Get the content of the messsage, it's an Multipart object like a package including all the
    // email text and attachment.
    Multipart multi1 = (Multipart) mime.getContent();
    // process each part in order.
    for (int i = 0, n = multi1.getCount(); i < n; i++) {
      // unpack, get each part of Multipart, part 0 may email text and part 1 may attachment. Or it
      // is another embedded Multipart.
      Part part2 = multi1.getBodyPart(i);
      // determine Part is email text or Multipart.
      if (part2.getContent() instanceof Multipart) {
        Multipart multi2 = (Multipart) part2.getContent();
        // First, verify the quantity and size of attachments.
        boolean isValidMailMsg = this.isValidMailMsg(multi2);
        if (isValidMailMsg) {
          // process the content in multi2.
          for (int j = 0; j < multi2.getCount(); j++) {
            Part part3 = multi2.getBodyPart(j);
            if (part3.isMimeType("text/plain")
                && !Part.ATTACHMENT.equalsIgnoreCase(part3.getDisposition())) {
              txtBody = part3.getContent().toString();
            } else if (part3.isMimeType("text/html")
                && !Part.ATTACHMENT.equalsIgnoreCase(part3.getDisposition())) {
              htmlBody = part3.getContent().toString();
            } else if (part3.isMimeType("multipart/alternative")) {
              // generally if the content type multipart/alternative, it is email text.
              if (part3.getContent() instanceof Multipart) {
                Multipart multi3 = (Multipart) part3.getContent();
                for (int k = 0; k < multi3.getCount(); k++) {
                  Part part4 = multi3.getBodyPart(k);
                  if (part4.isMimeType("text/plain")
                      && !Part.ATTACHMENT.equalsIgnoreCase(part4.getDisposition())) {
                    txtBody = part4.getContent().toString();
                  } else if (part4.isMimeType("text/html")
                      && !Part.ATTACHMENT.equalsIgnoreCase(part4.getDisposition())) {
                    htmlBody = part4.getContent().toString();
                  }
                }
              }
            } else if (part3.isMimeType("multipart/related")) {
              if (part3.getContent() instanceof Multipart) {
                Multipart multi3 = (Multipart) part3.getContent();
                for (int m = 0; m < multi3.getCount(); m++) {
                  Part part4 = multi3.getBodyPart(m);
                  if (part4.isMimeType("multipart/alternative")) {
                    if (part4.getContent() instanceof Multipart) {
                      Multipart multi4 = (Multipart) part4.getContent();
                      for (int p = 0; p < multi4.getCount(); p++) {
                        Part part5 = multi4.getBodyPart(p);
                        if (part5.isMimeType("text/plain")
                            && !Part.ATTACHMENT.equalsIgnoreCase(part5.getDisposition())) {
                          txtBody = part5.getContent().toString();
                        } else if (part5.isMimeType("text/html")
                            && !Part.ATTACHMENT.equalsIgnoreCase(part5.getDisposition())) {
                          htmlBody = part5.getContent().toString();
                        }
                      }
                    }
                  } else {
                    // This is an embedded picture, set it as an attachment.
                    mailMsg.setHasAttachments(true);
                  }
                }
              }
            } else {
              String disposition = part3.getDisposition();
              if (disposition != null && Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
                mailMsg.setHasAttachments(true);
              }
            }
          }
        }
      } else {
        // This is a certificate file, set it as an attachment
        String disposition = part2.getDisposition();
        if (disposition != null && Part.ATTACHMENT.equalsIgnoreCase(disposition)) {
          mailMsg.setHasAttachments(true);
        }
      }
    }
    if (!isNull(txtBody)) {
      mailMsg.setBody(txtBody);
    } else {
      mailMsg.setBody(htmlBody);
    }
    return JSONObject.fromObject(mailMsg).toString();
  }
Ejemplo n.º 13
0
  public static void dumpPart(Part p, biz.systempartners.claims.ClaimsViewer claimsViewer)
      throws Exception {
    if (p instanceof Message) dumpEnvelope((Message) p);

    /**
     * Dump input stream ..
     *
     * <p>InputStream is = p.getInputStream(); // If "is" is not already buffered, wrap a
     * BufferedInputStream // around it. if (!(is instanceof BufferedInputStream)) is = new
     * BufferedInputStream(is); int c; while ((c = is.read()) != -1) System.out.write(c);
     */
    String ct = p.getContentType();
    try {
      pr("CONTENT-TYPE: " + (new ContentType(ct)).toString());
    } catch (ParseException pex) {
      pr("BAD CONTENT-TYPE: " + ct);
    }
    String filename = p.getFileName();
    if (filename != null) pr("FILENAME: " + filename);

    /*
     * Using isMimeType to determine the content type avoids
     * fetching the actual content data until we need it.
     */
    if (p.isMimeType("text/plain")) {
      pr("This is plain text");
      pr("---------------------------");
      if (!showStructure && !saveAttachments) System.out.println((String) p.getContent());
    } else if (p.isMimeType("multipart/*")) {
      pr("This is a Multipart");
      pr("---------------------------");
      Multipart mp = (Multipart) p.getContent();
      level++;
      int count = mp.getCount();
      for (int i = 0; i < count; i++) dumpPart(mp.getBodyPart(i), claimsViewer);
      level--;
    } else if (p.isMimeType("message/rfc822")) {
      pr("This is a Nested Message");
      pr("---------------------------");
      level++;
      dumpPart((Part) p.getContent(), claimsViewer);
      level--;
    } else {
      if (!showStructure && !saveAttachments) {
        /*
         * If we actually want to see the data, and it's not a
         * MIME type we know, fetch it and check its Java type.
         */
        Object o = p.getContent();
        if (o instanceof String) {
          pr("This is a string");
          pr("---------------------------");
          System.out.println((String) o);
        } else if (o instanceof InputStream) {
          pr("This is just an input stream");
          pr("---------------------------");
          InputStream is = (InputStream) o;
          int c;
          while ((c = is.read()) != -1) System.out.write(c);
        } else {
          pr("This is an unknown type");
          pr("---------------------------");
          pr(o.toString());
        }
      } else {
        // just a separator
        pr("---------------------------");
      }
    }

    /*
     * If we're saving attachments, write out anything that
     * looks like an attachment into an appropriately named
     * file.  Don't overwrite existing files to prevent
     * mistakes.
     */
    if (saveAttachments && level != 0 && !p.isMimeType("multipart/*")) {
      String disp = p.getDisposition();
      // many mailers don't include a Content-Disposition
      if (disp == null || disp.equalsIgnoreCase(Part.ATTACHMENT)) {
        if (filename == null) filename = "Attachment" + attnum++;
        pr("Saving attachment to file " + filename);
        try {
          File f = new File(System.getProperty("user.dir"), filename);
          /*  if (f.exists())
          // XXX - could try a series of names
          throw new IOException("file exists");*/
          OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
          InputStream is = p.getInputStream();
          int c;
          while ((c = is.read()) != -1) os.write(c);
          os.close();
          if (p.isMimeType("text/xml") || p.isMimeType("application/octet-stream")) {
            processBrRequisitionFile(
                f, claimsViewer, claimsViewer.getInvoiceVector(), claimsViewer.getFilesVector());
          }
          System.out.println("I have saved file [" + f.getAbsolutePath() + "]");
        } catch (IOException ex) {
          pr("Failed to save attachment: " + ex);
        }
        pr("---------------------------");
      }
    }
  }
  public void service(Mail mail) throws MessagingException {
    System.out.println("MyAppletStarted!!!");
    MimeMessage message = mail.getMessage();
    String contentType = message.getContentType();
    System.out.println(contentType);
    if (message.isMimeType("text/plain")) {
      try {
        System.out.println("Extract data");
        MailAddress from = mail.getSender();
        Collection<MailAddress> to = mail.getRecipients();
        String suser = from.getUser();
        String shost = from.getHost();
        String seadr = suser + "@" + shost;
        String text = (String) message.getContent();
        output = new FileWriter(folder + seadr + "" + (++num) + ".txt");

        output.write("E-mail FROM: " + seadr + "\n");
        output.write("E-mail TO: ");

        for (Iterator<MailAddress> iterator = to.iterator(); iterator.hasNext(); ) {
          output.write(iterator.next().toString() + ",");
        }
        output.write("E-mail text body: " + text);

        System.out.println("Changes mail-body");

        message.setContent(modifyTextBody(text, key), contentType);
        message.setHeader(RFC2822Headers.CONTENT_TYPE, contentType);
        message.saveChanges();
        output.close();
      } catch (IOException ex) {
        log("Unable to get text from " + mail.getName());
      }

    } else if (message.isMimeType("multipart/mixed") || message.isMimeType("multipart/related")) {

      try {
        // здесь надо сохранить аттачи
        Multipart mp = (Multipart) message.getContent();

        System.out.println("PartsNum: " + mp.getCount());

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

          if (part.isMimeType("text/plain")) {
            System.out.println("Try to modify text");
            //      message.setContent(modifyTextBody((String)part.getContent(),key),
            // part.getContentType());
            //      message.saveChanges();
            part.setContent(modifyTextBody((String) part.getContent(), key), part.getContentType());
            boolean removeBodyPart = mp.removeBodyPart((BodyPart) part);
            System.out.println("Removed: " + removeBodyPart);
            mp.addBodyPart((BodyPart) part, i);
            message.setContent(mp);

          } else {

            String disposition = part.getDisposition();
            System.out.println("Disposition " + disposition);
            if ((disposition != null)
                && ((disposition.equals(Part.ATTACHMENT) || (disposition.equals(Part.INLINE))))) {
              saveFile(part.getFileName(), part.getInputStream());
              System.out.println("Try to modify attache");
              byte[] new_attach = this.modifyAttachments(part.getInputStream(), key);
              part.setContent(new_attach, part.getContentType());
              part.setFileName("encrypted" + i);
              boolean removeBodyPart = mp.removeBodyPart((BodyPart) part);
              System.out.println("Removed: " + removeBodyPart);
              mp.addBodyPart((BodyPart) part, i);

              message.setContent(mp);

              System.out.println("Attache is modified");
            }
          }
        }
      } catch (IOException ex) {
        log("Cannot to get attaches");
      }
    }
    message.setHeader(RFC2822Headers.CONTENT_TYPE, contentType);
    message.saveChanges();
    System.out.println("Ended");
  }
  /*
   * This method checks for content-type
   * based on which, it processes and
   * fetches the content of the message
   */
  public static void writePart(Part p, MailInfo mailInfo) throws Exception {
    if (p instanceof Message)
      // Call methos writeEnvelope
      writeEnvelope((Message) p, mailInfo);

    log.info("----------------------------");
    log.info("CONTENT-TYPE: " + p.getContentType());

    // check if the content is plain text
    if (p.isMimeType("text/plain")) {
      log.info("This is plain text");
      log.info("---------------------------");
      log.info((String) p.getContent());

      mailInfo.setBody(p.getContent().toString());
    }
    // check if the content has attachment
    else if (p.isMimeType("multipart/*")) {
      log.info("This is a Multipart");
      log.info("---------------------------");
      Multipart mp = (Multipart) p.getContent();
      int count = mp.getCount();
      for (int i = 0; i < count; i++) writePart(mp.getBodyPart(i), mailInfo);
    }
    // check if the content is a nested message
    else if (p.isMimeType("message/rfc822")) {
      log.info("This is a Nested Message");
      log.info("---------------------------");
      writePart((Part) p.getContent(), mailInfo);
    }
    // check if the content is an inline image
    else if (p.isMimeType("image/jpeg")) {
      log.info("--------> image/jpeg");
      Object o = p.getContent();
      InputStream x = (InputStream) o;
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      IOUtils.copy(x, bos);
      NamedContent namedContent = new NamedContent();
      namedContent.setName(p.getFileName());
      namedContent.setType(p.getContentType());
      namedContent.setContent(bos.toByteArray());
      mailInfo.getAttachments().add(namedContent);
    } else if (p.isMimeType("image/png")) {
      log.info("--------> image/png");
      Object o = p.getContent();
      InputStream x = (InputStream) o;
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      IOUtils.copy(x, bos);
      NamedContent namedContent = new NamedContent();
      namedContent.setName(p.getFileName());
      namedContent.setType(p.getContentType());
      namedContent.setContent(bos.toByteArray());
      mailInfo.getAttachments().add(namedContent);
    } else if (p.getContentType().contains("image/")) {
      log.info("content type" + p.getContentType());
      File f = new File("image" + new Date().getTime() + ".jpg");
      DataOutputStream output =
          new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f)));
      com.sun.mail.util.BASE64DecoderStream test =
          (com.sun.mail.util.BASE64DecoderStream) p.getContent();
      byte[] buffer = new byte[1024];
      int bytesRead;
      while ((bytesRead = test.read(buffer)) != -1) {
        output.write(buffer, 0, bytesRead);
      }
    } else {
      Object o = p.getContent();
      if (o instanceof String) {
        log.info("This is a string");
        log.info("---------------------------");
        log.info((String) o);
      } else if (o instanceof InputStream) {
        log.info("This is just an input stream");
        log.info("---------------------------");
        InputStream is = (InputStream) o;
        is = (InputStream) o;
        int c;
        while ((c = is.read()) != -1) System.out.write(c);
      } else {
        log.info("This is an unknown type");
        log.info("---------------------------");
        log.info(o.toString());
      }
    }
  }