基本上,我编写了一个可从收件箱中读取电子邮件的应用程序。我一直使用Gmail发送的电子邮件测试应用程序。但是现在,当我试图阅读从Outlook发送的电子邮件时,我没有收到任何内容。无法使用JavaMail读取Outlook邮件,而使用Gmail工作
我登录的CONTENTTYPES同时从电子邮件: Gmail会返回:multipart/alternative; boundary=047d7b342bf2b6847f04d11df78a 展望回报:text/html; charset=iso-8859-1 注:这些是相同的电子邮件,从不同的邮件客户端刚送来。
来自Gmail的邮件将是Multipart的一个实例。虽然Outlook电子邮件将是一个字符串的实例。
我的代码:
,其检查该消息是否是一个instanceof多部分或字符串的方法。
public void getContent(Message msg) throws IOException, Exception {
Object contt = msg.getContent();
System.out.println("Contenttype: " + msg.getContentType());
if (contt instanceof Multipart) {
checkDisposition = true;
handleMultipart((Multipart) contt);
} else if (contt instanceof String) {
handlePart((Part) msg);
}
prepareEmail(mpMessage);
}
如果该消息是多部分此方法将被称为:
public void handleMultipart(Multipart multipart)
throws MessagingException, IOException, Exception {
mpMessage = getText(multipart.getBodyPart(0));
for (int z = 1, n = multipart.getCount(); z < n; z++) {
handlePart(multipart.getBodyPart(z));
}
}
如果消息不是,这将直接调用:
public void handlePart(Part part)
throws MessagingException, IOException, Exception {
Object con = messageCopy.getContent();
String disposition = part.getDisposition();
String contentType = part.getContentType();
if (checkDisposition) {
if (disposition == null) {
System.out.println("Disposition is null");
} else if (disposition.equalsIgnoreCase(Part.ATTACHMENT)) {
System.out.println("Attachment: " + part.getFileName()
+ " : " + contentType);
input = part.getInputStream();
bytes = IOUtils.toByteArray(input);
} else if (disposition.equalsIgnoreCase(Part.INLINE)) {
System.out.println("Inline: "
+ part.getFileName()
+ " : " + contentType);
} else {
System.out.println("Other: " + disposition);
}
}else{
mpMessage = part.getContent().toString(); //returns nothing
System.out.println("mpMessage handlePart "+mpMessage); //returns nothing
System.out.println("mpMessage handlePart "+part.getLineCount()); //returns 0
System.out.println("mpMessage handlePart "+part.getContentType()); //returns text/html chartset=iso-8859-1
System.out.println("mpMessage handlePart "+part.getSize()); // returns 22334
part.writeTo(System.out); //See below
}
}
的从零件返回文本的方法: 个
private String getText(Part p) throws
MessagingException, IOException {
System.out.println("getText contentType "+p.getContentType());
//This part gets called if trying to read an Outlook mail, its not clear for me how to retrieve the text from the part. Since `p.getContent()` returns nothing
if (p.isMimeType("text/*")) {
String s = (String) p.getContent();
System.out.println();
return String.valueOf(s);
}
if (p.isMimeType("multipart/alternative")) {
Multipart mp = (Multipart) p.getContent();
String text = null;
for (int i = 0; i < mp.getCount(); i++) {
Part bp = mp.getBodyPart(i);
if (bp.isMimeType("text/plain")) {
String s = getText(bp);
if (s != null) {
return s;
}
}
}
return text;
}
return null;
}
part.writeTo(System.out)回报:
接收:从AMSPRD0710HT005.eurprd07.prod.outlook.com 服务器(TLS)ID 00000; 2012年12月20日09:28:23 +0000 收到:AMSPRD0710MB354.eurprd07.prod.outlook.com([00.000.0000]) AMSPRD0710HT005.eurprd07.prod.outlook.com([00.000.0000])与地图ID 14.16.0245.002;星期四,2012年12月20日9时28分05秒+0000 来源:测试 为:支持 主题:Verwerkingsverslag Kenmerk:0824496 线程主题:Verwerkingsverslag Kenmerk:0824496 主题指数:Ac3elFC2qYsSo + SOT2ii4HnbCCqgVw == 日期:星期四,2012年12月20日10:28:05 +0100 消息ID:...
依此类推。
消息本身的内容将作为HTML代码返回,而不仅仅是普通文本。
如何从Outlook电子邮件而不是HTML代码中检索纯文本?或者我如何检索handlePart中该部分的内容?
任何帮助表示赞赏,
谢谢!
2012-12-20
Jef