OfficeDev / ews-java-api

A java client library to access Exchange web services. The API works against Office 365 Exchange Online as well as on premises Exchange.
MIT License
867 stars 557 forks source link

When using createItem attachments are not sent to the Exchange Server #741

Open akainz opened 3 years ago

akainz commented 3 years ago

https://github.com/OfficeDev/ews-java-api/blob/33235f37d97871f17c496526aad0b8c458f904c3/src/main/java/microsoft/exchange/webservices/data/property/definition/AttachmentsPropertyDefinition.java#L69

in the AttachmentsPropertyDefinition the version is checked using: if (version != null && this.getVersion() .compareTo(ExchangeVersion.Exchange2010_SP2) >= 0) {

but this getVersion() is fixed in the constructor as ExchangeVersion.Exchange2007_SP1. This will always be <0 as its comparing to constant values. I'm thinking it was meant to read as if (version != null && version .compareTo(ExchangeVersion.Exchange2010_SP2) >= 0) {

which would mean that the flags are set according to the requested exchange version.

This results in the attachments never being sent to the server on create item calls.

As a temporary workaround, the version in AttachmentsPropertyDefinition can be increased to 2010_SP2.

            Field f= PropertyDefinition.class.getDeclaredField("version");
            f.setAccessible(true);
            f.set(ItemSchema.Attachments, ExchangeVersion.Exchange2010_SP2);

But the EWS API will fail the seemingly correct Exchange reponse:

This response <?xml version="1.0" encoding="utf-8"?>

</s:Header>
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <m:CreateItemResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
        <m:ResponseMessages>
            <m:CreateItemResponseMessage ResponseClass="Success">
                <m:ResponseCode>NoError</m:ResponseCode>
                <m:Items>
                    <t:Message>
                        <t:ItemId Id="AAIARgAAAAAAGkRzkKpmEc2byACqAC/EWgkAe6oKo+LA9ECbKfaUffWjvgAABm/kwQAAe6oKo+LA9ECbKfaUffWjvgAAMBwnGAAALgAAAAAAGkRzkKpmEc2byACqAC/EWgMAe6oKo+LA9ECbKfaUffWjvgAABm/kwQAA" ChangeKey="CQAAABYAAAB7qgqj4sD0QJsp9pR99aO+AAAwHaHN" />
                        <t:Attachments>
                            <t:FileAttachment>
                                <t:AttachmentId Id="AAIARgAAAAAAGkRzkKpmEc2byACqAC/EWgkAe6oKo+LA9ECbKfaUffWjvgAABm/kwQAAe6oKo+LA9ECbKfaUffWjvgAAMBwnGAAALgAAAAAAGkRzkKpmEc2byACqAC/EWgMAe6oKo+LA9ECbKfaUffWjvgAABm/kwQAAARIAEAAH5iHwypeURr4iFJHvbMRz" />
                            </t:FileAttachment>
                        </t:Attachments>
                    </t:Message>
                </m:Items>
            </m:CreateItemResponseMessage>
        </m:ResponseMessages>
    </m:CreateItemResponse>
</s:Body>

</s:Envelope>

will cause this error

Caused by: microsoft.exchange.webservices.data.core.exception.service.local.ServiceLocalException: Property type incompatible when updating collection. at microsoft.exchange.webservices.data.property.complex.ComplexPropertyCollection.updateFromXml(ComplexPropertyCollection.java:189) at microsoft.exchange.webservices.data.property.complex.ComplexProperty.updateFromXml(ComplexProperty.java:222) at microsoft.exchange.webservices.data.property.definition.ComplexPropertyDefinitionBase.internalLoadFromXml(ComplexPropertyDefinitionBase.java:105) at microsoft.exchange.webservices.data.property.definition.ComplexPropertyDefinitionBase.loadPropertyValueFromXml(ComplexPropertyDefinitionBase.java:150) at microsoft.exchange.webservices.data.core.PropertyBag.loadFromXml(PropertyBag.java:510) at microsoft.exchange.webservices.data.core.service.ServiceObject.loadFromXml(ServiceObject.java:298)