I am trying to read email body from outlook with following java code. How do I read properly if the email contains table? Code used is
// Methods to get Email message content.
private String getTextFromMessage(Message message) throws Exception {
String result = "";
if (message.isMimeType("text/plain")) {
result = message.getContent().toString();
} else if (message.isMimeType("multipart/*")) {
MimeMultipart mimeMultipart = (MimeMultipart)message.getContent();
result = getTextFromMimeMultipart(mimeMultipart);
}
result = removeHyperTextContent(result);
return result;
}
private String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws Exception
{
String result = "";
int count = mimeMultipart.getCount();
for (int i = 0; i < count; i++) {
BodyPart bodyPart = mimeMultipart.getBodyPart(i);
if (bodyPart.isMimeType("text/plain")) {
result = result + "\n" + bodyPart. getContent();
break; }
if (bodyPart.isMimeType("text/html")) {
String html = (String)bodyPart.getContent();
result = result + "\n" + html;
} else if ((bodyPart.getContent() instanceof MimeMultipart)) {
result = result + getTextFromMimeMultipart((MimeMultipart)bodyPart.getContent());
break; //This is added to break after recursive call, otherwise it fetches the content from next MultiPart.
}
}
result = removeHyperTextContent(result);
return result;
}
First, this JavaMail FAQ entry will help you find the main message body in an email message.
In your code, you're preferring the text/plain content over the text/html content. Tables may not be formatted nicely in the text/plain content, but it depends on the mailer being used. The text/html content will have all the information about the table, but you'll need something that parses html to be able to extract the table information in a form you can use.
It's not clear to me what you want to do with the table once you extract it. If you're displaying the result as html in a browser, having the original html table may be exactly what you need.