【IT168技术文档】
可以通过WEBDAV的方法读取EXCHANGE的邮件信息,在E8.Net工作流架构的应用程序中有过这样开发案例,因为客户可能一开始不习惯用OUTLOOK等工具去阅读邮件,所以通过。NET程序读取exchange中的邮件信息,展示在用户的首页上。
代码如下:
/// <summary> /// 取得未读邮件信息 /// </summary> /// <returns></returns> private string GetUnReadMailList(string strUserID, string strPassword) ...{ string url = System.Configuration.ConfigurationManager.AppSettings["ExchangeServer"]; System.Net.HttpWebRequest Request; System.Net.WebResponse Response; System.Net.CredentialCache MyCredentialCache; string strRootURI = url + "/" + strUserID + "/收件箱"; string strUserName = strUserID; //string strDomain = ConfigurationSettings.AppSettings["ExchangeDomain"]; string strDomain = System.Configuration.ConfigurationManager.AppSettings["Domain"]; string strQuery = ""; byte[] bytes = null; System.IO.Stream RequestStream = null; System.IO.Stream ResponseStream = null; XmlDocument ResponseXmlDoc = null; Epower.DevBase.Organization.SqlDAL.UserEntity user = new Epower.DevBase.Organization.SqlDAL.UserEntity(strUserID); string strEmailXml = ""; try ...{ strQuery = "<?xml version="1.0"?><D:searchrequest xmlns:D = "DAV:" >" + "<D:sql>SELECT "DAV:displayname","urn:schemas:mailheader:subject"," // + ""urn:schemas:mailheader:approved "," // + ""urn:schemas:mailheader:comment "," + ""urn:schemas:mailheader:from"," + ""urn:schemas:mailheader:date" FROM "" + strRootURI + """ + "where "urn:schemas:httpmail:read"=false" // + "WHERE "DAV:ishidden" = false AND "DAV:isfolder" = false" + "</D:sql></D:searchrequest>"; //集成验证方式下代码 Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI); Request.Timeout = 300000; //超时 5分钟 MyCredentialCache = new System.Net.CredentialCache(); MyCredentialCache.Add(new System.Uri(strRootURI), "NTLM", new System.Net.NetworkCredential(strUserName, user.Password, strDomain)// );//NTLM集成windows验证 Basic 基本验证 Request.Credentials = MyCredentialCache; //Request.Credentials = CredentialCache.DefaultNetworkCredentials; // Specify the method. Request.Method = "SEARCH"; // Encode the body using UTF-8. bytes = Encoding.UTF8.GetBytes((string)strQuery); // Set the content header length.. This must be // done before writing data to the request stream. Request.ContentLength = bytes.Length; // Get a reference to the request stream. RequestStream = Request.GetRequestStream(); // Write the SQL query to the request stream. RequestStream.Write(bytes, 0, bytes.Length); // Close the Stream object to release the connection // for further use. RequestStream.Close(); // Set the content type header. Request.ContentType = "text/xml;charset="utf-8""; // Send the SEARCH method request and get the // response from the server. Response = (HttpWebResponse)Request.GetResponse(); // Get the XML response stream. ResponseStream = Response.GetResponseStream(); // Create the XmlDocument object from the XML response stream. ResponseXmlDoc = new XmlDocument(); ResponseXmlDoc.Load(ResponseStream); strEmailXml = ResponseXmlDoc.InnerXml; ResponseStream.Close(); Response.Close(); } catch (Exception e) ...{ // Catch any exceptions. Any error codes from the SEARCH // method request on the server will be caught here, also. return ""; } return strEmailXml; }