Parse error when retrieve email connect to EWS API (Exchange 2016) using ews-java-api 2.0 in java platform (Character reference "&#x1" is an invalid XML character)

Wai 5 Reputation points
2025-11-19T08:19:13.52+00:00

I encountered an problem when retrieving an email message when connect to EWS API exposed by Exchange Server using ews-java-api 2.0, it fails to parse invalid character and fail to get email eventually.

Is there any work around/ solution when hit this kind of parse error?

And is there upgrade of ews-java-api version to solve?

Stack-trace below:

microsoft.exchange.webservices.data.core.exception.service.remote.ServiceRequestException: The request failed. ParseError at [row,col]:[37,25]

Exchange | Exchange Server | Development
{count} vote

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2025-11-19T08:19:34.5066667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    When encountering a parse error due to invalid XML characters while using the ews-java-api to connect to Exchange Server, there are a few potential workarounds you can consider:

    1. Character Filtering: Before sending the request to the EWS API, you can preprocess the email content to filter out or replace any invalid XML characters. This can help ensure that the XML being sent is well-formed.
    2. Error Handling: Implement robust error handling to catch ServiceRequestException and log the details. This can help you identify specific cases where invalid characters are causing issues.
    3. Upgrade ews-java-api: Check if there is a newer version of the ews-java-api available. Newer versions may have bug fixes or improvements that handle invalid characters more gracefully. As of the last known information, ews-java-api 2.0 is the version you are using, so looking for a more recent release could be beneficial.
    4. Contact Support: If the issue persists, consider reaching out to Microsoft support or the community forums for more tailored assistance, especially if this is a known issue with a specific version of Exchange or the API.

    Unfortunately, the context provided does not include specific details about the ews-java-api versions or known issues related to invalid XML characters, so checking the official documentation or release notes for the library may provide additional insights.


    References:

    0 comments No comments

  2. Steven-N 15,075 Reputation points Microsoft External Staff Moderator
    2025-11-19T09:25:37.0833333+00:00

    Hi Wai

    Thank you for reaching out to Microsoft Q&A forum

    Based on your description and the information stated in https://github.com/OfficeDev/ews-java-api/issues/440, it seems like the ParseError at [row,col] typically happens when the EWS response contains invalid XML characters. The ews-java-api library uses StAX for XML parsing, which is strict about XML compliance. So, if the email body or headers contain such characters, parsing fails and the message cannot be loaded.

    Therefore, you should consider workaround approach below:

    Inspect the raw XML response by enabling tracing in your code:

    service.setTraceEnabled(true);service.setTraceFlags(EnumSet.of(TraceFlags.EwsRequest, TraceFlags.EwsResponse));
    

    Sanitize the response before parsing by intercepting the HTTP response stream and removing invalid XML characters

    Retrieve MIME content instead of the text body by using:

    PropertySet props = new PropertySet(BasePropertySet.FirstClassProperties, ItemSchema.MimeContent);EmailMessage message = EmailMessage.bind(service, item.getId(), props);
    

    Consider migrating to Microsoft Graph API for long-term stability. Graph uses JSON instead of XML, you can read here for more insight: https://www.geeksforgeeks.org/java/getting-data-from-microsoft-exchange-server-in-java/

    Note: Microsoft is providing this information as a convenience to you. These sites are not controlled by Microsoft, and Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. Please ensure that you fully understand the risks before using any suggestions from the above link.
    Hope this clears things up, let me know if you have more details


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".     

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.