Am getting invalid XML as a response from EWS calls?

谁说胖子不能爱 提交于 2019-12-04 14:35:13

I was getting your problem until (after MUCH trial and error):

  1. set TraceEnabled to true, this will dump the back and forth messages to console.

  2. I used the URL https://yourexchangeserver/EWS/Exchange.asmx
    e.g. my work uses BPOS, in asia pacific region, so : https://red003.mail.apac.microsoftonline.com/EWS/Exchange.asmx

  3. Request a specific service version e.g. ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1)

Step 1 got me past your first problem - it was giving the Outlook Web Access html page.
Step 2 let me see that it was then requesting 2010_Sp1, but that version wasn't supported.
Step 3 got "Hello world" working/sending.

Another note if you use that server, I couldn't get it to take any version except 2007 SP1, and thus, no AutoDiscovery of the URL.

public static string sendMail_BPOS_EWS()
        {
              try
            {
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
                service.UseDefaultCredentials = false;
                service.Credentials = new WebCredentials("some_address@server.com", "password");

                service.Url = new Uri("https://red003.mail.apac.microsoftonline.com/EWS/Exchange.asmx");
                Console.WriteLine(service.Url);

                service.TraceEnabled = true;

                EmailMessage mail = new EmailMessage(service);
                mail.From = new EmailAddress("from_address@server.com");
                mail.ToRecipients.Add("to_address@server.com");
                mail.Subject = "Email Subject";
                mail.Body = "Email Body";

                mail.Send();
                return "sent";
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!