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:org.xmlactions.email.EMailParser.java

private void showPart(Part part) throws IOException, MessagingException {

    log.info("\n\n\nshowPart ==>>");
    log.info("part.toString():" + part.toString());
    log.info("part.getContent():" + (part.getFileName() == null ? part.getContent().toString() : "Attachment"));
    log.info("part.getContentType():" + part.getContentType());
    log.info("part.getFilename():" + part.getFileName());
    log.info("part.isAttachment:" + part.getFileName());
    log.info("part.isMessage:" + (part.getContent() instanceof Message));
    Object obj = part.getContent();
    if (obj instanceof Multipart) {
        log.info("MultiPart");

        Multipart mmp = (Multipart) obj;
        for (int i = 0; i < mmp.getCount(); i++) {
            Part bodyPart = mmp.getBodyPart(i);
            showPart(bodyPart);/*from w w  w .  j ava2s  .  c o m*/
        }
    } else if (obj instanceof Part) {
        showPart((Part) obj);
    } else {
        log.info("=== Add Part ===");
        log.info((String) (part.getFileName() != null ? "isAttachment" : part.getContent()));
        // log.info("not recognised class:" + obj.getClass().getName() +
        // "\n" + obj);
    }
    log.info("<<== showPart");
}

From source file:org.apache.hupa.server.handler.GetMessageDetailsHandler.java

private boolean handleMultiPartAlternative(Multipart mp, StringBuffer sbPlain)
        throws MessagingException, IOException {
    String text = null;/*from   ww  w.java  2 s.  c o m*/
    boolean isHTML = false;
    for (int i = 0; i < mp.getCount(); i++) {
        Part part = mp.getBodyPart(i);

        String contentType = part.getContentType().toLowerCase();

        // we prefer html
        if (text == null && contentType.startsWith("text/plain")) {
            isHTML = false;
            text = (String) part.getContent();
        } else if (contentType.startsWith("text/html")) {
            isHTML = true;
            text = (String) part.getContent();
        }
    }
    sbPlain.append(text);
    return isHTML;
}

From source file:org.apache.james.transport.mailets.StripAttachment.java

/**
 * Checks every part in this part (if it is a Multipart) for having a
 * filename that matches the pattern. If the name matches, the content of
 * the part is stored (using its name) in te given diretcory.
 * /*  w ww  .  j a  va  2 s . c  o m*/
 * Note: this method is recursive.
 * 
 * @param part
 *            The part to analyse.
 * @param mail
 * @return
 * @throws Exception
 */
private boolean analyseMultipartPartMessage(Part part, Mail mail) throws Exception {
    if (part.isMimeType("multipart/*")) {
        try {
            Multipart multipart = (Multipart) part.getContent();
            boolean atLeastOneRemoved = false;
            int numParts = multipart.getCount();
            for (int i = 0; i < numParts; i++) {
                Part p = multipart.getBodyPart(i);
                if (p.isMimeType("multipart/*")) {
                    atLeastOneRemoved |= analyseMultipartPartMessage(p, mail);
                } else {
                    boolean removed = checkMessageRemoved(p, mail);
                    if (removed) {
                        multipart.removeBodyPart(i);
                        atLeastOneRemoved = true;
                        i--;
                        numParts--;
                    }
                }
            }
            if (atLeastOneRemoved) {
                part.setContent(multipart);
                if (part instanceof Message) {
                    ((Message) part).saveChanges();
                }
            }
            return atLeastOneRemoved;
        } catch (Exception e) {
            log("Could not analyse part.", e);
        }
    }
    return false;
}

From source file:com.szmslab.quickjavamail.receive.MessageLoader.java

/**
 * ?????MessageContent????/*from w  w w.  ja  v  a  2s .  c o m*/
 *
 * @param multiPart
 *            ?
 * @param msgContent
 *            ????
 * @throws MessagingException
 * @throws IOException
 */
private void setMultipartContent(Multipart multiPart, MessageContent msgContent)
        throws MessagingException, IOException {
    for (int i = 0; i < multiPart.getCount(); i++) {
        Part part = multiPart.getBodyPart(i);
        if (part.getContentType().indexOf("multipart") >= 0) {
            setMultipartContent((Multipart) part.getContent(), msgContent);
        } else {
            String disposition = part.getDisposition();
            if (Part.ATTACHMENT.equals(disposition)) {
                // Disposition?"attachment"???ContentType????
                msgContent.attachmentFileList.add(new AttachmentFile(MimeUtility.decodeText(part.getFileName()),
                        part.getDataHandler().getDataSource()));
            } else {
                if (part.isMimeType("text/html")) {
                    msgContent.html = part.getContent().toString();
                } else if (part.isMimeType("text/plain")) {
                    msgContent.text = part.getContent().toString();
                } else {
                    // Disposition?"inline"???ContentType??
                    if (Part.INLINE.equals(disposition)) {
                        String cid = "";
                        if (part instanceof MimeBodyPart) {
                            MimeBodyPart mimePart = (MimeBodyPart) part;
                            cid = mimePart.getContentID();
                        }
                        msgContent.inlineImageFileList
                                .add(new InlineImageFile(cid, MimeUtility.decodeText(part.getFileName()),
                                        part.getDataHandler().getDataSource()));
                    }
                }
            }
        }
    }
}

From source file:com.studiostorti.ZimbraFlowHandler.java

private String getText(Part mimeMessage) throws MessagingException, IOException {
    if (mimeMessage != null) {
        if (mimeMessage.isMimeType("text/plain")) {
            return (String) mimeMessage.getContent();
        }// www  .j  a v a2 s.  c  om

        if (mimeMessage.isMimeType("multipart/alternative")) {
            Multipart multipart = (Multipart) mimeMessage.getContent();
            return getText(multipart.getBodyPart(0));
        } else if (mimeMessage.isMimeType("multipart/*")) {
            Multipart multipart = (Multipart) mimeMessage.getContent();
            for (int i = 0; i < multipart.getCount(); i++) {
                String body = getText(multipart.getBodyPart(i));
                if (!body.equals(""))
                    return body;
            }
        }
    }

    return "";
}

From source file:org.sourceforge.net.javamail4ews.transport.EwsTransport.java

private MessageBody createBodyFromPart(EmailMessage msg, Part part, boolean treatAsAttachement)
        throws MessagingException, IOException, ServiceLocalException {

    MessageBody mb = new MessageBody();
    if (part.isMimeType(TEXT_PLAIN)) {
        String s = (String) part.getContent();
        mb.setBodyType(BodyType.Text);
        mb.setText(s);//from   w  w  w.jav  a 2 s.  c om
    } else if (part.isMimeType(TEXT_STAR)) {
        logger.debug("mime-type is '" + part.getContentType() + "' handling as " + TEXT_HTML);
        String s = (String) part.getContent();
        mb.setBodyType(BodyType.HTML);
        mb.setText(s);
    } else if (part.isMimeType(MULTIPART_ALTERNATIVE) && !treatAsAttachement) {
        logger.debug("mime-type is '" + part.getContentType() + "'");
        Multipart mp = (Multipart) part.getContent();
        String text = "";
        for (int i = 0; i < mp.getCount(); i++) {
            Part p = mp.getBodyPart(i);
            if (p.isMimeType(TEXT_HTML)) {
                text += p.getContent();
            }
        }
        mb.setText(text);
        mb.setBodyType(BodyType.HTML);
        if (!treatAsAttachement)
            createBodyFromPart(msg, part, true);
    } else if (part.isMimeType(MULTIPART_STAR) && !part.isMimeType(MULTIPART_ALTERNATIVE)) {
        logger.debug("mime-type is '" + part.getContentType() + "'");
        Multipart mp = (Multipart) part.getContent();
        int start = 0;
        if (!treatAsAttachement) {
            mb = createBodyFromPart(msg, mp.getBodyPart(start), false);
            start++;
        }
        for (int i = start; i < mp.getCount(); i++) {
            BodyPart lBodyPart = mp.getBodyPart(i);
            byte[] lContentBytes = bodyPart2ByteArray(lBodyPart);

            FileAttachment lNewAttachment;

            String lContentId = getFirstHeaderValue(lBodyPart, "Content-ID");
            if (lContentId != null) {
                lNewAttachment = msg.getAttachments().addFileAttachment(lContentId, lContentBytes);
                lNewAttachment.setContentId(lContentId);
                lNewAttachment.setIsInline(true);

                logger.debug("Attached {} bytes as content {}", lContentBytes.length, lContentId);
            } else {
                String fileName = lBodyPart.getFileName();
                fileName = (fileName == null ? "" + i : fileName);
                lNewAttachment = msg.getAttachments().addFileAttachment(fileName, lContentBytes);
                lNewAttachment.setIsInline(false);
                lNewAttachment.setContentType(lBodyPart.getContentType());

                logger.debug("Attached {} bytes as file {}", lContentBytes.length, fileName);
                logger.debug("content type is {} ", lBodyPart.getContentType());
            }
            lNewAttachment.setIsContactPhoto(false);
        }
    }
    return mb;
}

From source file:com.glaf.mail.business.MailBean.java

/**
 * ??/*from  w w w.j a  va 2s . co m*/
 * 
 * @param part
 * @return
 * @throws MessagingException
 * @throws IOException
 */
public boolean isContainAttch(Part part) throws MessagingException, IOException {
    boolean flag = false;
    if (part.isMimeType("multipart/*")) {
        Multipart multipart = (Multipart) part.getContent();
        int count = multipart.getCount();
        for (int i = 0; i < count; i++) {
            BodyPart bodypart = multipart.getBodyPart(i);
            String dispostion = bodypart.getDisposition();
            if ((dispostion != null)
                    && (dispostion.equals(Part.ATTACHMENT) || dispostion.equals(Part.INLINE))) {
                flag = true;
            } else if (bodypart.isMimeType("multipart/*")) {
                flag = isContainAttch(bodypart);
            } else {
                String contentType = bodypart.getContentType();
                if (contentType.toLowerCase().indexOf("appliaction") != -1) {
                    flag = true;
                }
                if (contentType.toLowerCase().indexOf("name") != -1) {
                    flag = true;
                }
            }
        }
    } else if (part.isMimeType("message/rfc822")) {
        flag = isContainAttch((Part) part.getContent());
    }

    return flag;
}

From source file:org.xmlactions.email.EMailParser.java

private void handlePart(Part part) throws MessagingException, IOException, DocumentException {

    log.debug("\n\n\nhandlePart ==>>");
    log.debug("part.toString():" + part.toString());
    log.debug(/*from  www .jav a2 s .  co m*/
            "part.getContent():" + (part.getFileName() == null ? part.getContent().toString() : "Attachment"));
    log.debug("part.getContentType():" + part.getContentType());
    log.debug("part.getFilename():" + part.getFileName());
    log.debug("part.isAttachment:" + part.getFileName());
    log.debug("part.isMessage:" + (part.getContent() instanceof Message));
    Object obj = part.getContent();
    if (obj instanceof Multipart) {
        Multipart mmp = (Multipart) obj;
        for (int i = 0; i < mmp.getCount(); i++) {
            Part bodyPart = mmp.getBodyPart(i);
            if (bodyPart instanceof Message) {
                setFirstMessageProcessed(true);// need to mark this when we
                // get a forwarded message
                // so we don't look for case
                // numbers in forwarded
                // emails.
            }
            handlePart(bodyPart);
        }
    } else if (obj instanceof Part) {
        if (obj instanceof Message) {
            setFirstMessageProcessed(true);// need to mark this when we get
            // a forwarded message so we
            // don't look for case numbers
            // in forwarded emails.
        }
        handlePart((Part) obj);
    } else {
        if (part instanceof MimeBodyPart) {
            MimeBodyPart p = (MimeBodyPart) part;
            Enumeration enumeration = p.getAllHeaders();
            while (enumeration.hasMoreElements()) {
                Object e = enumeration.nextElement();
                if (e == null)
                    e = null;
            }
            Object content = p.getContent();
            enumeration = p.getAllHeaderLines();
            while (enumeration.hasMoreElements()) {
                Object e = enumeration.nextElement();
                if (e == null)
                    e = null;
            }
            DataHandler dh = p.getDataHandler();
            if (dh == null)
                dh = null;
        }
        addPart(part);
        log.debug("=== Add Part ===");
        log.debug((String) (part.getFileName() != null ? "isAttachment" : part.getContent()));
        // log.info("not recognised class:" + obj.getClass().getName() +
        // "\n" + obj);
    }
    log.debug("<<== handlePart");
}

From source file:com.glaf.mail.business.MailBean.java

/**
 * ?//from w ww . j  a  va 2s. c  om
 * 
 * @param part
 * @throws MessagingException
 * @throws IOException
 */
public void parseAttachment(Part part) throws MessagingException, IOException {
    String filename = "";
    if (part.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) part.getContent();
        for (int i = 0; i < mp.getCount(); i++) {
            BodyPart mpart = mp.getBodyPart(i);
            String dispostion = mpart.getDisposition();
            if ((dispostion != null)
                    && (dispostion.equals(Part.ATTACHMENT) || dispostion.equals(Part.INLINE))) {
                filename = mpart.getFileName();
                if (filename != null) {
                    logger.debug("orig filename=" + filename);
                    if (filename.indexOf("?") != -1) {
                        filename = new String(filename.getBytes("GBK"), "UTF-8");
                    }
                    if (filename.toLowerCase().indexOf("gb2312") != -1) {
                        filename = MimeUtility.decodeText(filename);
                    }
                    filename = MailUtils.convertString(filename);
                    logger.debug("filename=" + filename);
                    parseFileContent(filename, mpart.getInputStream());
                }
            } else if (mpart.isMimeType("multipart/*")) {
                parseAttachment(mpart);
            } else {
                filename = mpart.getFileName();
                if (filename != null) {
                    logger.debug("orig filename=" + filename);
                    if (filename.indexOf("?") != -1) {
                        filename = new String(filename.getBytes("GBK"), "UTF-8");
                    }
                    if (filename.toLowerCase().indexOf("gb2312") != -1) {
                        filename = MimeUtility.decodeText(filename);
                    }
                    filename = MailUtils.convertString(filename);
                    parseFileContent(filename, mpart.getInputStream());
                    logger.debug("filename=" + filename);
                }
            }
        }

    } else if (part.isMimeType("message/rfc822")) {
        parseAttachment((Part) part.getContent());
    }
}

From source file:mitm.common.mail.filter.UnsupportedFormatStripper.java

private void scanPart(Part part, PartsContext context, int depth) throws MessagingException, IOException {
    if (depth > MAX_DEPTH) {
        return;// w w w.j  a v  a 2 s.  c om
    }

    if (part.isMimeType("multipart/mixed")) {
        MimeMultipart parts = (MimeMultipart) part.getContent();

        int partCount = parts.getCount();

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

            scanPart(child, context, ++depth);
        }
    } else if (part.isMimeType("multipart/*")) {
        /*
         * We will always add any multipart that's not a mixed multipart
         */
        context.getParts().add(part);
    } else if (part.isMimeType("text/plain")) {
        context.getParts().add(part);
    } else if (part.isMimeType("text/html")) {
        context.getParts().add(part);
    } else if (!addSupportedPart(part, context)) {
        skipPart(part, true, context);
    }
}