0

我有一些問題,在使用EWS的Java API 1.3快照得到一些附件,我想附件在我的電子郵件,在這裏我的代碼:EWS的Java API獲取附件

try { 
    ExchangeService service; 
    service.setUrl(new URI("https://" + myserver + "/ews/Exchange.asmx")); 
    ExchangeCredentials credentials = new WebCredentials(username, password); 
    service.setCredentials(credentials); 
    ItemView view = new ItemView(Integer.MAX_VALUE); 
    view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Ascending); 
    Folder folder = Folder.bind(service, WellKnownFolderName.Inbox); 
    FindItemsResults<Item> results = service.findItems(folder.getId(),view); 
    service.loadPropertiesForItems(results, new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments)); 

     for (Item item : results) { 
     Item itm = Item.bind(service, item.getId(), new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments)); 
     EmailMessage emailMessage = EmailMessage.bind(service, itm.getId(), new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments)); 
     if (emailMessage.getHasAttachments()) { 
       for (Attachment attachment : emailMessage.getAttachments()) { 
        String FileExtension = getFileExtension(attachment.getName()); 
        File TempFile = File.createTempFile(attachment.getName(), FileExtension); 
        attachment.load(TempFile.getAbsolutePath()); 
       } 
      } 
     } 
} catch (Exception e) { 
    logger.error("Error ", e.getMessage()); 
} 

我的問題是可以得到另一有沒有附件,總是跳過帶有附件的電子郵件電子郵件的例子是這樣的, 在我的收件箱我從這個郵件列表

當我跑我的代碼,它總能得到一個沒有電子郵件附件,這樣的:

並跳過有附件的其他電子郵件,我不知道如何發生這種情況。有人能幫助我嗎?

+0

您正在使用哪個java api?鏈接? – Mahesha999 2015-02-12 08:47:57

+0

我使用EWS的Java API 1.3快照, 我從Maven倉庫在這裏下載和我的pom.xml com.microsoft.office EWS-Java的API 系統 1.3快照 2015-02-13 09:17:50

+0

看起來是正確的,可能會捕獲SOAP包與TraceListener將有所幫助。這裏有一些鏈接:[1](https://msdn.microsoft.com/en-us/library/office/dd633676(v = exchg.80).aspx),[2](https://msdn.microsoft .com/en-us/library/office/dd633655(v = exchg.80).aspx),[3](https://msdn.microsoft.com/en-us/library/office/dn495632(v = exchg) .150).aspx)另外我會建議你在[exchange development forum](https://social.msdn.microsoft.com/Forums/office/en-US/home?forum=exchangesvrdevelopment)上發佈這個問題以獲得精確響應。當我面對類似奇怪的事情時,這幫助了我。 – Mahesha999 2015-02-13 10:14:02

回答

0
HashMap<String, HashMap<String, String>> attachments = new HashMap<String, HashMap<String, String>>();  

if (emailMessage.getHasAttachments() || emailMessage.getAttachments().getItems().size() > 0) { 

     //get all the attachments 
     AttachmentCollection attachmentsCol = emailMessage.getAttachments(); 

     log.info("File Count: " +attachmentsCol.getCount()); 

     //loop over the attachments 
     for (int i = 0; i < attachmentsCol.getCount(); i++) { 
      Attachment attachment = attachmentsCol.getPropertyAtIndex(i); 
      //log.debug("Starting to process attachment "+ attachment.getName()); 

       //FileAttachment - Represents a file that is attached to an email item 
       if (attachment instanceof FileAttachment || attachment.getIsInline()) { 

        attachments.putAll(extractFileAttachments(attachment, properties)); 

       } else if (attachment instanceof ItemAttachment) { //ItemAttachment - Represents an Exchange item that is attached to another Exchange item. 

        attachments.putAll(extractItemAttachments(service, attachment, properties, appendedBody)); 
       } 
      } 
     } 
    } else { 
     log.debug("Email message does not have any attachments."); 
    } 


//Extract File Attachments 
try { 
    FileAttachment fileAttachment = (FileAttachment) attachment; 
    // if we don't call this, the Content property may be null. 
    fileAttachment.load(); 

    //extract the attachment content, it's not base64 encoded. 
    attachmentContent = fileAttachment.getContent(); 

    if (attachmentContent != null && attachmentContent.length > 0) { 

     //check the size 
     int attachmentSize = attachmentContent.length; 

     //check if the attachment is valid 
     ValidateEmail.validateAttachment(fileAttachment, properties, 
       emailIdentifier, attachmentSize); 

     fileAttachments.put(UtilConstants.ATTACHMENT_SIZE, String.valueOf(attachmentSize)); 

     //get attachment name 
     String fileName = fileAttachment.getName(); 
     fileAttachments.put(UtilConstants.ATTACHMENT_NAME, fileName); 

     String mimeType = fileAttachment.getContentType(); 
     fileAttachments.put(UtilConstants.ATTACHMENT_MIME_TYPE, mimeType); 

     log.info("File Name: " + fileName + " File Size: " + attachmentSize); 


     if (attachmentContent != null && attachmentContent.length > 0) { 
      //convert the content to base64 encoded string and add to the collection. 
      String base64Encoded = UtilFunctions.encodeToBase64(attachmentContent); 
      fileAttachments.put(UtilConstants.ATTACHMENT_CONTENT, base64Encoded); 
     } 



//Extract Item Attachment 
try { 
    ItemAttachment itemAttachment = (ItemAttachment) attachment; 

    PropertySet propertySet = new PropertySet(
      BasePropertySet.FirstClassProperties, ItemSchema.Attachments, 
      ItemSchema.Body, ItemSchema.Id, ItemSchema.DateTimeReceived, 
      EmailMessageSchema.DateTimeReceived, EmailMessageSchema.Body); 

    itemAttachment.load(); 
    propertySet.setRequestedBodyType(BodyType.Text); 

    Item item = itemAttachment.getItem(); 

    eBody = appendItemBody(item, appendedBody.get(UtilConstants.BODY_CONTENT)); 

    appendedBody.put(UtilConstants.BODY_CONTENT, eBody); 

    /* 
    * We need to check if Item attachment has further more 
    * attachments like .msg attachment, which is an outlook email 
    * as attachment. Yes, we can attach an email chain as 
    * attachment and that email chain can have multiple 
    * attachments. 
    */ 
    AttachmentCollection childAttachments = item.getAttachments(); 
    //check if not empty collection. move on 
    if (childAttachments != null && !childAttachments.getItems().isEmpty() && childAttachments.getCount() > 0) { 

     for (Attachment childAttachment : childAttachments) { 

      if (childAttachment instanceof FileAttachment) { 

       itemAttachments.putAll(extractFileAttachments(childAttachment, properties, emailIdentifier)); 

      } else if (childAttachment instanceof ItemAttachment) { 

       itemAttachments = extractItemAttachments(service, childAttachment, properties, appendedBody, emailIdentifier); 
      } 
     } 
    } 
} catch (Exception e) { 
    throw new Exception("Exception while extracting Item Attachments: " + e.getMessage()); 
}