Example usage for javax.mail.internet MimePart getContent

List of usage examples for javax.mail.internet MimePart getContent

Introduction

In this page you can find the example usage for javax.mail.internet MimePart getContent.

Prototype

public Object getContent() throws IOException, MessagingException;

Source Link

Document

Return the content as a Java object.

Usage

From source file:mitm.common.mail.MailUtils.java

/**
 * Converts any 8bit encoded parts to 7bit. Returns true if a part (or all parts) were converted from
 * 8bit to 7bit./*from   ww w .  j a v  a 2  s .co m*/
 * 
 * @param part
 * @throws MessagingException
 */
public static boolean convertTo7Bit(MimePart part) throws MessagingException, IOException {
    boolean converted = false;

    if (part.isMimeType("multipart/*")) {
        Multipart parts = (Multipart) part.getContent();

        int count = parts.getCount();

        for (int i = 0; i < count; i++) {
            boolean partConverted = convertTo7Bit((MimePart) parts.getBodyPart(i));

            if (partConverted) {
                converted = true;
            }
        }
    } else if ("8bit".equalsIgnoreCase(part.getEncoding())) {
        String encoding = part.isMimeType("text/*") ? "quoted-printable" : "base64";

        /*
         * We need to use a ByteArrayDataSource to make sure it will always be encoded
         */
        part.setDataHandler(
                new DataHandler(new ByteArrayDataSource(part.getInputStream(), part.getContentType())));

        part.setHeader("Content-Transfer-Encoding", encoding);
        part.addHeader("X-MIME-Autoconverted", "from 8bit to " + encoding + " by Djigzo");

        converted = true;
    }

    return converted;
}

From source file:mitm.common.mail.BodyPartUtils.java

public static MimeBodyPart makeContentBodyPart(MimePart sourcePart, HeaderMatcher matcher)
        throws MessagingException, IOException {
    MimeBodyPart newBodyPart = new MimeBodyPart();

    newBodyPart.setContent(sourcePart.getContent(), sourcePart.getContentType());

    HeaderUtils.copyHeaders(sourcePart, newBodyPart, matcher);

    return newBodyPart;
}

From source file:mailbox.CreationViaEmail.java

private static Content getContent(MimePart part) throws IOException, MessagingException {
    Content result = new Content();
    result.body = (String) part.getContent();
    result.type = part.getContentType();
    return result;
}

From source file:mailbox.CreationViaEmail.java

private static Content getJoinedContent(MimePart part) throws IOException, MessagingException {
    Content result = new Content();
    MimeMultipart mp = (MimeMultipart) part.getContent();
    for (int i = 0; i < mp.getCount(); i++) {
        MimeBodyPart p = (MimeBodyPart) mp.getBodyPart(i);
        result.merge(processPart(p, part));
    }/*from   ww  w .ja  v a2s  . c  o  m*/
    return result;
}

From source file:mailbox.CreationViaEmail.java

private static Content getContentOfBestPart(MimePart part, MimePart parent)
        throws IOException, MessagingException {
    MimeBodyPart best = null;//  www.java  2 s.c  o  m
    MimeMultipart mp = (MimeMultipart) part.getContent();
    for (int i = 0; i < mp.getCount(); i++) {
        // Prefer HTML if the parent is a multipart/related part which may contain
        // inline images, because text/plain cannot embed the images.
        boolean isHtmlPreferred = parent != null && parent.isMimeType(MimeType.MULTIPART_RELATED);
        best = better((MimeBodyPart) mp.getBodyPart(i), best, isHtmlPreferred);
    }
    return processPart(best, part);
}

From source file:mailbox.CreationViaEmail.java

private static Content getContentWithAttachments(MimePart part) throws MessagingException, IOException {
    Content result = new Content();
    String rootId = new ContentType(part.getContentType()).getParameter("start");
    MimeMultipart mp = (MimeMultipart) part.getContent();
    for (int i = 0; i < mp.getCount(); i++) {
        MimePart p = (MimePart) mp.getBodyPart(i);
        if (isRootPart(p, i, rootId)) {
            result = result.merge(processPart(p, part));
        } else {/*from   w  w w  .  j av a  2  s . co  m*/
            result.attachments.add(p);
        }
    }
    return result;
}

From source file:com.cubusmail.mail.util.MessageUtils.java

/**
 * Method for checking if the message has attachments.
 *///from  w  ww.jav a  2s .  com
public static List<MimePart> attachmentsFromPart(Part part) throws MessagingException, IOException {

    List<MimePart> attachmentParts = new ArrayList<MimePart>();
    if (part instanceof MimePart) {
        MimePart mimePart = (MimePart) part;

        if (part.isMimeType("multipart/*")) {
            Multipart mp = (Multipart) mimePart.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
                MimeBodyPart bodyPart = (MimeBodyPart) mp.getBodyPart(i);
                if (Part.ATTACHMENT.equals(bodyPart.getDisposition())
                        || Part.INLINE.equals(bodyPart.getDisposition())) {
                    attachmentParts.add(bodyPart);
                }
            }
        } else if (part.isMimeType("application/*")) {
            attachmentParts.add(mimePart);
        }
    }

    return attachmentParts;
}

From source file:com.consol.citrus.mail.message.MailMessageConverter.java

/**
 * Construct simple binary body part with base64 data.
 * @param textPart//from   w  w  w.  j av  a2 s. co  m
 * @param contentType
 * @return
 * @throws IOException
 */
protected BodyPart handleTextPart(MimePart textPart, String contentType)
        throws IOException, MessagingException {
    String text = (String) textPart.getContent();
    return new BodyPart(stripMailBodyEnding(text), contentType);
}

From source file:com.consol.citrus.mail.message.MailMessageConverter.java

/**
 * Process message part. Can be a text, binary or multipart instance.
 * @param part/*from ww  w.j  a va  2s.  c om*/
 * @return
 * @throws java.io.IOException
 */
protected BodyPart handlePart(MimePart part) throws IOException, MessagingException {
    String contentType = parseContentType(part.getContentType());

    if (part.isMimeType("multipart/*")) {
        return handleMultiPart((Multipart) part.getContent());
    } else if (part.isMimeType("text/*")) {
        return handleTextPart(part, contentType);
    } else if (part.isMimeType("image/*")) {
        return handleImageBinaryPart(part, contentType);
    } else if (part.isMimeType("application/*")) {
        return handleApplicationContentPart(part, contentType);
    } else {
        return handleBinaryPart(part, contentType);
    }
}

From source file:com.zimbra.cs.mime.Mime.java

/** Returns the MimeMultipart object encapsulating the body of a MIME
 *  part with content-type "multipart/*".  Use this method instead of
 *  {@link Part#getContent()} to work around JavaMail's fascism about
 *  proper MIME format and failure to support RFC 2184. */
public static MimeMultipart getMultipartContent(MimePart multipartPart, String contentType)
        throws IOException, MessagingException {
    MimeMultipart mmp = null;//from www. ja v a  2  s. c  o m
    Object content = multipartPart.getContent();
    if (content instanceof MimeMultipart) {
        mmp = (MimeMultipart) content;
    } else if (content instanceof InputStream) {
        try {
            // handle unparsed content due to miscapitalization of content-type value
            mmp = new ZMimeMultipart(new InputStreamDataSource((InputStream) content, contentType));
        } catch (Exception e) {
        } finally {
            ByteUtil.closeStream((InputStream) content);
        }
    }

    return validateMultipart(mmp, multipartPart);
}