Search code examples
c#exchange-serverexchangewebservices

EWS Managed API Getting Error "DTD dtd is prohibited in this xml document"


While searching for allitems folder I am getting "dtd is prohibited in this xml document" error. Below is the sample code that I am executing

        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);

        string exchangeURL = "ExchangeURL";

        Uri myUri = new Uri(exchangeURL);
        service.TraceEnabled = true;
        service.Url = myUri;
        service.UseDefaultCredentials = true;
        service.Credentials = new WebCredentials("email", "password");
        //service.AutodiscoverUrl(empMail.PrimaryEmail);
        service.ImpersonatedUserId = new ImpersonatedUserId(ConnectingIdType.SmtpAddress, empMail.EmployeeEmail);

        //Certificate validation handling
        ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
        {
            return true;
        };


                EmailMessage message;
                PropertySet propertySet;

                Mailbox mailBox = new Mailbox("Email");
                ItemView itemView = new ItemView(1);
                itemView.Traversal = ItemTraversal.Shallow;

                //Try to get 'Allitems' folder so that we could search items in all folders
                FolderId fid = new FolderId(WellKnownFolderName.Root);
                FolderView fv = new FolderView(1);
                fv.Traversal = FolderTraversal.Shallow;

                ExtendedPropertyDefinition epd = new ExtendedPropertyDefinition(13825, MapiPropertyType.Integer);
                SearchFilter sf = new SearchFilter.IsEqualTo(epd, "2");
                SearchFilter sf2 = new SearchFilter.IsEqualTo(FolderSchema.DisplayName, "allitems");
                SearchFilter.SearchFilterCollection sfc = new SearchFilter.SearchFilterCollection(LogicalOperator.And);
                sfc.Add(sf);
                sfc.Add(sf2);
                FindFoldersResults findAllItemsFolderresult = service.FindFolders(fid, sfc, fv);

This was working for years and when we changed the server this worked for few months then started giving this error. Please let me know the reason for this error and its solution.


Solution

  • I would suggest you enable tracing https://learn.microsoft.com/en-us/exchange/client-developer/exchange-web-services/how-to-trace-requests-responses-to-troubleshoot-ews-managed-api-applications and look at the response that is causing the issue. It maybe your getting a non EWS reply eg from a proxy or firewall etc