// description of your code here
1
2 private int GetUnReadMailCount()
3 {
4 string url=“http://mail.felixwoo.com/exchange/�; //指定Exchange�务器地�
5 System.Net.HttpWebRequest Request;
6 System.Net.WebResponse Response;
7 System.Net.CredentialCache MyCredentialCache;
8 string strUserName = “wufâ€?; //指定登录的用户å??
9 string strRootURI = url+strUserName ; //得到�访问邮箱的WebDAV地�
10 string strPassword = “123456â€?; //指定该用户的密ç ?
11 string strDomain = “felixwoo.comâ€?; //指定域å??
12 string strQuery ="";
13 byte[] bytes = null;
14 System.IO.Stream RequestStream = null;
15 System.IO.Stream ResponseStream = null;
16 XmlDocument ResponseXmlDoc = null;
17 XmlNodeList HrefNodes= null;
18 XmlNodeList SizeNodes= null;
19 int count=0;
20 try
21 {
22 // 用SQL查询WebDAV返回结果ä¸çš„unreadcount节点.
23 strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >"
24 + "<D:sql>SELECT \"DAV:displayname\",\"urn:schemas:httpmail:unreadcount\" FROM \"" + strRootURI + "\""
25 + "</D:sql></D:searchrequest>";
26
27 // 创建新的CredentialCacheå¯¹è±¡ï¼Œæž„å»ºèº«ä»½å‡æ?®
28 MyCredentialCache = new System.Net.CredentialCache();
29 MyCredentialCache.Add( new System.Uri(strRootURI),
30 "NTLM",
31 new System.Net.NetworkCredential(strUserName, strPassword, strDomain)
32 );
33
34 // Create the HttpWebRequest object.
35 Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI);
36
37 // 指定HttpWebRequestçš„èº«ä»½å‡æ?®ï¼Œæ¤å¤„为关键所在。如果使用之å‰?
38 // 创建的MyCredentialCacheï¼Œåˆ™è¿™ä¸ªèº«ä»½å‡æ?®æ˜¯å?¯ä»¥ä»ŽWebæœ?åŠ¡å™¨ä¼ é€’
39 // 到Exchangeæœ?åŠ¡å™¨çš„ï¼Œä½†æ˜¯è¿™æ ·å¸¦æ?¥çš„问题也很明显,就是ä¸?能够自
40 // 动获�当�登录到域的用户的身份。�便已��功登录到域,那也�
41 // 能通过formå†?次输入用户å??密ç ?ã€‚å› æ¤ï¼Œæˆ‘在这里用的是
42 // Request.Credentials = CredentialCache.DefaultCredentials,
43 // è¿™æ ·ä¾¿å?¯ä»¥è޷得当å‰?ç”¨æˆ·çš„å‡æ?®ï¼Œä½†æ˜¯è¿™æ ·å¸¦æ?¥çš„问题便是上é?¢æ??到的
44 // èº«ä»½å‡æ?®æ— æ³•ä¼ é€’çš„é—®é¢˜ï¼Œè§£å†³æ–¹æ³•è¯·å…³æ³¨ä¸‹ç¯‡æ–‡ç« ã€‚
45 Request.Credentials = MyCredentialCache;
46
47 // 指定WebDAV的SEARCH方法
48 Request.Method = "SEARCH";
49
50 // Encode the body using UTF-8.
51 bytes = Encoding.UTF8.GetBytes((string)strQuery);
52
53 // Set the content header length. This must be
54 // done before writing data to the request stream.
55 Request.ContentLength = bytes.Length;
56
57 // Get a reference to the request stream.
58 RequestStream = Request.GetRequestStream();
59
60 // Write the SQL query to the request stream.
61 RequestStream.Write(bytes, 0, bytes.Length);
62
63 // Close the Stream object to release the connection
64 // for further use.
65 RequestStream.Close();
66
67 // Set the content type header.
68 Request.ContentType = "text/xml";
69
70 // Send the SEARCH method request and get the
71 // response from the server.
72 Response = (HttpWebResponse)Request.GetResponse();
73
74 // Get the XML response stream.
75 ResponseStream = Response.GetResponseStream();
76
77 // 创建XmlDocument对象,并获�收件箱的unreadcount节点的值
78 ResponseXmlDoc = new XmlDocument();
79 ResponseXmlDoc.Load(ResponseStream);
80 HrefNodes = ResponseXmlDoc.GetElementsByTagName("a:displayname");
81 SizeNodes = ResponseXmlDoc.GetElementsByTagName("d:unreadcount");
82 for(int i=0;i<HrefNodes.Count;i++)
83 {
84 if(HrefNodes[i].InnerText=="æ”¶ä»¶ç®±")
85 count=int.Parse(SizeNodes[i].InnerText);
86 }
87 ResponseStream.Close();
88 Response.Close();
89 }
90 catch(Exception)
91 {
92 // Catch any exceptions. Any error codes from the SEARCH
93 // method request on the server will be caught here, also.
94 return -1;
95 }
96 return count;
97 }