/**
   * Count the mail attachments' size, it is a total mail attachments' size.
   *
   * @param multi
   * @param mailAttachmentsSize
   * @return
   * @throws MessagingException
   * @throws IOException
   */
  protected int countMailAttachmentsSize(Multipart multi, int mailAttachmentsSize)
      throws MessagingException, IOException {
    for (int i = 0, n = multi.getCount(); i < n; i++) {
      Part part = multi.getBodyPart(i);

      if (part.getContent() instanceof Multipart) {
        mailAttachmentsSize =
            this.countMailAttachmentsSize((Multipart) part.getContent(), mailAttachmentsSize);
      }
      int partSize = part.getSize();
      if (partSize != -1) {
        mailAttachmentsSize = mailAttachmentsSize + partSize;
      }
    }
    return mailAttachmentsSize;
  }
  private MyMessage map(Message message) throws IOException, MessagingException {

    MimeMessage m = (MimeMessage) message;

    dump(m);

    Object content = m.getContent();

    log.info("================= " + m.getSubject() + " =================");
    log.info("content class: " + content.getClass());
    log.info("contentType: " + m.getContentType());

    if (content instanceof Multipart) {

      Multipart mp = (Multipart) content;
      log.info("---------------------- " + mp.getCount() + " ----------------------");
      for (int i = 0; i < mp.getCount(); i++) {

        Part part = mp.getBodyPart(i);
        String disposition = part.getDisposition();

        log.info("---------------------- >>>>> ----------------------");

        log.info("part.size: " + part.getSize());
        log.info("part.lineCount: " + part.getLineCount());
        log.info("part.description: " + part.getDescription());
        log.info("part.contentType: " + part.getContentType());
        log.info("part.fileName: " + part.getFileName());
        log.info("part.disposition: " + disposition);

        Enumeration headers = part.getAllHeaders();

        while (headers.hasMoreElements()) {
          Header header = (Header) headers.nextElement();
          log.info("part.header - " + header.getName() + " : " + header.getValue());
        }

        log.info("---------------------- <<<<< ----------------------");

        if (disposition != null) {}
      }
    }

    return new MyMessage().setSubject(m.getSubject()).setId(m.getMessageID());
  }
  /**
   * Save attachment.
   *
   * @param part
   * @param attachList
   * @throws MessagingException
   * @throws IOException
   */
  protected void saveAttachment(Part part, List<Attachment> attachList)
      throws MessagingException, IOException {
    // generate a new file name with unique UUID.
    String fileName = part.getFileName();
    fileName = MimeUtility.decodeText(fileName);
    Attachment attachment = new Attachment();
    attachment.setFileName(fileName);

    UUID uuid = UUID.randomUUID();
    String prefix = fileName.substring(0, fileName.lastIndexOf(".") + 1);
    String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
    String tempDir = System.getProperty("java.io.tmpdir");
    String filePath = tempDir + prefix + uuid + "." + suffix;

    int fileSize = part.getSize();
    attachment.setFilePath(filePath);
    attachment.setFileType(suffix);
    attachment.setFileSize(fileSize);
    attachList.add(attachment);
    this.saveFile(filePath, part.getInputStream());
  }