/**
     * 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);
      }
    }
Example #2
0
 /**
  * Sets the content of this part to be the specified multipart.
  *
  * @param mp the multipart content
  * @exception IllegalWriteException if the underlying implementation does not support modification
  * @exception IllegalStateException if this body part is obtained from a READ_ONLY folder
  */
 public void setContent(Multipart mp) throws MessagingException {
   setDataHandler(new DataHandler(mp, mp.getContentType()));
   // Ensure component hierarchy
   mp.setParent(this);
 }