我们有一个 C# 应用程序,它使用 http 服务读取当前托管在 Exchange 2003 上的电子邮件收件箱。
现在,邮箱将迁移到 Exchange 2010 服务器,因此我们正在测试代码以确认它仍然可以工作。
我们收到错误"错误请求",其中包含以下代码(尝试获取所有邮件): public static XmlDocument GetUnreadMailAll() { HttpWebRequest loRequest = default(HttpWebRequest); HttpWebResponse loResponse = default(HttpWebResponse); 字符串 lsRootUri = 空; 字符串 lsQuery = 空; 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>
从 MSDN 开始,答案是 WebDAV 在 Exchange 2007 之后被弃用,取而代之的是 Exchange Web Services
这里有几个链接:
- MSDN 库:Exchange Web 服务入门
- OMEGACODER:使用 Exchange Web Services 从 Exchange 获取所有电子邮件
- MSDN 代码下载: 交换 - 101 个示例