1

I'm using EWS JAVA API 1.2 and I have a problem saving an ItemAttachment using this code.

if(attachmentsCol.getPropertyAtIndex(i) instanceof FileAttachment) 
{
    ...
}
else
{
    ItemAttachment attachment = (ItemAttachment)attachmentsCol.getPropertyAtIndex(i);                                          
    attachment.load();
    Item item = attachment.getItem();
    item.load(newPropertySet(ItemSchema.MimeContent));`
    MimeContent Itemmc = item.getMimeContent();
    ....
} 

item.load(....) returns this error

microsoft.exchange.webservices.data.InvalidOperationException: This operation can't be performed because this service object doesn't have an Id.

Thank you for your help.

Enrico M
  • 11
  • 2

1 Answers1

1

You can't do a Load on the ItemAttachment itself because this will try to do a GetItem request which isn't valid for Attachments. What you need to do is on the Attachment.load() include a propertyset with the Mime-content eg something like

                foreach (var item in findResults.Items)
                {
                    foreach (Attachment Attach in item.Attachments) {
                        if (Attach is ItemAttachment) {
                            PropertySet psProp = new PropertySet(BasePropertySet.FirstClassProperties);
                            psProp.Add(ItemSchema.MimeContent);
                            ((ItemAttachment)Attach).Load(psProp);
                            if (((ItemAttachment)Attach).Item.MimeContent != null)
                            {
                                System.IO.File.WriteAllBytes("c:\\temp\\file.eml", ((ItemAttachment)Attach).Item.MimeContent.Content);
                            }                               
                        }
                    }                      

Cheers Glen

Glen Scales
  • 20,495
  • 1
  • 20
  • 23
  • Thank you for your reply... `((ItemAttachment)attachment).load(new PropertySet(ItemSchema.MimeContent));` returns `java.lang.ClassCastException: microsoft.exchange.webservices.data.PropertySet cannot be cast to microsoft.exchange.webservices.data.PropertyDefinitionBase` seems that the JAVA API behaves different than C#. – Enrico M Oct 03 '14 at 15:29
  • 1
    That's likely a known bug in EWS Java. Fortunately, EWS Java was recently open-sourced and is available on Github: https://github.com/OfficeDev/ews-java-api That particular bug occurs in ExchangeService.internalGetAttachments(), issue #12 in the Issues section. You'll know it when you see it in the code by all the compiler warnings. A fix should be in the repo soon, but it's not difficult to fix yourself if you take a look. – user1017413 Oct 03 '14 at 21:08
  • Thank you for both advice. It fixed my problem. – Enrico M Oct 06 '14 at 09:44