none
EWS: ServiceXmlSerializationException when updating Mail body text (invalid value) RRS feed

  • Question

  • Hi,

    I'm looping through the messages of an Exchange 2010 mail box using EWS in order to insert a piece of text at the beginning of each mail message. 

    So I do something like this:

    EmailMessage.Body.Text = "MyText" + EmailMessage.Body.Text;
    EmailMessage.Update(ConflictResolutionMode.AlwaysOverwrite);

    This works fine for most of the items, but for some mail items I get the following exception:

    An error has been occured Microsoft.Exchange.WebServices.Data.ServiceXmlSerializationException: The invalid value '{whole mail body stands here}' was specified for the 'Body' element. ---> System.ArgumentException: '', hexadecimal value 0x1F, is an invalid character.

       at System.Xml.XmlUtf8RawTextWriter.InvalidXmlChar(Int32 ch, Byte* pDst, Boolean entitize)
       at System.Xml.XmlUtf8RawTextWriter.WriteElementTextBlock(Char* pSrc, Char* pSrcEnd)
       at System.Xml.XmlUtf8RawTextWriter.WriteString(String text)
       at System.Xml.XmlUtf8RawTextWriterIndent.WriteString(String text)
       at System.Xml.XmlRawWriter.WriteValue(String value)
       at System.Xml.XmlWellFormedWriter.WriteValue(String value)
       at Microsoft.Exchange.WebServices.Data.EwsServiceXmlWriter.WriteValue(String value, String name)
       --- End of inner exception stack trace ---
       at Microsoft.Exchange.WebServices.Data.EwsServiceXmlWriter.WriteValue(String value, String name)
       at Microsoft.Exchange.WebServices.Data.MessageBody.WriteElementsToXml(EwsServiceXmlWriter writer)
       at Microsoft.Exchange.WebServices.Data.ComplexProperty.WriteToXml(EwsServiceXmlWriter writer, XmlNamespace xmlNamespace, String xmlElementName)
       at Microsoft.Exchange.WebServices.Data.ComplexProperty.WriteToXml(EwsServiceXmlWriter writer, String xmlElementName)
       at Microsoft.Exchange.WebServices.Data.ComplexPropertyDefinitionBase.WritePropertyValueToXml(EwsServiceXmlWriter writer, PropertyBag propertyBag, Boolean isUpdateOperation)
       at Microsoft.Exchange.WebServices.Data.PropertyBag.WriteSetUpdateToXml(EwsServiceXmlWriter writer, PropertyDefinition propertyDefinition)
       at Microsoft.Exchange.WebServices.Data.PropertyBag.WriteToXmlForUpdate(EwsServiceXmlWriter writer)
       at Microsoft.Exchange.WebServices.Data.UpdateItemRequest.WriteElementsToXml(EwsServiceXmlWriter writer)
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.WriteBodyToXml(EwsServiceXmlWriter writer)
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.WriteToXml(EwsServiceXmlWriter writer)
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.Emit(HttpWebRequest& request)
       at Microsoft.Exchange.WebServices.Data.ServiceRequestBase.ValidateAndEmitRequest(HttpWebRequest& request)
       at Microsoft.Exchange.WebServices.Data.SimpleServiceRequestBase.InternalExecute()
       at Microsoft.Exchange.WebServices.Data.MultiResponseServiceRequest`1.Execute()
       at Microsoft.Exchange.WebServices.Data.ExchangeService.InternalUpdateItems(IEnumerable`1 items, FolderId savedItemsDestinationFolderId, ConflictResolutionMode conflictResolution, Nullable`1 messageDisposition, Nullable`1 sendInvitationsOrCancellationsMode, ServiceErrorHandling errorHandling)
       at Microsoft.Exchange.WebServices.Data.ExchangeService.UpdateItem(Item item, FolderId savedItemsDestinationFolderId, ConflictResolutionMode conflictResolution, Nullable`1 messageDisposition, Nullable`1 sendInvitationsOrCancellationsMode)
       at Microsoft.Exchange.WebServices.Data.Item.InternalUpdate(FolderId parentFolderId, ConflictResolutionMode conflictResolutionMode, Nullable`1 messageDisposition, Nullable`1 sendInvitationsOrCancellationsMode)
       at Microsoft.Exchange.WebServices.Data.Item.Update(ConflictResolutionMode conflictResolutionMode)

    In the example mail item, the EmailMessage.Body.Text property contains 3 occurences of '0x1F'. Why does EWS return the mail body string in a incorrect format, which won't be storable? How to work around this? I'ved tried to install SP2 of Exchange Server and all further updates, and using the latest EWS API, but it didn't help. So even if I don't change the body text by appending my portion of custom text, and just setting the initial value, it fails.

    EmailMessage.Body.Text = EmailMessage.Body.Text; // also fails, don't change anything, and just re-set origin value
    EmailMessage.Update(ConflictResolutionMode.AlwaysOverwrite);


    Best Regards

    Andreas

    Friday, October 19, 2012 7:11 AM

Answers