如何阅读带有表格 Java 代码的电子邮件?
How Read Email With Tables Java Code?
我正在尝试使用以下 java 代码从 Outlook 中读取电子邮件正文。如果电子邮件包含 table,我该如何正确阅读?
使用的代码是
// 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;
}
1. Body of original body text
2 . Out put string I got
首先,此 JavaMail 常见问题解答条目将帮助您在电子邮件中找到 main message body。
在您的代码中,您更喜欢 text/plain 内容而不是 text/html 内容。 text/plain 内容中的表格可能无法很好地格式化,但这取决于所使用的邮件程序。 text/html 内容将包含有关 table 的所有信息,但您需要一些能够解析 html 的内容,以便能够以您可以的形式提取 table 信息使用。
我不清楚解压缩后要用 table 做什么。如果您在浏览器中将结果显示为 html,那么原始的 html table 可能正是您所需要的。
我正在尝试使用以下 java 代码从 Outlook 中读取电子邮件正文。如果电子邮件包含 table,我该如何正确阅读? 使用的代码是
// 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;
}
1. Body of original body text 2 . Out put string I got
首先,此 JavaMail 常见问题解答条目将帮助您在电子邮件中找到 main message body。
在您的代码中,您更喜欢 text/plain 内容而不是 text/html 内容。 text/plain 内容中的表格可能无法很好地格式化,但这取决于所使用的邮件程序。 text/html 内容将包含有关 table 的所有信息,但您需要一些能够解析 html 的内容,以便能够以您可以的形式提取 table 信息使用。
我不清楚解压缩后要用 table 做什么。如果您在浏览器中将结果显示为 html,那么原始的 html table 可能正是您所需要的。