2012-11-22 19 views
7

問題は次のようになります。Javaでインライン画像を含む電子メール本文をダウンロード

特定のアカウントから電子メールを読むためのコード設定があります。その部分は完全に機能します。

問題はメールメッセージの解析にあります。添付ファイルと電子メール本文(インラインイメージを含む)を区切ります。

私のコードは次のようになります:

私が手の問題は、私はこのコードを実行すると、私はイメージを示すエラー画像の符号に置き換えられるHTMLファイルが、インライン画像を得ることである
Void readMessages(Folder folder){ 

      Message[] messages = folder.getMessages(); 
      // loading of message objects. 
       for (int messageNumber = 0; messageNumber < messages.length; messageNumber++) { 

      final Message currentMessage = messages[messageNumber]; 
       logger.info("Handling the mail with subject " + currentMessage.getSubject()); 
       logger.info("Content type for the current message is " +         currentMessage.getContentType()); 
       final String messageFileName = currentMessage.getFileName(); 
       logger.info("File name for the message " + messageFileName + ". File name is blank " 
               +      StringUtils.isBlank(messageFileName)); 


         Object messageContentObject = currentMessage.getContent(); 
         if (messageContentObject instanceof Multipart) { 
          Multipart multipart = (Multipart) messageContentObject; 

          // downloading all attachments.... 
          int attachmentCount = multipart.getCount(); 
          logger.info("Number of attachments "); 
          for (int i = 0; i < attachmentCount; i++) { 
           Part part = (Part) multipart.getBodyPart(i); 
           downloadAttachment(part, folderPath.toString()); 
          } 

         } 

        } 
       } 
      } 
     private void downloadAttachment(Part part, String folderPath) throws Exception { 
    String disPosition = part.getDisposition(); 
    String fileName = part.getFileName(); 
    String decodedText = null; 
    logger.info("Disposition type :: " + disPosition); 
    logger.info("Attached File Name :: " + fileName); 

    if (disPosition != null && disPosition.equalsIgnoreCase(Part.ATTACHMENT)) { 
     logger.info("DisPosition is ATTACHMENT type."); 
     File file = new File(folderPath + File.separator + decodedText); 
     file.getParentFile().mkdirs(); 
     saveEmailAttachment(file, part); 
    } else if (fileName != null && disPosition == null) { 
     logger.info("DisPosition is Null type but file name is valid. Possibly inline attchment"); 
     File file = new File(folderPath + File.separator + decodedText); 
     file.getParentFile().mkdirs(); 
     saveEmailAttachment(file, part); 
    } else if (fileName == null && disPosition == null) { 
     logger.info("DisPosition is Null type but file name is null. It is email body."); 
     File file = new File(folderPath + File.separator + "mail.html"); 
     file.getParentFile().mkdirs(); 
     saveEmailAttachment(file, part); 
    } 


} 
    protected int saveEmailAttachment(File saveFile, Part part) throws Exception { 

    BufferedOutputStream bos = null; 
    InputStream is = null; 
    int ret = 0, count = 0; 
    try { 
     bos = new BufferedOutputStream(new FileOutputStream(saveFile)); 
     part.writeTo(new FileOutputStream(saveFile)); 

    } finally { 
     try { 
      if (bos != null) { 
       bos.close(); 
      } 
      if (is != null) { 
       is.close(); 
      } 
     } catch (IOException ioe) { 
      logger.error("Error while closing the stream.", ioe); 
     } 
    } 
    return count; 
} 

ソースはありません。

私を助けてください。さらに情報が必要な場合はお知らせください。

私も変更して.emlファイルとして身体を保存しようとした:

File file = new File(folderPath + File.separator + "mail.eml"); 

File file = new File(folderPath + File.separator + "mail.html"); 

しかし、私は同じ結果を得ました。

+0

"ソースなしの画像を示すエラー画像の記号"。 ソースは空ですか?あなたはHTMLソースをチェックしましたか? – Janoz

+0

このHTMLファイルを確認してください。 https://www.dropbox.com/s/5evk4pjq721yo8m/mail.html – GSG

+0

このHTMLには、Image source missingが表示されます。 – GSG

答えて

1

私は電子メール本文テキストをインラインイメージを含むpdfに変換するコードを以下に書いています。コード内の は、ダウンロードイメージパスでイメージコード(例:cid:[email protected])を置き換えました。画像をダウンロードしながら、画像キーとダウンロードパスの「ハッシュマップ」を構築しています。

HTMLWorker htmlWorker = new HTMLWorker(document); 
      if(bodyStr!=null) 
      { 

       //find inline images 
       inlineImages=downloadInLineImage(mostRecentMatch, dynamicOutputDirectory); 
       if(inlineImages!=null) 
       { 

        for (Map.Entry<String, String> entry : inlineImages.entrySet()) { 
         //System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue()); 
         bodyStr=bodyStr.replaceAll("cid:"+entry.getKey() , entry.getValue()); 
        } 
       } 
       htmlWorker.parse(new StringReader(bodyStr)); 

     } 

ダウンロードアイテムを渡してインラインイメージをダウンロードします。

private HashMap<String,String> downloadInLineImage(Item item, String dynamicOutputDirectory) 
     throws Exception, ServiceLocalException { 
    //create output directory if not present 

     //bind the item to a new email message. if you do not bind, then the getHasAttachments() function will fail 
    EmailMessage mostRecentMatch = (EmailMessage)item; 
    String from = mostRecentMatch.getFrom().getAddress(); 
    String user =StringUtils.substringBefore(from, "@"); 
    AttachmentCollection collection=item.getAttachments(); 

    HashMap<String,String> inlineFiles=new HashMap<String,String>(); 

    if(collection.getCount()>0) 
    { 
     for (Attachment attachment : collection.getItems()) { 

      if(attachment.getIsInline()) 
      { 

       FileAttachment currentFile = (FileAttachment) attachment; 
       String filePath=dynamicOutputDirectory+"/"+user+currentFile.getName(); 
       File file=new File(filePath); 
       FileOutputStream fio=new FileOutputStream(file); 
       currentFile.load(fio); 
       inlineFiles.put(currentFile.getContentId(), filePath); 
       fio.close(); 
      } 
     } 
    } 
1

メールにファイル名がないため、インライン画像への参照はのようなcid: URNに置き換えられます。 SOMEIDは、MultipartオブジェクトのContent-IDを参照します。

これを機能させるには、ファイルに複数パートの添付ファイル(仮名など)を保存し、cid URNを実際のファイル名で置き換える必要があります。

+0

私はこれを試してみる。 – GSG

+0

どうすればいいですか?同じ問題があります。私はインラインイメージを正常にダウンロードしましたが、返信できませんでした。src:私の元のパスを使って可能な限り早く解決してください。 –

+0

私のために働かなかった。まだ道を探しています。 – GSG

関連する問題