/**
* 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) {}
// For To, CC and BCC, try to match the names
// up with their email addresses. Relies on the
// Recipient Chunks being in the same order as
// 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");
f.setTimeZone(TimeZone.getTimeZone("UTC"));
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(header.toLowerCase().startsWith("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) {}
// Display attachment names
// To get the attachments, use ExtractorFactory
for(AttachmentChunks att : msg.getAttachmentFiles()) {
String ats = att.attachLongFileName.getValue();
if(att.attachMimeTag != null &&
att.attachMimeTag.getValue() != null) {
ats = att.attachMimeTag.getValue() + " = " + ats;
}
s.append("Attachment: " + ats + "\n");
}
try {
s.append("\n" + msg.getTextBody() + "\n");
} catch(ChunkNotFoundException e) {}
return s.toString();
}