ExchangeService(EWS)錯誤:The response received from the service didn’t contain valid XML.

來源:互聯網
上載者:User

在用ExchangeService(EWS)串連Exchange郵件伺服器時,報錯:

        The response received from the service didn't contain valid XML.其InnerException為:        The expected XML node type was XmlDeclaration, but the actual type is Element.其原始碼如下:   
 //串連伺服器            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);            service.Credentials = new NetworkCredential("***", "***", "***");            service.AutodiscoverUrl("***");               //擷取郵件清單(收件匣中的郵件)            FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox,new ItemView(10));                     foreach (Item item in findResults.Items)            {                               //擷取具體的郵件對象                EmailMessage email = EmailMessage.Bind(service, item.Id);                //判斷附件是否為檔案                 if (!(email.Attachments[0] is FileAttachment)) continue;                FileAttachment fileAttachment = email.Attachments[0] as FileAttachment;                fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);                //標示讀取                   email.IsRead = true;                //將對郵件的改動提交到伺服器                   email.Update(ConflictResolutionMode.AlwaysOverwrite);            }

查了一些資料,也沒有找到具體的原因,自己嘗試了好多方法,最終將代碼更改如下:       

 //串連伺服器            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);            service.Credentials = new NetworkCredential("使用者名稱", "密碼", "域");            service.AutodiscoverUrl("你的郵箱");            service.Url = new Uri("這裡是你的郵箱Service地址(以asmx結尾)");               //擷取郵件清單(收件匣中的郵件)            FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox,new ItemView(10));                     foreach (Item item in findResults.Items)            {                               //擷取具體的郵件對象                EmailMessage email = EmailMessage.Bind(service, item.Id);                //判斷附件是否為檔案                 if (!(email.Attachments[0] is FileAttachment)) continue;                FileAttachment fileAttachment = email.Attachments[0] as FileAttachment;                fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);                //標示讀取                   email.IsRead = true;                //將對郵件的改動提交到伺服器                   email.Update(ConflictResolutionMode.AlwaysOverwrite);            }

這一次就能正常收取了,加入了URL問題就解決了,不知道這個是什麼原因,我原來用第一段代碼的時候,並沒有加入Url也能夠正常訪問的,這個事情比較的奇怪。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.