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();
}
}
}
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();
}
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();
}
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());
}
Aggregations