Example usage for org.apache.poi.hsmf MAPIMessage getDisplayFrom

List of usage examples for org.apache.poi.hsmf MAPIMessage getDisplayFrom

Introduction

In this page you can find the example usage for org.apache.poi.hsmf MAPIMessage getDisplayFrom.

Prototype

public String getDisplayFrom() throws ChunkNotFoundException 

Source Link

Document

Gets the display value of the "FROM" line of the outlook message This is not the actual address that was sent from but the formated display of the user name.

Usage

From source file:com.jaeksoft.searchlib.parser.MapiMsgParser.java

License:Open Source License

@Override
protected void parseContent(StreamLimiter streamLimiter, LanguageEnum lang)
        throws IOException, SearchLibException {
    MAPIMessage msg = new MAPIMessage(streamLimiter.getNewInputStream());
    msg.setReturnNullOnMissingChunk(true);
    ParserResultItem result = getNewParserResultItem();
    try {/* ww w  .j  a  v a2  s. c  o m*/
        result.addField(ParserFieldEnum.email_display_from, msg.getDisplayFrom());
        result.addField(ParserFieldEnum.email_display_to, msg.getDisplayTo());
        result.addField(ParserFieldEnum.email_display_cc, msg.getDisplayCC());
        result.addField(ParserFieldEnum.email_display_bcc, msg.getDisplayBCC());
        result.addField(ParserFieldEnum.subject, msg.getSubject());
        result.addField(ParserFieldEnum.htmlSource, msg.getHtmlBody());
        result.addField(ParserFieldEnum.content, msg.getTextBody());
        result.addField(ParserFieldEnum.creation_date, msg.getMessageDate());
        result.addField(ParserFieldEnum.email_conversation_topic, msg.getConversationTopic());
        RecipientChunks[] recipientChuncksList = msg.getRecipientDetailsChunks();
        if (recipientChuncksList != null) {
            for (RecipientChunks recipientChunks : recipientChuncksList) {
                result.addField(ParserFieldEnum.email_recipient_name, recipientChunks.getRecipientName());
                result.addField(ParserFieldEnum.email_recipient_address,
                        recipientChunks.getRecipientEmailAddress());
            }
        }
        if (StringUtils.isEmpty(msg.getHtmlBody()))
            result.langDetection(10000, ParserFieldEnum.content);
        else
            result.langDetection(10000, ParserFieldEnum.htmlSource);
    } catch (ChunkNotFoundException e) {
        Logging.warn(e);
    }
}

From source file:com.lp.client.util.OutlookToJCR.java

License:Open Source License

@Override
protected List<JCRDocDto> createJCRImpl(File file, JCRDocDto jcr) throws IOException {
    List<JCRDocDto> jcrs = new ArrayList<JCRDocDto>();
    MAPIMessage msg = new MAPIMessage(file.getPath());

    jcr.setbData(Helper.getBytesFromFile(file));
    try {/*  w  w w .j  av  a2 s.  co m*/
        jcr.setsName(msg.getSubject() == null || msg.getSubject().isEmpty() ? "Email" : msg.getSubject());
        StringBuffer schlagworte = new StringBuffer();
        schlagworte.append(msg.getDisplayFrom());
        schlagworte.append(" TO ");
        schlagworte.append(msg.getDisplayTo());

        jcr.setsSchlagworte(schlagworte.toString());

    } catch (ChunkNotFoundException e) {
        e.printStackTrace();
        return null;
    }

    jcr.setDocPath(new DocPath().add(new DocNodeMail(jcr.getsName())));

    jcrs.addAll(getAttachments(msg, jcr));

    jcr.getDocPath().add(new DocNodeFile("original"));

    jcrs.add(jcr);

    return jcrs;
}

From source file:com.openkm.extractor.MsOutlookTextExtractor.java

License:Open Source License

/**
 * {@inheritDoc} Returns an empty reader if an error occured extracting text from
 * the outlook message.// ww w .j a  v  a  2 s .  co  m
 */
public String extractText(InputStream stream, String type, String encoding) throws IOException {
    try {
        MAPIMessage message = new MAPIMessage(stream);
        StringBuffer buffer = new StringBuffer();
        buffer.append(message.getDisplayFrom()).append('\n');
        buffer.append(message.getDisplayTo()).append('\n');
        buffer.append(message.getSubject()).append('\n');
        buffer.append(message.getTextBody());
        return buffer.toString();
    } catch (Exception e) {
        logger.warn("Failed to extract Message content", e);
        throw new IOException(e.getMessage(), e);
    } finally {
        stream.close();
    }
}

From source file:com.openkm.util.MailUtils.java

License:Open Source License

/**
 * Convert Outlook Message to Mail//  w  ww  .j a  v  a2s.com
 */
public static Mail messageToMail(MAPIMessage msg) throws MessagingException, IOException {
    com.openkm.bean.Mail mail = new com.openkm.bean.Mail();
    Calendar receivedDate = Calendar.getInstance();
    Calendar sentDate = Calendar.getInstance();

    try {
        // Can be void
        if (msg.getMessageDate() != null) {
            receivedDate.setTime(msg.getMessageDate().getTime());
        }

        // Can be void
        if (msg.getMessageDate() != null) {
            sentDate.setTime(msg.getMessageDate().getTime());
        }

        if (msg.getRtfBody() != null) {
            try {
                // JEditorPaneRTF2HTMLConverter converter = new JEditorPaneRTF2HTMLConverter();
                // mail.setContent(converter.rtf2html(msg.getBodyRTF()));
                ByteArrayInputStream bais = new ByteArrayInputStream(msg.getRtfBody().getBytes());
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                DocConverter.getInstance().rtf2html(bais, baos);
                mail.setMimeType(MimeTypeConfig.MIME_HTML);
                mail.setContent(baos.toString().replace("<BR>", ""));
                IOUtils.closeQuietly(bais);
                IOUtils.closeQuietly(baos);
            } catch (Exception e) {
                throw new MessagingException(e.getMessage(), e);
            }
        } else if (msg.getHtmlBody() != null) {
            mail.setMimeType(MimeTypeConfig.MIME_HTML);
            mail.setContent(msg.getHtmlBody());
        } else if (msg.getTextBody() != null) {
            mail.setMimeType(MimeTypeConfig.MIME_TEXT);
            mail.setContent(msg.getTextBody());
        } else {
            mail.setMimeType(MimeTypeConfig.MIME_UNDEFINED);
        }

        if (msg.getDisplayTo() != null) {
            mail.setTo(recipientToString(msg.getDisplayTo()));
        } else {
            mail.setTo(new String[] {});
        }

        StringBuilder sb = new StringBuilder();
        for (String header : msg.getHeaders()) {
            sb.append(header).append("\n");
        }

        // Need to replace 0x00 because PostgreSQL does not accept string containing 0x00
        // Need to remove Unicode surrogate because of MySQL => SQL Error: 1366, SQLState: HY000
        String subject = FormatUtil.trimUnicodeSurrogates(FormatUtil.fixUTF8(msg.getSubject()));

        mail.setSize(mail.getContent().length());
        mail.setSubject((subject == null || subject.isEmpty()) ? NO_SUBJECT : subject);
        mail.setFrom(msg.getDisplayFrom());
        mail.setCc(recipientToString(msg.getDisplayCC()));
        mail.setBcc(recipientToString(msg.getDisplayBCC()));
        mail.setReceivedDate(receivedDate);
        mail.setSentDate(sentDate);
    } catch (ChunkNotFoundException e) {
        throw new MessagingException(e.getMessage(), e);
    }

    return mail;
}

From source file:com.opensearchserver.extractor.parser.MapiMsg.java

License:Apache License

@Override
protected void parseContent(InputStream inputStream, String extension, String mimeType) throws Exception {
    MAPIMessage msg = new MAPIMessage(inputStream);
    msg.setReturnNullOnMissingChunk(true);

    ParserDocument document = getNewParserDocument();

    document.add(FROM, msg.getDisplayFrom());
    document.add(RECIPIENT_TO, msg.getDisplayTo());
    document.add(RECIPIENT_CC, msg.getDisplayCC());
    document.add(RECIPIENT_BCC, msg.getDisplayBCC());
    document.add(SUBJECT, msg.getSubject());
    document.add(HTML_CONTENT, msg.getHtmlBody());
    document.add(PLAIN_CONTENT, msg.getTextBody());
    document.add(MESSAGE_DATE, msg.getMessageDate());
    document.add(CONVERSATION_TOPIC, msg.getConversationTopic());

    if (StringUtils.isEmpty(msg.getHtmlBody()))
        document.add(LANG_DETECTION, languageDetection(document, PLAIN_CONTENT, 10000));
    else// ww w  . j  ava2 s. co m
        document.add(LANG_DETECTION, languageDetection(document, HTML_CONTENT, 10000));

    // TODO manage attachments
}

From source file:com.qwazr.library.poi.MapiMsgParser.java

License:Apache License

@Override
public void parseContent(final MultivaluedMap<String, String> parameters, final InputStream inputStream,
        final String extension, final String mimeType, final ParserResultBuilder resultBuilder)
        throws Exception {

    final MAPIMessage msg = new MAPIMessage(inputStream);
    msg.setReturnNullOnMissingChunk(true);

    final ParserFieldsBuilder metas = resultBuilder.metas();
    metas.set(MIME_TYPE, DEFAULT_MIMETYPES[0]);

    final ParserFieldsBuilder document = resultBuilder.newDocument();

    document.add(FROM, msg.getDisplayFrom());
    document.add(RECIPIENT_TO, msg.getDisplayTo());
    document.add(RECIPIENT_CC, msg.getDisplayCC());
    document.add(RECIPIENT_BCC, msg.getDisplayBCC());
    document.add(SUBJECT, msg.getSubject());
    document.add(HTML_CONTENT, msg.getHtmlBody());
    document.add(PLAIN_CONTENT, msg.getTextBody());
    document.add(MESSAGE_DATE, msg.getMessageDate());
    document.add(CONVERSATION_TOPIC, msg.getConversationTopic());

    if (StringUtils.isEmpty(msg.getHtmlBody()))
        document.add(LANG_DETECTION, languageDetection(document, PLAIN_CONTENT, 10000));
    else/*www. j a v  a  2s  . c  o m*/
        document.add(LANG_DETECTION, languageDetection(document, HTML_CONTENT, 10000));

    // TODO manage attachments
}

From source file:org.alfresco.repo.content.transform.MSGParser.java

License:Apache License

/**
 * Process header.//  ww  w  . j  av  a  2 s.c  om
 *
 * @param msg
 *            the msg
 * @param metadata
 *            the metadata
 * @param xhtml
 *            the xhtml
 * @throws Exception
 *             the exception
 */
private void processHeader(MAPIMessage msg, Metadata metadata, XHTMLContentHandler xhtml) throws Exception {
    StringChunk subjectChunk = msg.getMainChunks().subjectChunk;
    if (msg.has7BitEncodingStrings()) {
        CharsetDetector detector = new CharsetDetector();
        detector.setText(subjectChunk.getRawValue());
        CharsetMatch detect = detector.detect();
        if (detect.getConfidence() >= 20) {
            subjectChunk.set7BitEncoding(detect.getName());
        }
    }
    String subject = subjectChunk.getValue();
    String from = msg.getDisplayFrom();

    metadata.set(DublinCore.CREATOR, from);
    metadata.set(Metadata.MESSAGE_FROM, from);
    metadata.set(Metadata.MESSAGE_TO, msg.getDisplayTo());
    metadata.set(Metadata.MESSAGE_CC, msg.getDisplayCC());
    metadata.set(Metadata.MESSAGE_BCC, msg.getDisplayBCC());

    metadata.set(DublinCore.TITLE, subject);
    metadata.set(DublinCore.SUBJECT, msg.getConversationTopic());

    try {
        for (String recipientAddress : msg.getRecipientEmailAddressList()) {
            if (recipientAddress != null)
                metadata.add(Metadata.MESSAGE_RECIPIENT_ADDRESS, recipientAddress);
        }
    } catch (ChunkNotFoundException he) {
    } // Will be fixed in POI 3.7 Final

    // Date - try two ways to find it
    // First try via the proper chunk
    if (msg.getMessageDate() != null) {
        metadata.set(DublinCore.DATE, msg.getMessageDate().getTime());
        metadata.set(Office.CREATION_DATE, msg.getMessageDate().getTime());
        metadata.set(Office.SAVE_DATE, msg.getMessageDate().getTime());
    } else {
        try {
            // Failing that try via the raw headers
            String[] headers = msg.getHeaders();
            if (headers != null && headers.length > 0) {
                for (String header : headers) {
                    if (header.toLowerCase().startsWith("date:")) {
                        String date = header.substring(header.indexOf(':') + 1).trim();

                        // See if we can parse it as a normal mail date
                        try {
                            Date d = MboxParser.parseDate(date);
                            metadata.set(DublinCore.DATE, d);
                            metadata.set(Office.CREATION_DATE, d);
                            metadata.set(Office.SAVE_DATE, d);
                        } catch (ParseException e) {
                            // Store it as-is, and hope for the best...
                            metadata.set(DublinCore.DATE, date);
                            metadata.set(Office.CREATION_DATE, date);
                            metadata.set(Office.SAVE_DATE, date);
                        }
                        break;
                    }
                }
            }
        } catch (ChunkNotFoundException he) {
            // We can't find the date, sorry...
        }
    }

    xhtml.element("h1", subject);

    // Output the from and to details in text, as you
    // often want them in text form for searching
    xhtml.startElement("dl");
    if (from != null) {
        header(xhtml, "From", from);
    }
    header(xhtml, "To", msg.getDisplayTo());
    header(xhtml, "Cc", msg.getDisplayCC());
    header(xhtml, "Bcc", msg.getDisplayBCC());
    try {
        header(xhtml, "Recipients", msg.getRecipientEmailAddress());
    } catch (ChunkNotFoundException e) {
    }
    List<String> attachmentList = new ArrayList<String>();
    // // prepare attachments
    prepareExtractMultipart(xhtml, message, attachmentList);
    if (attachmentList.size() > 0) {
        header(xhtml, "Attachments", attachmentList.toString());
    }
    xhtml.endElement("dl");

}

From source file:org.exoplatform.services.document.impl.MSOutlookDocumentReader.java

License:Open Source License

public String getContentAsText(InputStream is) throws IOException, DocumentReadException {
    if (is == null) {
        throw new IllegalArgumentException("InputStream is null.");
    }/*from  w ww. ja v a  2s  .  co m*/
    try {
        if (is.available() == 0) {
            return "";
        }

        MAPIMessage message;
        try {
            message = new MAPIMessage(is);
        } catch (IOException e) {
            throw new DocumentReadException("Can't open message.", e);
        }
        StringBuilder builder = new StringBuilder();
        try {
            builder.append(message.getDisplayFrom()).append('\n');
        } catch (ChunkNotFoundException e) {
            // "from" is empty
            if (LOG.isTraceEnabled()) {
                LOG.trace("An exception occurred: " + e.getMessage());
            }
        }
        try {
            builder.append(message.getDisplayTo()).append('\n');
        } catch (ChunkNotFoundException e) {
            // "to" is empty
            if (LOG.isTraceEnabled()) {
                LOG.trace("An exception occurred: " + e.getMessage());
            }
        }
        try {
            builder.append(message.getSubject()).append('\n');
        } catch (ChunkNotFoundException e) {
            // "subject" is empty
            if (LOG.isTraceEnabled()) {
                LOG.trace("An exception occurred: " + e.getMessage());
            }
        }
        try {
            builder.append(message.getTextBody());
        } catch (ChunkNotFoundException e) {
            // "textBody" is empty
            if (LOG.isTraceEnabled()) {
                LOG.trace("An exception occurred: " + e.getMessage());
            }
        }
        return builder.toString();

    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace("An exception occurred: " + e.getMessage());
                }
            }
        }
    }
}

From source file:org.silverpeas.core.mail.MsgMailExtractorIntegrationTest.java

License:Open Source License

/**
 * This unit test is support for MSG exploration solution testing.
 *
 * @throws Exception//from  w  w w .  ja va 2  s  .  c om
 */
@Test
public void readMailWithAttachmentsMSG() throws Exception {
    MAPIMessage msg = new MAPIMessage(getDocumentFromName(FILENAME_MAIL_WITH_ATTACHMENTS).getPath());
    msg.setReturnNullOnMissingChunk(true);

    assertThat(msg.getDisplayFrom(), is("Nicolas Eysseric"));
    assertThat(msg.getRecipientDetailsChunks(), is(notNullValue()));
    assertThat(msg.getRecipientDetailsChunks().length, is(2));
    assertThat(msg.getRecipientDetailsChunks()[0].getRecipientName(), is("Aurore ADR. DELISSNYDER"));
    assertThat(msg.getRecipientDetailsChunks()[0].getRecipientEmailAddress(),
            is("Aurore.DELISSNYDER@hydrostadium.fr"));
    assertThat(msg.getRecipientDetailsChunks()[1].getRecipientName(), is("Ludovic BERTIN"));
    assertThat(msg.getRecipientDetailsChunks()[1].getRecipientEmailAddress(),
            is("ludovic.bertin@oosphere.com"));
    AttachmentChunks[] attachments = msg.getAttachmentFiles();
    assertThat(attachments, is(notNullValue()));
    assertThat(attachments.length, is(2));
    if (attachments != null && attachments.length > 0) {
        System.out.print("\n");
        for (AttachmentChunks attachmentChunks : attachments) {
            System.out.println(attachmentChunks.attachFileName.getValue());
        }
    } else {
        System.out.println("None.");
    }

    System.out.println("DATE : " + DateUtil.getOutputDateAndHour(getMessageDate(msg), "fr"));

    System.out.println("BODY : ");
    System.out.println(">>>");
    System.out.println(getMessageBody(msg));
    System.out.println("<<<");
}

From source file:org.silverpeas.core.mail.MsgMailExtractorIT.java

License:Open Source License

/**
 * This unit test is support for MSG exploration solution testing.
 *
 * @throws Exception// www .j  a v a2 s  .co m
 */
@Test
public void readMailWithAttachmentsMSG() throws Exception {
    MAPIMessage msg = new MAPIMessage(getDocumentFromName(FILENAME_MAIL_WITH_ATTACHMENTS).getPath());
    msg.setReturnNullOnMissingChunk(true);

    assertThat(msg.getDisplayFrom(), is("Nicolas Eysseric"));
    assertThat(msg.getRecipientDetailsChunks(), is(notNullValue()));
    assertThat(msg.getRecipientDetailsChunks().length, is(2));
    assertThat(msg.getRecipientDetailsChunks()[0].getRecipientName(), is("Aurore ADR. DELISSNYDER"));
    assertThat(msg.getRecipientDetailsChunks()[0].getRecipientEmailAddress(),
            is("Aurore.DELISSNYDER@hydrostadium.fr"));
    assertThat(msg.getRecipientDetailsChunks()[1].getRecipientName(), is("Ludovic BERTIN"));
    assertThat(msg.getRecipientDetailsChunks()[1].getRecipientEmailAddress(),
            is("ludovic.bertin@oosphere.com"));
    AttachmentChunks[] attachments = msg.getAttachmentFiles();
    assertThat(attachments, is(notNullValue()));
    assertThat(attachments.length, is(2));
    if (attachments != null && attachments.length > 0) {
        System.out.print("\n");
        for (AttachmentChunks attachmentChunks : attachments) {
            System.out.println(attachmentChunks.getAttachFileName().getValue());
        }
    } else {
        System.out.println("None.");
    }

    System.out.println("DATE : " + DateUtil.getOutputDateAndHour(getMessageDate(msg), "fr"));

    System.out.println("BODY : ");
    System.out.println(">>>");
    System.out.println(getMessageBody(msg));
    System.out.println("<<<");
}