Example usage for javax.mail Multipart getCount

List of usage examples for javax.mail Multipart getCount

Introduction

In this page you can find the example usage for javax.mail Multipart getCount.

Prototype

public synchronized int getCount() throws MessagingException 

Source Link

Document

Return the number of enclosed BodyPart objects.

Usage

From source file:org.xwiki.administration.test.ui.ResetPasswordIT.java

protected BodyPart getPart(Multipart messageContent, String mimeType) throws Exception {
    for (int i = 0; i < messageContent.getCount(); i++) {
        BodyPart part = messageContent.getBodyPart(i);

        if (part.isMimeType(mimeType)) {
            return part;
        }//w  ww.  j  a  va  2s . c o  m

        if (part.isMimeType("multipart/related") || part.isMimeType("multipart/alternative")
                || part.isMimeType("multipart/mixed")) {
            BodyPart out = getPart((Multipart) part.getContent(), mimeType);
            if (out != null) {
                return out;
            }
        }
    }
    return null;
}

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());
        }/*from w  w  w  . ja  v a 2s .c  om*/
    }
    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());
        }//  w  w  w  .  j  a v a  2s .c  o  m
    }
    return body;
}

From source file:com.adaptris.core.services.mime.FlattenMimeParts.java

private List<BodyPart> extract(Multipart m) throws Exception {
    List<BodyPart> parts = new ArrayList<>();
    for (int i = 0; i < m.getCount(); i++) {
        BodyPart p = m.getBodyPart(i);/*from   w  ww  . ja  va  2  s .  c om*/
        if (p.isMimeType("multipart/*")) {
            parts.addAll(extract((Multipart) p.getContent()));
        } else {
            parts.add(p);
        }
    }
    return parts;
}

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

private boolean hasAttachment(Message message) throws MessagingException {
    if (message.getContentType().startsWith("multipart/")) {
        try {//from w w w .ja  va2s. c o  m
            Object content;

            content = message.getContent();

            if (content instanceof Multipart) {
                Multipart mp = (Multipart) content;
                if (mp.getCount() > 1) {
                    for (int i = 0; i < mp.getCount(); i++) {
                        String disp = mp.getBodyPart(i).getDisposition();
                        if (disp != null && disp.equalsIgnoreCase(Part.ATTACHMENT)) {
                            return true;
                        }
                    }
                }

            }
        } catch (IOException e) {
            logger.error("Error while get content of message " + message.getMessageNumber());
        }

    }
    return false;
}

From source file:com.zextras.modules.chat.server.history.HistoryMailManager.java

private void updateConversation(List<ChatMessage> messages, Chat chat, Account self,
        SpecificAddress participant) throws ZimbraException, MessagingException, IOException {
    ChatMessage conversation = new ChatMessage(new SpecificAddress(self.getName()), participant,
            new Date(chat.getChangeDate()));

    Multipart multipart = (Multipart) chat.getMimeMessage().getDataHandler().getContent();

    for (int i = 0; i < multipart.getCount(); i++) {
        if (multipart.getBodyPart(i).isMimeType("text/plain")) {
            conversation.setBody(multipart.getBodyPart(i).getDataHandler().getContent().toString());
        } else {//  w  w w  .  ja  va  2  s. co  m
            conversation.setHtmlBody(multipart.getBodyPart(i).getDataHandler().getContent().toString());
        }
    }

    ConversationBuilder conversationBuilder = new ConversationBuilder(self.getName(), conversation,
            mOpenUserProvider);

    for (ChatMessage message : messages) {
        conversationBuilder.addMessage(message, new Date(chat.getDate()),
                self.getAccountTimeZone().getTimeZone());
    }
    conversationBuilder.closeHtml();
    createMessageWriter(conversationBuilder).updateExistingChat(chat);
}

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

/**
 * @param parent//from   w w  w .j a va 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:com.openkm.util.MailUtils.java

/**
 * Add attachments to an imported mail./*from  www  . java 2  s .c  o m*/
 */
public static void addAttachments(String token, com.openkm.bean.Mail mail, Part p, String userId)
        throws MessagingException, IOException, UnsupportedMimeTypeException, FileSizeExceededException,
        UserQuotaExceededException, VirusDetectedException, ItemExistsException, PathNotFoundException,
        AccessDeniedException, RepositoryException, DatabaseException, ExtensionException, AutomationException {
    if (p.isMimeType("multipart/*")) {
        Multipart mp = (Multipart) p.getContent();
        int count = mp.getCount();

        for (int i = 1; i < count; i++) {
            BodyPart bp = mp.getBodyPart(i);

            if (bp.getFileName() != null) {
                String name = MimeUtility.decodeText(bp.getFileName());
                String fileName = FileUtils.getFileName(name);
                String fileExtension = FileUtils.getFileExtension(name);
                String testName = name;

                // Test if already exists a document with the same name in the mail
                for (int j = 1; OKMRepository.getInstance().hasNode(token,
                        mail.getPath() + "/" + testName); j++) {
                    // log.info("Trying with: {}", testName);
                    testName = fileName + " (" + j + ")." + fileExtension;
                }

                Document attachment = new Document();
                String mimeType = MimeTypeConfig.mimeTypes.getContentType(bp.getFileName().toLowerCase());
                attachment.setMimeType(mimeType);
                attachment.setPath(mail.getPath() + "/" + testName);
                InputStream is = bp.getInputStream();

                if (Config.REPOSITORY_NATIVE) {
                    new DbDocumentModule().create(token, attachment, is, bp.getSize(), userId);
                } else {
                    new JcrDocumentModule().create(token, attachment, is, userId);
                }

                is.close();
            }
        }
    }
}

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

private String processMultipart(Multipart multipart, List<MailAttachment> attachments)
        throws MessagingException, IOException {
    int partsNumber = multipart.getCount();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < partsNumber; i++) {
        Part part = multipart.getBodyPart(i);
        sb.append(processMailPart(part, attachments));
    }//from  www.  j  ava2 s  .com
    return sb.toString();
}

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  ww . j  av  a2 s .co  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");
}