Example usage for javax.mail Part isMimeType

List of usage examples for javax.mail Part isMimeType

Introduction

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

Prototype

public boolean isMimeType(String mimeType) throws MessagingException;

Source Link

Document

Is this Part of the specified MIME type?

Usage

From source file:Main.java

public static boolean isContainAttachment(Part part) throws MessagingException, IOException {
    boolean flag = false;
    if (part.isMimeType("multipart/*")) {
        MimeMultipart multipart = (MimeMultipart) part.getContent();
        int partCount = multipart.getCount();
        for (int i = 0; i < partCount; i++) {
            BodyPart bodyPart = multipart.getBodyPart(i);
            String disp = bodyPart.getDisposition();
            if (disp != null
                    && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
                flag = true;//from   w  ww.  ja  v a 2  s  .c o m
            } else if (bodyPart.isMimeType("multipart/*")) {
                flag = isContainAttachment(bodyPart);
            } else {
                String contentType = bodyPart.getContentType();
                if (contentType.indexOf("application") != -1) {
                    flag = true;
                }

                if (contentType.indexOf("name") != -1) {
                    flag = true;
                }
            }

            if (flag)
                break;
        }
    } else if (part.isMimeType("message/rfc822")) {
        flag = isContainAttachment((Part) part.getContent());
    }
    return flag;
}

From source file:org.nuxeo.ecm.core.convert.plugins.text.extractors.RFC822ToTextConverter.java

protected static List<Part> getAttachmentParts(Part p) throws Exception {
    List<Part> res = new ArrayList<Part>();
    if (p.isMimeType(MESSAGE_RFC822_MIMETYPE)) {
        res.addAll(getAttachmentParts((Part) p.getContent()));
    } else if (p.isMimeType("multipart/alternative")) {
        // only return one of the text alternatives
        Multipart mp = (Multipart) p.getContent();
        int count = mp.getCount();
        Part alternativePart = null;/*w  w w .  j  ava 2s . co  m*/
        for (int i = 0; i < count; i++) {
            Part subPart = mp.getBodyPart(i);
            if (subPart.isMimeType(TXT_MT)) {
                alternativePart = subPart;
                break;
            } else if (subPart.isMimeType("text/*")) {
                alternativePart = subPart;
            } else {
                res.addAll(getAttachmentParts(subPart));
            }
        }
        if (alternativePart != null) {
            res.add(alternativePart);
        }
    } else if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) p.getContent();
        int count = mp.getCount();
        for (int i = 0; i < count; i++) {
            res.addAll(getAttachmentParts(mp.getBodyPart(i)));
        }
    } else {
        res.add(p);
    }
    return res;
}

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

/**
 * Extracts the message from the RFC822 attachment.
 * @throws MessagingException /*from w  w  w .j  av  a 2 s  .co m*/
 * @throws IOException 
 */
public static MimeMessage extractFromRFC822(Part rfc822) throws IOException, MessagingException {
    if (!rfc822.isMimeType("message/rfc822")) {
        throw new MessagingException("Part is-not-a message/rfc822 but " + rfc822.getContentType());
    }

    return new MimeMessage(MailSession.getDefaultSession(), rfc822.getInputStream());
}

From source file:com.email.ReceiveEmail.java

/**
 * Gather the email body/*ww  w  . j a v  a2s .co m*/
 *
 * @param p Part
 * @return String body
 */
private static String getEmailBodyText(Part p) {
    try {
        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 = getEmailBodyText(bp);
                    }
                } else if (bp.isMimeType("text/html")) {
                    String s = getEmailBodyText(bp);
                    if (s != null) {
                        return s;
                    }
                } else {
                    return getEmailBodyText(bp);
                }
            }
            return text;
        } else if (p.isMimeType("multipart/*")) {
            Multipart mp = (Multipart) p.getContent();
            for (int i = 0; i < mp.getCount(); i++) {
                String s = getEmailBodyText(mp.getBodyPart(i));
                if (s != null) {
                    return s;
                }
            }
        }
        return "";
    } catch (MessagingException | IOException ex) {
        ExceptionHandler.Handle(ex);
    }
    return "";
}

From source file:javamailclient.GmailAPI.java

/**
 * Return the primary text content of the message.
 */// w ww  .  j ava 2 s  .c  om
private static String getMessageBody(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 = getMessageBody(bp);
                }
                continue;
            } else if (bp.isMimeType("text/html")) {
                String s = getMessageBody(bp);
                if (s != null) {
                    return s;
                }
            } else {
                return getMessageBody(bp);
            }
        }
        return text;
    } else if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) p.getContent();
        for (int i = 0; i < mp.getCount(); i++) {
            String s = getMessageBody(mp.getBodyPart(i));
            if (s != null) {
                return s;
            }
        }
    }

    return null;
}

From source file:org.nuxeo.cm.mail.actionpipe.ExtractMessageInformation.java

protected static String getFilename(Part p, String defaultFileName) throws MessagingException {
    String originalFilename = p.getFileName();
    if (originalFilename == null || originalFilename.trim().length() == 0) {
        String filename = defaultFileName;
        // using default filename => add extension for this type
        if (p.isMimeType("text/plain")) {
            filename += ".txt";
        } else if (p.isMimeType("text/html")) {
            filename += ".html";
        }//from  w ww  .  jav a  2 s  . co m
        return filename;
    } else {
        try {
            return MimeUtility.decodeText(originalFilename.trim());
        } catch (UnsupportedEncodingException e) {
            return originalFilename.trim();
        }
    }
}

From source file:org.nuxeo.cm.mail.actionpipe.ExtractMessageInformation.java

@SuppressWarnings("unchecked")
protected static void getAttachmentParts(Part p, String defaultFilename, MimetypeRegistry mimeService,
        ExecutionContext context) throws MessagingException, IOException {
    String filename = getFilename(p, defaultFilename);
    List<Blob> blobs = (List<Blob>) context.get(ATTACHMENTS_KEY);

    if (!p.isMimeType("multipart/*")) {
        String disp = p.getDisposition();
        // no disposition => consider it may be body
        if (disp == null && !context.containsKey(BODY_KEY)) {
            // this will need to be parsed
            context.put(BODY_KEY, p.getContent());
        } else if (disp != null
                && (disp.equalsIgnoreCase(Part.ATTACHMENT) || (disp.equalsIgnoreCase(Part.INLINE)
                        && !(p.isMimeType("text/*") || p.isMimeType("image/*"))))) {
            log.debug("Saving attachment to file " + filename);
            Blob blob = Blobs.createBlob(p.getInputStream());
            String mime = DEFAULT_BINARY_MIMETYPE;
            try {
                if (mimeService != null) {
                    ContentType contentType = new ContentType(p.getContentType());
                    mime = mimeService.getMimetypeFromFilenameAndBlobWithDefault(filename, blob,
                            contentType.getBaseType());
                }// ww w . j  a  v  a  2s .c om
            } catch (MimetypeDetectionException e) {
                log.error(e);
            }
            blob.setMimeType(mime);

            blob.setFilename(filename);

            blobs.add(blob);

            // for debug
            // File f = new File(filename);
            // ((MimeBodyPart) p).saveFile(f);

            log.debug("---------------------------");

        } else {
            log.debug(String.format("Ignoring part with type %s", p.getContentType()));
        }
    }

    if (p.isMimeType("multipart/*")) {
        log.debug("This is a Multipart");
        log.debug("---------------------------");
        Multipart mp = (Multipart) p.getContent();

        int count = mp.getCount();
        for (int i = 0; i < count; i++) {
            getAttachmentParts(mp.getBodyPart(i), defaultFilename, mimeService, context);
        }
    } else if (p.isMimeType(MESSAGE_RFC822_MIMETYPE)) {
        log.debug("This is a Nested Message");
        log.debug("---------------------------");
        getAttachmentParts((Part) p.getContent(), defaultFilename, mimeService, context);
    }

}

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

private static String getPlainBodyAndAttachments(final Part part, Collection<Part> attachments, int level,
        int maxLevel) throws MessagingException, IOException {
    String body = null;//w ww . ja v a  2s. c  o  m

    if (part.isMimeType("text/plain")) {
        body = (String) part.getContent();
    } else {
        /*
         * Maximum level deep
         */
        if (level <= maxLevel && part.isMimeType("multipart/*")) {
            Multipart mp = (Multipart) part.getContent();

            int partCount = mp.getCount();

            for (int i = 0; i < partCount; i++) {
                Part child = mp.getBodyPart(i);

                if (body == null) {
                    body = getPlainBodyAndAttachments(child, attachments, level + 1, maxLevel);

                    if (body == null && part.isMimeType("multipart/mixed")) {
                        if (attachments != null) {
                            attachments.add(child);
                        }
                    }
                } else if (part.isMimeType("multipart/mixed")) {
                    if (attachments != null) {
                        attachments.add(child);
                    }
                }
            }
        }
    }

    return body;
}

From source file:org.apache.hupa.server.utils.MessageUtils.java

/**
 * Loop over MuliPart and write the content to the Outputstream if a
 * attachment with the given name was found.
 *
 * @param logger//from   w w w  . j  a va  2s .  com
 *            The logger to use
 * @param content
 *            Content which should checked for attachments
 * @param attachmentName
 *            The attachmentname or the unique id for the searched attachment
 * @throws MessagingException
 * @throws IOException
 */
public static Part handleMultiPart(Log logger, Object content, String attachmentName)
        throws MessagingException, IOException {
    if (content instanceof Multipart) {
        Multipart part = (Multipart) content;
        for (int i = 0; i < part.getCount(); i++) {
            Part bodyPart = part.getBodyPart(i);
            String fileName = bodyPart.getFileName();
            String[] contentId = bodyPart.getHeader("Content-ID");
            if (bodyPart.isMimeType("multipart/*")) {
                Part p = handleMultiPart(logger, bodyPart.getContent(), attachmentName);
                if (p != null)
                    return p;
            } else {
                if (contentId != null) {
                    for (String id : contentId) {
                        id = id.replaceAll("^.*<(.+)>.*$", "$1");
                        System.out.println(attachmentName + " " + id);
                        if (attachmentName.equals(id))
                            return bodyPart;
                    }
                }
                if (fileName != null) {
                    if (cleanName(attachmentName).equalsIgnoreCase(cleanName(MimeUtility.decodeText(fileName))))
                        return bodyPart;
                }
            }
        }
    } else {
        logger.error("Unknown content: " + content.getClass().getName());
    }
    return null;
}

From source file:org.elasticsearch.river.email.EmailToJson.java

public static void saveAttachment(Part part, String destDir)
        throws UnsupportedEncodingException, MessagingException, FileNotFoundException, IOException {
    if (part.isMimeType("multipart/*")) {
        Multipart multipart = (Multipart) part.getContent();

        int partCount = multipart.getCount();
        for (int i = 0; i < partCount; i++) {

            BodyPart bodyPart = multipart.getBodyPart(i);

            String disp = bodyPart.getDisposition();
            if (disp != null
                    && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE))) {
                InputStream is = bodyPart.getInputStream();
                saveFile(is, destDir, decodeText(bodyPart.getFileName()));
            } else if (bodyPart.isMimeType("multipart/*")) {
                saveAttachment(bodyPart, destDir);
            } else {
                String contentType = bodyPart.getContentType();
                if (contentType.indexOf("name") != -1 || contentType.indexOf("application") != -1) {
                    saveFile(bodyPart.getInputStream(), destDir, decodeText(bodyPart.getFileName()));
                }//w ww.  ja v  a 2s  . co  m
            }
        }
    } else if (part.isMimeType("message/rfc822")) {
        saveAttachment((Part) part.getContent(), destDir);
    }
}