Example usage for javax.mail BodyPart getDescription

List of usage examples for javax.mail BodyPart getDescription

Introduction

In this page you can find the example usage for javax.mail BodyPart getDescription.

Prototype

public String getDescription() throws MessagingException;

Source Link

Document

Return a description String for this part.

Usage

From source file:MultipartViewer.java

protected void setupDisplay(Multipart mp) {
    // we display the first body part in a main frame on the left, and then
    // on the right we display the rest of the parts as attachments

    GridBagConstraints gc = new GridBagConstraints();
    gc.gridheight = GridBagConstraints.REMAINDER;
    gc.fill = GridBagConstraints.BOTH;
    gc.weightx = 1.0;//from w  ww. j  a  va2  s. c o  m
    gc.weighty = 1.0;

    // get the first part
    try {
        BodyPart bp = mp.getBodyPart(0);
        Component comp = getComponent(bp);
        add(comp, gc);

    } catch (MessagingException me) {
        add(new Label(me.toString()), gc);
    }

    // see if there are more than one parts
    try {
        int count = mp.getCount();

        // setup how to display them
        gc.gridwidth = GridBagConstraints.REMAINDER;
        gc.gridheight = 1;
        gc.fill = GridBagConstraints.NONE;
        gc.anchor = GridBagConstraints.NORTH;
        gc.weightx = 0.0;
        gc.weighty = 0.0;
        gc.insets = new Insets(4, 4, 4, 4);

        // for each one we create a button with the content type
        for (int i = 1; i < count; i++) { // we skip the first one 
            BodyPart curr = mp.getBodyPart(i);
            String label = null;
            if (label == null)
                label = curr.getFileName();
            if (label == null)
                label = curr.getDescription();
            if (label == null)
                label = curr.getContentType();

            Button but = new Button(label);
            but.addActionListener(new AttachmentViewer(curr));
            add(but, gc);
        }

    } catch (MessagingException me2) {
        me2.printStackTrace();
    }

}

From source file:fr.gouv.culture.vitam.eml.EmlExtract.java

private static final String addSubIdentities(Element identification, BodyPart bp, InputStream inputStream,
        VitamArgument argument, ConfigLoader config) {
    Element newElt = XmlDom.factory.createElement(EMAIL_FIELDS.subidentity.name);
    String filename = null;/*www .  j  a v a 2 s .  c  o m*/
    String result = "";
    try {
        filename = bp.getFileName();
        filename = StringUtils.toFileName(filename);
        if (filename != null) {
            Element elt = XmlDom.factory.createElement(EMAIL_FIELDS.filename.name);
            elt.setText(filename);
            newElt.add(elt);
        } else {
            filename = "eml.eml";
        }
    } catch (MessagingException e) {
    }
    try {
        int size = bp.getSize();
        if (size > 0) {
            Element elt = XmlDom.factory.createElement(EMAIL_FIELDS.attSize.name);
            elt.setText(Integer.toString(size));
            newElt.add(elt);
        }
    } catch (MessagingException e) {
    }
    try {
        String description = bp.getDescription();
        if (description != null) {
            Element elt = XmlDom.factory.createElement(EMAIL_FIELDS.description.name);
            elt.setText(description);
            newElt.add(elt);
        }
    } catch (MessagingException e) {
    }
    try {
        String disposition = bp.getDisposition();
        if (disposition != null) {
            Element elt = XmlDom.factory.createElement(EMAIL_FIELDS.disposition.name);
            elt.setText(disposition);
            newElt.add(elt);
        }
    } catch (MessagingException e) {
    }
    File filetemp = null;
    FileOutputStream outputStream = null;
    try {
        // Force out to analysis
        if (config.extractFile) {
            filetemp = new File(argument.currentOutputDir, filename);
        } else {
            filetemp = File.createTempFile(StaticValues.PREFIX_TEMPFILE, filename);
        }
        byte[] buffer = new byte[8192];
        int read = 0;
        outputStream = new FileOutputStream(filetemp);
        while ((read = inputStream.read(buffer)) >= 0) {
            outputStream.write(buffer, 0, read);
        }
        outputStream.close();
        outputStream = null;
    } catch (IOException e1) {
        if (filetemp != null && !config.extractFile) {
            filetemp.delete();
        }
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (IOException e) {
            }
        }
        String status = "Error during access to attachment";
        newElt.addAttribute(EMAIL_FIELDS.status.name, status);
        identification.add(newElt);
        return "";
    }
    try {
        Commands.addFormatIdentification(newElt, filename, filetemp, config, argument);
        if (argument.extractKeyword) {
            // get back keyword in the main list
            Element keyw = (Element) newElt.selectSingleNode(EMAIL_FIELDS.keywords.name);
            if (keyw != null) {
                StringBuilder builder = new StringBuilder();
                @SuppressWarnings("unchecked")
                List<Element> elts = (List<Element>) keyw.selectNodes(EMAIL_FIELDS.keywordRank.name);
                for (Element elt : elts) {
                    String value = elt.attributeValue(EMAIL_FIELDS.keywordOccur.name);
                    int occur = Integer.parseInt(value) / 2 + 1;
                    @SuppressWarnings("unchecked")
                    List<Element> words = (List<Element>) elt.selectNodes(EMAIL_FIELDS.keywordWord.name);
                    for (Element eword : words) {
                        String word = eword.attributeValue(EMAIL_FIELDS.keywordValue.name) + " ";
                        for (int i = 0; i < occur; i++) {
                            builder.append(word);
                        }
                    }
                }
                result = builder.toString().trim();
            }
        }

    } catch (Exception e) {
        String status = "Error during identification";
        e.printStackTrace();
        config.addRankId(newElt);
        newElt.addAttribute(EMAIL_FIELDS.status.name, status);
    }
    if (filetemp != null && !config.extractFile) {
        filetemp.delete();
    }
    identification.add(newElt);
    return result;
}

From source file:com.seleniumtests.connectors.mails.ImapClient.java

/**
 * get list of all emails in folder/*from  w  w w .j  a  v  a  2s .c  o  m*/
 * 
 * @param folderName      folder to read
 * @param firstMessageTime   date from which we should get messages
 * @param firstMessageIndex index of the firste message to find
 * @throws MessagingException
 * @throws IOException
 */
@Override
public List<Email> getEmails(String folderName, int firstMessageIndex, LocalDateTime firstMessageTime)
        throws MessagingException, IOException {

    if (folderName == null) {
        throw new MessagingException("folder ne doit pas tre vide");
    }

    // Get folder
    Folder folder = store.getFolder(folderName);
    folder.open(Folder.READ_ONLY);

    // Get directory
    Message[] messages = folder.getMessages();

    List<Message> preFilteredMessages = new ArrayList<>();

    final LocalDateTime firstTime = firstMessageTime;

    // on filtre les message en fonction du mode de recherche
    if (searchMode == SearchMode.BY_INDEX || firstTime == null) {
        for (int i = firstMessageIndex, n = messages.length; i < n; i++) {
            preFilteredMessages.add(messages[i]);
        }
    } else {
        preFilteredMessages = Arrays.asList(folder.search(new SearchTerm() {
            private static final long serialVersionUID = 1L;

            @Override
            public boolean match(Message msg) {
                try {
                    return !msg.getReceivedDate()
                            .before(Date.from(firstTime.atZone(ZoneId.systemDefault()).toInstant()));
                } catch (MessagingException e) {
                    return false;
                }
            }
        }));

    }

    List<Email> filteredEmails = new ArrayList<>();
    lastMessageIndex = messages.length;

    for (Message message : preFilteredMessages) {

        String contentType = "";
        try {
            contentType = message.getContentType();
        } catch (MessagingException e) {
            MimeMessage msg = (MimeMessage) message;
            message = new MimeMessage(msg);
            contentType = message.getContentType();
        }

        // decode content
        String messageContent = "";
        List<String> attachments = new ArrayList<>();

        if (contentType.toLowerCase().contains("text/html")) {
            messageContent += StringEscapeUtils.unescapeHtml4(message.getContent().toString());
        } else if (contentType.toLowerCase().contains("multipart/")) {
            List<BodyPart> partList = getMessageParts((Multipart) message.getContent());

            // store content in list
            for (BodyPart part : partList) {

                String partContentType = part.getContentType().toLowerCase();
                if (partContentType.contains("text/html")) {
                    messageContent = messageContent
                            .concat(StringEscapeUtils.unescapeHtml4(part.getContent().toString()));

                } else if (partContentType.contains("text/") && !partContentType.contains("vcard")) {
                    messageContent = messageContent.concat((String) part.getContent().toString());

                } else if (partContentType.contains("image") || partContentType.contains("application/")
                        || partContentType.contains("text/x-vcard")) {
                    if (part.getFileName() != null) {
                        attachments.add(part.getFileName());
                    } else {
                        attachments.add(part.getDescription());
                    }
                } else {
                    logger.debug("type: " + part.getContentType());
                }
            }
        }

        // create a new email
        filteredEmails.add(new Email(message.getSubject(), messageContent, "",
                message.getReceivedDate().toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(),
                attachments));
    }

    folder.close(false);

    return filteredEmails;
}