以编程方式从Exchange Server 2010邮箱中读取电子邮件

时间:2014-02-20 16:53:09

标签: c# xml httpwebrequest httpwebresponse exchange-server-2010

我们有一个c#应用程序,它使用http服务读取当前托管在Exchange 2003上的电子邮件收件箱。

现在要将邮箱迁移到Exchange 2010服务器,因此我们正在测试我们的代码以确认它仍然有效。

我们收到以下代码(尝试获取所有邮件)的错误“错误请求”:         public static XmlDocument GetUnreadMailAll()         {             HttpWebRequest loRequest = default(HttpWebRequest);             HttpWebResponse loResponse = default(HttpWebResponse);             string lsRootUri = null;             string lsQuery = null;             byte [] laBytes = null;

        Stream loRequestStream = default(Stream);
        Stream loResponseStream = default(Stream);
        XmlDocument loXmlDoc = default(XmlDocument);
        loXmlDoc = new XmlDocument();
        try
        {
            lsRootUri = strServer + "/Exchange/" + strAlias + "/" + strInboxURL;
            lsQuery = "<?xml version=\"1.0\"?>"
                + "<D:searchrequest xmlns:D = \"DAV:\" xmlns:m=\"urn:schemas:httpmail:\">"
                + "<D:sql>SELECT "
                + "\"urn:schemas:httpmail:to\", "
                + "\"urn:schemas:httpmail:displayto\", "
                + "\"urn:schemas:httpmail:from\", "
                + "\"urn:schemas:httpmail:fromemail\", "
                + "\"urn:schemas:httpmail:subject\", "
                + "\"urn:schemas:httpmail:textdescription\", "
                //+ "\"urn:schemas:httpmail:htmldescription\", "
                + "\"urn:schemas:httpmail:hasattachment\", "
                + "\"urn:schemas:httpmail:attachmentfilename\", "
                + "\"urn:schemas:httpmail:senderemail\", "
                + "\"urn:schemas:httpmail:sendername\", "
                + "\"DAV:displayname\", "
                + "\"urn:schemas:httpmail:datereceived\", "
                + "\"urn:schemas:httpmail:read\", "
                + "\"DAV:id\" "
                + "FROM \"" + lsRootUri
                + "\" WHERE \"DAV:ishidden\" = false "
                + "AND \"DAV:isfolder\" = false "
                + "AND \"urn:schemas:httpmail:read\" = false "
                + "AND \"urn:schemas:httpmail:fromemail\" != 'emailAddy@domainName.co.uk' "
                + "</D:sql></D:searchrequest>";
            loRequest = (HttpWebRequest)WebRequest.Create(lsRootUri);
            loRequest.Credentials = new NetworkCredential(strUserName, strPassword);
            loRequest.Method = "SEARCH";
            laBytes = System.Text.Encoding.UTF8.GetBytes(lsQuery);
            loRequest.ContentLength = laBytes.Length;
            loRequestStream = loRequest.GetRequestStream();
            loRequestStream.Write(laBytes, 0, laBytes.Length);
            loRequestStream.Close();
            loRequest.ContentType = "text/xml";
            loRequest.Headers.Add("Translate", "F");
            loResponse = (HttpWebResponse)loRequest.GetResponse();
            loResponseStream = loResponse.GetResponseStream();
            loXmlDoc.Load(loResponseStream);
            loResponseStream.Close();
        }

loResponseStream = loResponse.GetResponseStream();

上抛出异常

这是我们发送的xml:

  <?xml version="1.0" ?> 
- <D:searchrequest xmlns:D="DAV:" xmlns:m="urn:schemas:httpmail:">
  <D:sql>SELECT "urn:schemas:httpmail:to", "urn:schemas:httpmail:displayto", "urn:schemas:httpmail:from", "urn:schemas:httpmail:fromemail", "urn:schemas:httpmail:subject", "urn:schemas:httpmail:textdescription", "urn:schemas:httpmail:hasattachment", "urn:schemas:httpmail:attachmentfilename", "urn:schemas:httpmail:senderemail", "urn:schemas:httpmail:sendername", "DAV:displayname", "urn:schemas:httpmail:datereceived", "urn:schemas:httpmail:read", "DAV:id" FROM "https://domain/Exchange/bbtest/Inbox" WHERE "DAV:ishidden" = false AND "DAV:isfolder" = false AND "urn:schemas:httpmail:read" = false AND "urn:schemas:httpmail:fromemail" != 'emailAddy@domainName.co.uk'</D:sql> 
  </D:searchrequest>

1 个答案:

答案 0 :(得分:0)

来自MSDN,答案是 WebDAV在Exchange 2007之后已弃用,并被 Exchange Web服务

取代

这里有几个链接: