Example usage for javax.mail Part getContent

List of usage examples for javax.mail Part getContent

Introduction

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

Prototype

public Object getContent() throws IOException, MessagingException;

Source Link

Document

Return the content as a Java object.

Usage

From source file:com.aurel.track.util.emailHandling.MailReader.java

/**
 * Processes part of a message//from  ww  w.j  av  a  2 s.  c o m
 */
private static String handleSimplePart(Part part, List<EmailAttachment> attachments, boolean ignoreAttachments)
        throws MessagingException, IOException {
    // Check for content disposition and content type
    String disposition = part.getDisposition();
    String contentType = part.getContentType();
    LOGGER.debug("disposition=" + disposition);
    LOGGER.debug("Body type is: " + contentType);

    // tread if the part is a message
    boolean inlineMessage = part.isMimeType("message/rfc822");
    if (inlineMessage) {
        LOGGER.debug("Inner message:" + part.getFileName());
        Message subMessage = (Message) part.getContent();
        StringBuffer s = handlePart(subMessage, attachments, ignoreAttachments);
        System.out.println();
        return s.toString();
    }
    if (disposition == null) {
        if ("image/BMP".equals(contentType)) {
            // BMP image add as attachment
            if (ignoreAttachments == false) {
                try {
                    attachments.add(createEmailAttachment(part, "image.bmp"));
                } catch (Exception e) {
                    // just ignore
                }
            }
            return null;
        } else if (part.isMimeType("text/*")) {
            return getText(part);
        } else {
            if (ignoreAttachments == false) {
                handleAttachment(part, attachments);
            }
            return null;
        }
    }
    if (disposition.equalsIgnoreCase(Part.INLINE)) {
        return handleInline(part, attachments, ignoreAttachments);
    }
    if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
        if (ignoreAttachments == false) {
            handleAttachment(part, attachments);
        }
        return null;
    }
    LOGGER.debug("Unknown disposition:" + disposition + "Threat as attachment");
    if (ignoreAttachments == false) {
        handleAttachment(part, attachments);
    }
    return null;
}

From source file:org.silverpeas.core.mail.extractor.EMLExtractor.java

private String processMailPart(Part part, List<MailAttachment> attachments)
        throws MessagingException, IOException {
    if (!isTextPart(part)) {
        Object content = part.getContent();
        if (content instanceof Multipart) {
            Multipart mContent = (Multipart) content;
            return processMultipart(mContent, attachments);
        } else if (attachments != null) {
            String fileName = getFileName(part);
            if (fileName != null) {
                MailAttachment attachment = new MailAttachment(fileName);
                String dir = FileRepositoryManager.getTemporaryPath() + "mail" + System.currentTimeMillis();
                File file = new File(dir, fileName);
                FileUtils.copyInputStreamToFile(part.getInputStream(), file);
                attachment.setPath(file.getAbsolutePath());
                attachment.setSize(file.length());
                attachments.add(attachment);
            }//from ww w  .j  av a  2  s.c om
        }
    } else {
        if (part.getContentType().indexOf(MimeTypes.HTML_MIME_TYPE) >= 0) {
            return (String) part.getContent();
        } else if (part.getContentType().indexOf(MimeTypes.PLAIN_TEXT_MIME_TYPE) >= 0) {
            return WebEncodeHelper.javaStringToHtmlParagraphe((String) part.getContent());
        }
    }
    return "";
}

From source file:org.silverpeas.util.mail.EMLExtractor.java

private String processMailPart(Part part, List<MailAttachment> attachments)
        throws MessagingException, IOException {
    if (!isTextPart(part)) {
        Object content = part.getContent();
        if (content instanceof Multipart) {
            Multipart mContent = (Multipart) content;
            return processMultipart(mContent, attachments);
        } else if (attachments != null) {
            String fileName = getFileName(part);
            if (fileName != null) {
                MailAttachment attachment = new MailAttachment(fileName);
                String dir = FileRepositoryManager.getTemporaryPath() + "mail" + System.currentTimeMillis();
                File file = new File(dir, fileName);
                FileUtils.copyInputStreamToFile(part.getInputStream(), file);
                attachment.setPath(file.getAbsolutePath());
                attachment.setSize(file.length());
                attachments.add(attachment);
            }/*from  ww  w . j  a  v a  2 s  . c o m*/
        }
    } else {
        if (part.getContentType().indexOf(MimeTypes.HTML_MIME_TYPE) >= 0) {
            return (String) part.getContent();
        } else if (part.getContentType().indexOf(MimeTypes.PLAIN_TEXT_MIME_TYPE) >= 0) {
            return EncodeHelper.javaStringToHtmlParagraphe((String) part.getContent());
        }
    }
    return "";
}

From source file:org.silverpeas.core.mail.extractor.EMLExtractor.java

private String getBody(Multipart multipart) throws MessagingException, IOException {
    int partsNumber = multipart.getCount();
    String body = "";
    for (int i = 0; i < partsNumber; i++) {
        Part part = multipart.getBodyPart(i);
        if (part.isMimeType(MimeTypes.HTML_MIME_TYPE)) {
            // if present, return always HTML part
            return (String) part.getContent();
        } else if (part.isMimeType(MimeTypes.PLAIN_TEXT_MIME_TYPE)) {
            body = WebEncodeHelper.javaStringToHtmlParagraphe((String) part.getContent());
        } else if (part.getContent() instanceof Multipart) {
            return getBody((Multipart) part.getContent());
        }/*  www.  j  a  v a  2s  .  co  m*/
    }
    return body;
}

From source file:org.silverpeas.util.mail.EMLExtractor.java

private String getBody(Multipart multipart) throws MessagingException, IOException {
    int partsNumber = multipart.getCount();
    String body = "";
    for (int i = 0; i < partsNumber; i++) {
        Part part = multipart.getBodyPart(i);
        if (part.isMimeType(MimeTypes.HTML_MIME_TYPE)) {
            // if present, return always HTML part
            return (String) part.getContent();
        } else if (part.isMimeType(MimeTypes.PLAIN_TEXT_MIME_TYPE)) {
            body = EncodeHelper.javaStringToHtmlParagraphe((String) part.getContent());
        } else if (part.getContent() instanceof Multipart) {
            return getBody((Multipart) part.getContent());
        }/*from   w w w  . j a  v  a2  s.  co m*/
    }
    return body;
}

From source file:com.silverpeas.mailinglist.service.job.MailProcessor.java

/**
 * Processes a part for a multi-part email.
 *
 * @param part the part to be processed.
 * @param message the message corresponding to the email.
 * @throws MessagingException//from w  ww.j  a va 2s.  co  m
 * @throws IOException
 */
public void processMailPart(Part part, Message message) throws MessagingException, IOException {
    if (!isTextPart(part)) {
        Object content = part.getContent();
        if (content instanceof Multipart) {
            processMultipart((Multipart) content, message);
        } else {
            String fileName = getFileName(part);
            if (fileName != null) {
                Attachment attachment = new Attachment();
                attachment.setSize(part.getSize());
                attachment.setFileName(fileName);
                attachment.setContentType(extractContentType(part.getContentType()));
                String attachmentPath = saveAttachment(part, message.getComponentId(), message.getMessageId());
                attachment.setPath(attachmentPath);
                message.getAttachments().add(attachment);
            }
        }
    } else {
        processBody((String) part.getContent(), extractContentType(part.getContentType()), message);
    }
}

From source file:org.jahia.modules.gateway.mail.MailToJSONImpl.java

protected void parseMailMessage(Part part, MailContent content) throws IOException, MessagingException {
    Object mailContent = part.getContent();
    if (mailContent instanceof MimeMultipart) {
        MimeMultipart mailMessageContent = (MimeMultipart) mailContent;
        // We have some attachments
        for (int i = 0; i < mailMessageContent.getCount(); i++) {
            BodyPart bodyPart = mailMessageContent.getBodyPart(i);
            parseMailMessage(bodyPart, content);
        }/*from   w  ww. j a v a  2  s . c  om*/
    } else if (mailContent instanceof String && part.getDisposition() == null) {
        boolean isHtml = false;
        if (content.getBody() == null || ((isHtml = part.isMimeType("text/html")) && !content.isHtml())) {
            if (isHtml) {
                content.setBodyHtml((String) mailContent);
            } else {
                content.setBody((String) mailContent);
            }
        }
    } else if (mailContent instanceof InputStream || mailContent instanceof String) {
        File tempFile = File.createTempFile("mail2json-", null);
        try {
            FileUtils.copyInputStreamToFile(
                    mailContent instanceof InputStream ? (InputStream) mailContent : part.getInputStream(),
                    tempFile);
            content.getFiles()
                    .add(new FileItem(StringUtils.defaultIfEmpty(part.getFileName(), "unknown"), tempFile));
        } catch (IOException e) {
            FileUtils.deleteQuietly(tempFile);
            throw e;
        }
    }
    assert content.getBody() != null;
}

From source file:com.cubusmail.server.services.RetrieveImageServlet.java

/**
 * @param parent//from   w  w w  . j ava  2 s .  co m
 * @return
 */
private Part findImagePart(Part parent) {

    try {
        if (MessageUtils.isImagepart(parent)) {
            return parent;
        } else if (parent.isMimeType("multipart/*")) {
            Multipart mp;
            mp = (Multipart) parent.getContent();
            int count = mp.getCount();
            for (int i = 0; i < count; i++) {
                Part subPart = findImagePart(mp.getBodyPart(i));
                if (subPart != null) {
                    return subPart;
                }
            }
        }
    } catch (MessagingException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    return null;
}

From source file:org.sakaiproject.kernel.messaging.activemq.ActiveMQEmailDeliveryT.java

private String getBodyAsString(Object content) {
    Multipart mime = null;//from  w  w w .j  a va 2s  . c om
    StringBuffer sb = new StringBuffer();
    if (content instanceof String) {
        return (String) content;
    } else if (content instanceof Multipart) {
        try {
            mime = (Multipart) content;
            for (int i = 0; i < mime.getCount(); ++i) {
                Part p = mime.getBodyPart(i);
                sb.append(p.getContent());
            }
        } catch (MessagingException e) {
            e.printStackTrace();
            Assert.assertTrue(false);
        } catch (IOException e) {
            e.printStackTrace();
            Assert.assertTrue(false);
        }
    } else {
        Assert.assertTrue(false);
    }
    return sb.toString();
}

From source file:de.contentreich.alfresco.repo.email.EMLTransformer.java

private void processPreviewMultiPart(Multipart multipart, Map<String, String> parts)
        throws MessagingException, IOException {
    logger.debug("Processing multipart of type {}", multipart.getContentType());
    // FIXME : Implement strict Depth or breadth first ?
    for (int i = 0, n = multipart.getCount(); i < n; i++) {
        Part part = multipart.getBodyPart(i);
        logger.debug("Processing part name {}, disposition = {}, type type = {}",
                new Object[] { part.getFileName(), part.getDisposition(), part.getContentType() });
        if (part.getContent() instanceof Multipart) {
            processPreviewMultiPart((Multipart) part.getContent(), parts);

        } else if (part.getContentType().contains("text")) {
            String key = part.getContentType().split(";")[0];
            String content = null;
            logger.debug("Add part with content type {} using key {}", part.getContentType(), key);

            if (key.endsWith("html")) {
                // <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
                // Breaks preview !
                content = part.getContent().toString().replaceAll("(?i)(?s)<meta.*charset=[^>]*>", "");
            } else {
                content = part.getContent().toString();
            }/*from   w  w w.  j a v a2s . com*/
            appendPreviewContent(parts, key, content);

        }

    }
}