0
此問題與Java郵件API和Gmail帳戶有關。如何在不下載附件文件的情況下獲取電子郵件正文
我想通過忽略附件文件來顯示gmail電子郵件中的消息部分。我的代碼在沒有附件的情況下可以正常工作,但是當涉及附件的電子郵件時,它不會提供輸出。
我只想從郵件中顯示郵件正文。
在此先感謝。
此問題與Java郵件API和Gmail帳戶有關。如何在不下載附件文件的情況下獲取電子郵件正文
我想通過忽略附件文件來顯示gmail電子郵件中的消息部分。我的代碼在沒有附件的情況下可以正常工作,但是當涉及附件的電子郵件時,它不會提供輸出。
我只想從郵件中顯示郵件正文。
在此先感謝。
這是唯一沒有嘗試調試代碼第一槍,但你可以嘗試按照original Oracle suggestion:
private boolean textIsHtml = false;
/**
* Return the primary text content of the message.
*/
private String getText(Part p) throws
MessagingException, IOException {
if (p.isMimeType("text/*")) {
String s = (String)p.getContent();
textIsHtml = p.isMimeType("text/html");
return s;
}
if (p.isMimeType("multipart/alternative")) {
// prefer html text over plain text
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")) {
if (text == null)
text = getText(bp);
continue;
} else if (bp.isMimeType("text/html")) {
String s = getText(bp);
if (s != null)
return s;
} else {
return getText(bp);
}
}
return text;
} else if (p.isMimeType("multipart/*")) {
Multipart mp = (Multipart)p.getContent();
for (int i = 0; i < mp.getCount(); i++) {
String s = getText(mp.getBodyPart(i));
if (s != null)
return s;
}
}
return null;
}
我覺得這是你試圖讓多問題,內容從它。 上述工作的代碼僅當:
可以調用的getText方法與消息對象(其是 部分)。
我在哪裏添加此代碼在我現有的代碼?我試圖添加它,但得到編譯時錯誤? –
添加您的代碼,其工作沒有附件,如果您有附件的情況下有任何日誌,也可以添加它。 – Hrabosch
這裏你去http://pastebin.com/vtKcas0K –
好吧,問題部分來自第76行,是不是?你嘗試調試它嗎?你知道那裏發生了什麼嗎?我會嘗試,但這些信息應該由您在您的問題中提供;)當我有更多空閒時間時,我會嘗試調試它。 – Hrabosch