2013-06-19 139 views
0

我目前正在使用java郵件api。我需要列出附件的詳細信息,也希望從某些電子郵件中刪除附件並將其轉發給其他人。所以我試圖找出附件ID。我該怎麼做?任何建議將不勝感激!java中電子郵件的附件ID

回答

0

這有幫助嗎?

private void getAttachments(Part p, File inputFolder, List<String> fileNames) throws Exception{ 
    String disp = p.getDisposition(); 
    if (!p.isMimeType("multipart/*")) { 
     if (disp == null || (disp != null && (disp.equalsIgnoreCase(Part.ATTACHMENT) || disp.equalsIgnoreCase(Part.INLINE)))) {    
      String fileName = p.getFileName(); 
      File opFile = new File(inputFolder, fileName); 
      ((MimeBodyPart) p).saveFile(opFile); 
      fileNames.add(fileName);      
      } 
     } 
    }else{ 
     Multipart mp = (Multipart) p.getContent(); 
     int count = mp.getCount(); 
     for (int i = 0; i < count; i++){     
      getAttachments(mp.getBodyPart(i),inputFolder, fileNames); 
     } 
    } 
} 
0

沒有任何東西作爲附件ID。你的郵件客戶端顯示爲帶有附加內容的消息,確實是一個MIME多,看起來像這樣(sample source):

From: John Doe <[email protected]> 
MIME-Version: 1.0 
Content-Type: multipart/mixed; boundary="XXXXboundary text" 

This is a multipart message in MIME format. 

--XXXXboundary text 
Content-Type: text/plain 

this is the body text 

--XXXXboundary text 
Content-Type: text/plain; 
Content-Disposition: attachment; filename="test.txt" 

this is the attachment text 

--XXXXboundary text-- 

重要注意事項:

  1. 在多每個部分都有Content-Type
  2. 任選地,可以存在Content-Dispositionheader
  3. 單份可以是本身多部分

注意,的確有Content-ID頭,但我不認爲這是你在找什麼:例如,它是在multipart/related消息中使用嵌入從text/htmlimage/* S和文本在同一封電子郵件。你必須瞭解它是如何工作的,以及它是否用於你的輸入。

我認爲你最好的選擇是檢查Content-DispositionContent-Type標題。剩下的就是猜測,沒有實際要求,人們無法幫助代碼。

+0

謝謝你們的建議 –