无法使用EWS下载ItemAttachment

时间:2014-10-06 18:48:16

标签: java exchangewebservices

我在使用EWS下载“ItemAttachment”类型的附件时遇到问题。 以下是我用来下载附件的代码:

 PropertySet ps = new PropertySet(BasePropertySet.FirstClassProperties);
                    ps.add(ItemSchema.MimeContent);
                    ia = (ItemAttachment) attach;
                    ia.load(ps);
                    //ia.load();
                    System.out.println(ia.getItem().getSubject());
                    MimeContent mc = ia.getItem().getMimeContent();
                    String itemName = ia.getName().replace(" ","").replace(":","-").trim();
                    attname.add(itemName);
                    System.out.println(itemName);
                    byte[] contentBytes = mc.getContent();
                    theStream = new FileOutputStream(
                            "C:\\Users\\502000317\\Desktop\\test\\"
                                    + itemName);
                    ia.getItem().getAttachments();
                    theStream.write(contentBytes);
                    theStream.flush();

但是当我尝试执行此操作时,我得到了以下错误:

java.lang.ClassCastException:microsoft.exchange.webservices.data.PropertySet无法强制转换为microsoft.exchange.webservices.data.PropertyDefinitionBase     at microsoft.exchange.webservices.data.PropertySet.writeAdditionalPropertiesToXml(Unknown Source)     at microsoft.exchange.webservices.data.GetAttachmentRequest.writeElementsToXml(Unknown Source)     at microsoft.exchange.webservices.data.ServiceRequestBase.writeBodyToXml(Unknown Source)     at microsoft.exchange.webservices.data.ServiceRequestBase.writeToXml(Unknown Source)     at microsoft.exchange.webservices.data.ServiceRequestBase.buildEwsHttpWebRequest(Unknown Source)     at microsoft.exchange.webservices.data.ServiceRequestBase.validateAndEmitRequest(Unknown Source)     at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(Unknown Source)     at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(Unknown Source)     at microsoft.exchange.webservices.data.ExchangeService.internalGetAttachments(Unknown Source)     at microsoft.exchange.webservices.data.ExchangeService.getAttachment(Unknown Source)     at microsoft.exchange.webservices.data.Attachment.internalLoad(Unknown Source)     at microsoft.exchange.webservices.data.ItemAttachment.load(Unknown Source)     在com.medpro.roundrobinmailer.RoundRobinMailer.sendunreadmailtoRRRecpt(RoundRobinMailer.java:173)     在com.medpro.roundrobinmailer.RoundRobinMailer.delegateAccessSearchEmailWithFilter(RoundRobinMailer.java:114)     在com.medpro.roundrobinmailer.RoundRobinMailer.main(RoundRobinMailer.java:57)

代码中断:       ia.load(PS);

这让我疯了。 请帮忙。

由于 图莎尔

2 个答案:

答案 0 :(得分:1)

这是EWS Java中的已知错误。幸运的是,它已经开源,可以在Github上找到:

https://github.com/OfficeDev/ews-java-api

见问题#12。问题出在ExchangeService.internalGetAttachments()中。在那里有一些非常丑陋的代码使用了一个无法解释的泛型和无类型集合的混合。因此,当它应该复制到各个PropertyDefinitionBase对象时,它会将整个PropertySet添加到列表中,这就是您看到ClassCastException的原因。看一看。如果它还没有修好,那么你自己就不难做到。

答案 1 :(得分:0)

/ *   @author rmansink

此方法下载与电子邮件关联的所有附件

我认为你收到错误是因为在加载之前你需要绑定项目..以下是工作正常的代码...经过测试并且当前正在我的项目中使用..希望它有所帮助..祝你好运

* /

public static void getAttachments(ExchangeService service, ItemId id) throws Exception{

    Item item = Item.bind(service, id);
    item.load();
    if (item.getHasAttachments()){ 

        AttachmentCollection attachmentsCol = item.getAttachments();

        int NumberOfAttachments = attachmentsCol.getCount();

        System.out.println("Attchments being Downloaded:: "+NumberOfAttachments);
        String directoryName = "C:\\MSExchangeEmail\\WebContent\\attachments";
        File theDir = new File(directoryName);

        // if the directory does not exist, create it
        if (!theDir.exists()) {
          System.out.println("creating directory: " + directoryName);
          boolean result = false;

          try{

              result = theDir.mkdir();

           } catch(SecurityException se){
              //handle it
           }        
           if(result) {    
             System.out.println("DIR created: "+result);  
           }
        }  
        for (int i = 0; i < NumberOfAttachments; i++) { 

            FileAttachment attachment = (FileAttachment)attachmentsCol.getPropertyAtIndex(i); 
            attachment.load(directoryName+"\\"+attachment.getName());
        }
    } 

}
相关问题