StringBuffer resultText = new StringBuffer();
StringBuffer keyText = new StringBuffer();
try {
MimeMessage message = new MimeMessage(session, mimeInput);
resultText.append("Subject: " + message.getSubject()).append("\n");
if (message.getSentDate() != null) {
resultText.append("Sent: " + simpleFormat.format(message.getSentDate())).append("\n");
}
if (message.getReceivedDate() != null) {
resultText.append("Received: " + simpleFormat.format(message.getReceivedDate())).append("\n");
}
Address[] recipientsArray = null;
try {
recipientsArray = message.getAllRecipients();
} catch (AddressException ae) {
// KJB: an issue with getAllRecipients if To: contains a semi-colon rather than comma.
recipientsArray = fixAddress(ae, message, "To");
}
if (recipientsArray != null && recipientsArray.length > 0) {
String recipients = "";
for (int i = 0; i < recipientsArray.length; i++) {
recipients += recipientsArray[i].toString() + ", ";
keyText.append(stripNoneWordChars(recipientsArray[i].toString()) + " ");
}
recipients = recipients.substring(0, recipients.length() - 2);
resultText.append("Recipient(s): " + recipients).append("\n");
}
Address[] repliesArray = null;
try {
repliesArray = message.getReplyTo();
} catch (AddressException ae) {
repliesArray = fixAddress(ae, message, "Reply-to");
}
if (repliesArray != null && repliesArray.length > 0) {
String replies = "";
for (int i = 0; i < repliesArray.length; i++) {
replies += repliesArray[i].toString() + ", ";
keyText.append(stripNoneWordChars(repliesArray[i].toString()) + " ");
}
replies = replies.substring(0, replies.length() - 2);
resultText.append("Reply to: " + replies).append("\n");
}
Address[] senderArray = null;
try {
senderArray = message.getFrom();
} catch (AddressException ae) {
senderArray = fixAddress(ae, message, "From");
}
if (senderArray != null && senderArray.length > 0) {
String sender = "";
for (int i = 0; i < senderArray.length; i++) {
sender += senderArray[i].toString() + ", ";
keyText.append(stripNoneWordChars(senderArray[i].toString()) + " ");
}
sender = sender.substring(0, sender.length() - 2);
setTitle(message.getSubject() + " from " + sender);
resultText.append("Sender: " + sender).append("\n");
}
resultText.append("Header key words: " + keyText.toString()).append("\n");
resultText.append("-------------------------------------------------------------").append("\n");
// multipart or not multipart
if (message.getContent() instanceof Multipart) {
//contentType = "multipart";
Multipart mp = (Multipart) message.getContent();
for (int i = 0; i < mp.getCount(); i++) {
BodyPart bp = mp.getBodyPart(i);
String disposition = bp.getDisposition();
if ((disposition != null) && ((disposition.equals(Part.ATTACHMENT)))) {
attachments.add("attachment: " + bp.getFileName());
if (bp.isMimeType("text/*")) {
textParts.add((String) bp.getContent());
mLog.debug("added txt from attachment: " + bp.getFileName() + " : " + bp.getContentType());
}
} else if (disposition == null || disposition.equals(Part.INLINE)) {
// Plain Text oder HTML
if (bp.isMimeType("text/*")) {
textParts.add((String) bp.getContent());
} else if (bp.isMimeType("multipart/*")) {
// another bodypart container
// we process only depth = 1 and not deeper and only text.
Multipart mpInner = (Multipart) bp.getContent();
for (int k = 0; k < mpInner.getCount(); k++) {
BodyPart bpInner = mpInner.getBodyPart(k);
if (bpInner != null && (bpInner.getDisposition() == null ||
bpInner.getDisposition().equals(Part.INLINE))) {
if (bpInner.isMimeType("text/*")) {
textParts.add((String) bpInner.getContent());
}
} // end of bodypart which are not attachments
} // end of iterate over all inner bodyparts
} // MultipartContainer in a MultipartContainer
else if (bp.isMimeType("message/*")) {
Object bpContent = bp.getContent();
if (bpContent instanceof MimeMessage) {
// Added by KJB.
// Message container.
MimeMessage mmInner = (MimeMessage) bp.getContent();
// tack on the headers.
for (Enumeration<String> e = mmInner.getAllHeaderLines(); e.hasMoreElements();) {
textParts.add(e.nextElement());
}
Object mmContent = mmInner.getContent();
if (mmContent instanceof String) {
textParts.add((String) mmContent);
} else if (mmContent instanceof InputStream) {
textParts.add(RegainToolkit.readStringFromStream((InputStream) mmContent));
} else {