Example usage for javax.mail.internet MimePart isMimeType

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

Introduction

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

Prototype

public boolean isMimeType(String mimeType) throws MessagingException;

Source Link

Document

Is this Part of the specified MIME type?

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   w w  w  . jav a 2 s  . c o 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:mailbox.CreationViaEmail.java

@Nonnull
private static Content processPart(MimePart part, MimePart parent) throws MessagingException, IOException {
    if (part == null) {
        return new Content();
    }//from  ww w.ja  v a  2 s .c  o m

    if (part.getFileName() != null) {
        // Assume that a part which has a filename is an attachment.
        return new Content(part);
    }

    if (part.isMimeType("text/*")) {
        return getContent(part);
    } else if (part.isMimeType("multipart/*")) {
        if (part.isMimeType(MimeType.MULTIPART_RELATED)) {
            return getContentWithAttachments(part);
        } else if (part.isMimeType(MimeType.MULTIPART_ALTERNATIVE)) {
            return getContentOfBestPart(part, parent);
        } else {
            return getJoinedContent(part);
        }
    }

    return new Content();
}

From source file:mailbox.CreationViaEmail.java

private static Content getContentOfBestPart(MimePart part, MimePart parent)
        throws IOException, MessagingException {
    MimeBodyPart best = null;/*from   w w  w.ja v  a  2s .  com*/
    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:com.consol.citrus.mail.message.MailMessageConverter.java

/**
 * Construct body part form special application data. Based on known application content types delegate to text,
 * image or binary body construction.//from   www.ja  va2  s . c om
 * @param applicationData
 * @param contentType
 * @return
 * @throws IOException
 */
protected BodyPart handleApplicationContentPart(MimePart applicationData, String contentType)
        throws IOException, MessagingException {
    if (applicationData.isMimeType("application/pdf")) {
        return handleImageBinaryPart(applicationData, contentType);
    } else if (applicationData.isMimeType("application/rtf")) {
        return handleImageBinaryPart(applicationData, contentType);
    } else if (applicationData.isMimeType("application/java")) {
        return handleTextPart(applicationData, contentType);
    } else if (applicationData.isMimeType("application/x-javascript")) {
        return handleTextPart(applicationData, contentType);
    } else if (applicationData.isMimeType("application/xhtml+xml")) {
        return handleTextPart(applicationData, contentType);
    } else if (applicationData.isMimeType("application/json")) {
        return handleTextPart(applicationData, contentType);
    } else if (applicationData.isMimeType("application/postscript")) {
        return handleTextPart(applicationData, contentType);
    } else {
        return handleBinaryPart(applicationData, 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   www  .j a va 2s.  co  m
 * @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:org.apache.james.smtpserver.fastfail.URIRBLHandler.java

/**
 * Recursively scans all MimeParts of an email for domain strings. Domain
 * strings that are found are added to the supplied HashSet.
 * /*from  w w  w.j a  v a  2s . co  m*/
 * @param part
 *            MimePart to scan
 * @param session
 *            not null
 * @return domains The HashSet that contains the domains which were
 *         extracted
 */
private HashSet<String> scanMailForDomains(MimePart part, SMTPSession session)
        throws MessagingException, IOException {
    HashSet<String> domains = new HashSet<String>();
    session.getLogger().debug("mime type is: \"" + part.getContentType() + "\"");

    if (part.isMimeType("text/plain") || part.isMimeType("text/html")) {
        session.getLogger().debug("scanning: \"" + part.getContent().toString() + "\"");
        HashSet<String> newDom = URIScanner.scanContentForDomains(domains, part.getContent().toString());

        // Check if new domains are found and add the domains
        if (newDom != null && newDom.size() > 0) {
            domains.addAll(newDom);
        }
    } else if (part.isMimeType("multipart/*")) {
        MimeMultipart multipart = (MimeMultipart) part.getContent();
        int count = multipart.getCount();
        session.getLogger().debug("multipart count is: " + count);

        for (int index = 0; index < count; index++) {
            session.getLogger().debug("recursing index: " + index);
            MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(index);
            HashSet<String> newDomains = scanMailForDomains(mimeBodyPart, session);

            // Check if new domains are found and add the domains
            if (newDomains != null && newDomains.size() > 0) {
                domains.addAll(newDomains);
            }
        }
    }
    return domains;
}