Search in sources :

Example 11 with AttachmentChunks

use of org.apache.poi.hsmf.datatypes.AttachmentChunks in project poi by apache.

the class Msg2txt method processMessage.

/**
	 * Processes the message.
	 * 
	 * @throws IOException if an exception occurs while writing the message out
	 */
public void processMessage() throws IOException {
    String txtFileName = fileNameStem + ".txt";
    String attDirName = fileNameStem + "-att";
    PrintWriter txtOut = null;
    try {
        txtOut = new PrintWriter(txtFileName);
        try {
            String displayFrom = msg.getDisplayFrom();
            txtOut.println("From: " + displayFrom);
        } catch (ChunkNotFoundException e) {
        // ignore
        }
        try {
            String displayTo = msg.getDisplayTo();
            txtOut.println("To: " + displayTo);
        } catch (ChunkNotFoundException e) {
        // ignore
        }
        try {
            String displayCC = msg.getDisplayCC();
            txtOut.println("CC: " + displayCC);
        } catch (ChunkNotFoundException e) {
        // ignore
        }
        try {
            String displayBCC = msg.getDisplayBCC();
            txtOut.println("BCC: " + displayBCC);
        } catch (ChunkNotFoundException e) {
        // ignore
        }
        try {
            String subject = msg.getSubject();
            txtOut.println("Subject: " + subject);
        } catch (ChunkNotFoundException e) {
        // ignore
        }
        try {
            String body = msg.getTextBody();
            txtOut.println(body);
        } catch (ChunkNotFoundException e) {
            System.err.println("No message body");
        }
        AttachmentChunks[] attachments = msg.getAttachmentFiles();
        if (attachments.length > 0) {
            File d = new File(attDirName);
            if (d.mkdir()) {
                for (AttachmentChunks attachment : attachments) {
                    processAttachment(attachment, d);
                }
            } else {
                System.err.println("Can't create directory " + attDirName);
            }
        }
    } finally {
        if (txtOut != null) {
            txtOut.close();
        }
    }
}
Also used : ChunkNotFoundException(org.apache.poi.hsmf.exceptions.ChunkNotFoundException) AttachmentChunks(org.apache.poi.hsmf.datatypes.AttachmentChunks) File(java.io.File) PrintWriter(java.io.PrintWriter)

Example 12 with AttachmentChunks

use of org.apache.poi.hsmf.datatypes.AttachmentChunks in project poi by apache.

the class HSMFFileHandler method handleFile.

@Override
public void handleFile(InputStream stream, String path) throws Exception {
    MAPIMessage mapi = new MAPIMessage(stream);
    assertNotNull(mapi.getAttachmentFiles());
    assertNotNull(mapi.getDisplayBCC());
    assertNotNull(mapi.getMessageDate());
    AttachmentChunks[] attachments = mapi.getAttachmentFiles();
    for (AttachmentChunks attachment : attachments) {
        DirectoryChunk chunkDirectory = attachment.getAttachmentDirectory();
        if (chunkDirectory != null) {
            MAPIMessage attachmentMSG = chunkDirectory.getAsEmbededMessage();
            assertNotNull(attachmentMSG);
            String body = attachmentMSG.getTextBody();
            assertNotNull(body);
        }
    }
    /* => Writing isn't yet supported...
		// write out the file
		File file = TempFile.createTempFile("StressTest", ".msg");
		writeToFile(mapi, file);
		
		MAPIMessage read = new MAPIMessage(file.getAbsolutePath());
		assertNotNull(read.getAttachmentFiles());
		assertNotNull(read.getDisplayBCC());
		assertNotNull(read.getMessageDate());
		*/
    // writing is not yet supported... handlePOIDocument(mapi);
    mapi.close();
}
Also used : MAPIMessage(org.apache.poi.hsmf.MAPIMessage) DirectoryChunk(org.apache.poi.hsmf.datatypes.DirectoryChunk) AttachmentChunks(org.apache.poi.hsmf.datatypes.AttachmentChunks)

Example 13 with AttachmentChunks

use of org.apache.poi.hsmf.datatypes.AttachmentChunks in project poi by apache.

the class OutlookTextExtactor method getText.

/**
    * Outputs something a little like a RFC822 email
    */
public String getText() {
    MAPIMessage msg = (MAPIMessage) document;
    StringBuffer s = new StringBuffer();
    // See if we can get a suitable encoding for any
    //  non unicode text in the file
    msg.guess7BitEncoding();
    // Off we go
    StringsIterator emails;
    try {
        emails = new StringsIterator(msg.getRecipientEmailAddressList());
    } catch (ChunkNotFoundException e) {
        emails = new StringsIterator(new String[0]);
    }
    try {
        s.append("From: " + msg.getDisplayFrom() + "\n");
    } catch (ChunkNotFoundException e) {
    }
    //  people in To + CC + BCC.
    try {
        handleEmails(s, "To", msg.getDisplayTo(), emails);
    } catch (ChunkNotFoundException e) {
    }
    try {
        handleEmails(s, "CC", msg.getDisplayCC(), emails);
    } catch (ChunkNotFoundException e) {
    }
    try {
        handleEmails(s, "BCC", msg.getDisplayBCC(), emails);
    } catch (ChunkNotFoundException e) {
    }
    // Date - try two ways to find it
    try {
        // First try via the proper chunk
        SimpleDateFormat f = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss Z", Locale.ROOT);
        f.setTimeZone(LocaleUtil.getUserTimeZone());
        s.append("Date: " + f.format(msg.getMessageDate().getTime()) + "\n");
    } catch (ChunkNotFoundException e) {
        try {
            // Failing that try via the raw headers 
            String[] headers = msg.getHeaders();
            for (String header : headers) {
                if (startsWithIgnoreCase(header, "date:")) {
                    s.append("Date:" + header.substring(header.indexOf(':') + 1) + "\n");
                    break;
                }
            }
        } catch (ChunkNotFoundException he) {
        // We can't find the date, sorry...
        }
    }
    try {
        s.append("Subject: " + msg.getSubject() + "\n");
    } catch (ChunkNotFoundException e) {
    }
    // To get the attachments, use ExtractorFactory
    for (AttachmentChunks att : msg.getAttachmentFiles()) {
        StringChunk name = att.getAttachLongFileName();
        if (name == null)
            name = att.getAttachFileName();
        String attName = name == null ? null : name.getValue();
        if (att.getAttachMimeTag() != null && att.getAttachMimeTag().getValue() != null) {
            attName = att.getAttachMimeTag().getValue() + " = " + attName;
        }
        s.append("Attachment: " + attName + "\n");
    }
    try {
        s.append("\n" + msg.getTextBody() + "\n");
    } catch (ChunkNotFoundException e) {
    }
    return s.toString();
}
Also used : ChunkNotFoundException(org.apache.poi.hsmf.exceptions.ChunkNotFoundException) MAPIMessage(org.apache.poi.hsmf.MAPIMessage) SimpleDateFormat(java.text.SimpleDateFormat) AttachmentChunks(org.apache.poi.hsmf.datatypes.AttachmentChunks) StringsIterator(org.apache.poi.util.StringUtil.StringsIterator) StringChunk(org.apache.poi.hsmf.datatypes.StringChunk)

Example 14 with AttachmentChunks

use of org.apache.poi.hsmf.datatypes.AttachmentChunks in project poi by apache.

the class TestFileWithAttachmentsRead method testReadMsgAttachments.

/**
     * Test that we can handle both PDF and MSG attachments
     */
@Test
public void testReadMsgAttachments() throws Exception {
    AttachmentChunks[] attachments = pdfMsgAttachments.getAttachmentFiles();
    assertEquals(2, attachments.length);
    AttachmentChunks attachment;
    // Second is a PDF
    attachment = pdfMsgAttachments.getAttachmentFiles()[1];
    assertEquals("smbprn~1.pdf", attachment.getAttachFileName().getValue());
    assertEquals("smbprn.00009008.KdcPjl.pdf", attachment.getAttachLongFileName().getValue());
    assertEquals(".pdf", attachment.getAttachExtension().getValue());
    assertNull(attachment.getAttachMimeTag());
    assertNull(attachment.getAttachmentDirectory());
    //or compare the hashes of the attachment data
    assertEquals(13539, attachment.getAttachData().getValue().length);
    // First in a nested message
    attachment = pdfMsgAttachments.getAttachmentFiles()[0];
    assertEquals("Test Attachment", attachment.getAttachFileName().getValue());
    assertNull(attachment.getAttachLongFileName());
    assertNull(attachment.getAttachExtension());
    assertNull(attachment.getAttachMimeTag());
    assertNull(attachment.getAttachData());
    assertNotNull(attachment.getAttachmentDirectory());
    // Check we can see some bits of it
    MAPIMessage nested = attachment.getAttachmentDirectory().getAsEmbededMessage();
    assertEquals(1, nested.getRecipientNamesList().length);
    assertEquals("Nick Booth", nested.getRecipientNames());
    assertEquals("Test Attachment", nested.getConversationTopic());
}
Also used : AttachmentChunks(org.apache.poi.hsmf.datatypes.AttachmentChunks) Test(org.junit.Test)

Aggregations

AttachmentChunks (org.apache.poi.hsmf.datatypes.AttachmentChunks)14 MAPIMessage (org.apache.poi.hsmf.MAPIMessage)7 Test (org.junit.Test)5 ChunkNotFoundException (org.apache.poi.hsmf.exceptions.ChunkNotFoundException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ChunkGroup (org.apache.poi.hsmf.datatypes.ChunkGroup)3 Chunks (org.apache.poi.hsmf.datatypes.Chunks)3 NameIdChunks (org.apache.poi.hsmf.datatypes.NameIdChunks)3 RecipientChunks (org.apache.poi.hsmf.datatypes.RecipientChunks)3 Entry (org.apache.poi.poifs.filesystem.Entry)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 StringChunk (org.apache.poi.hsmf.datatypes.StringChunk)2 OutlookTextExtactor (org.apache.poi.hsmf.extractor.OutlookTextExtactor)2 WordExtractor (org.apache.poi.hwpf.extractor.WordExtractor)2 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)2 DirectoryNode (org.apache.poi.poifs.filesystem.DirectoryNode)2 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)2