DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

获�Exchange未读邮件数

10.15.2006
| 3551 views |
  • submit to reddit
        // description of your code here

private int GetUnReadMailCount()
{
string url=“http://mail.felixwoo.com/exchange/�; //指定Exchange�务器地� 
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
string strUserName = “wufâ€?; //指定登录的用户å??
string strRootURI = url+strUserName ; //得到�访问邮箱的WebDAV地�
string strPassword = “123456�; //指定该用户的密�
string strDomain = “felixwoo.comâ€?; //指定域å??
string strQuery ="";
byte[] bytes = null;
System.IO.Stream RequestStream = null;
System.IO.Stream ResponseStream = null;
XmlDocument ResponseXmlDoc = null;
XmlNodeList HrefNodes= null;
XmlNodeList SizeNodes= null;
int count=0;
try
{
  // 用SQL查询WebDAV返回结果中的unreadcount节点.
  strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >"
   + "<D:sql>SELECT \"DAV:displayname\",\"urn:schemas:httpmail:unreadcount\" FROM \"" + strRootURI + "\""
   + "</D:sql></D:searchrequest>";

  // 创建新的CredentialCache对象,构建身份凭�
  MyCredentialCache = new System.Net.CredentialCache();
  MyCredentialCache.Add( new System.Uri(strRootURI),
   "NTLM",
   new System.Net.NetworkCredential(strUserName, strPassword, strDomain)
   );

  // Create the HttpWebRequest object.
  Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI);

  // 指定HttpWebRequest的身份凭�,此处为关键所在。如果使用之�
  // 创建的MyCredentialCache,则这个身份凭�是�以从Web�务器传递
  // 到Exchange�务器的,但是这样带�的问题也很明显,就是�能够自
  // 动获�当�登录到域的用户的身份。�便已��功登录到域,那也�
  // 能通过formå†?次输入用户å??密ç ?。因此,我在这里用的是
  // Request.Credentials = CredentialCache.DefaultCredentials,
  // 这样便å?¯ä»¥èŽ·å¾—当å‰?用户的凭æ?®ï¼Œä½†æ˜¯è¿™æ ·å¸¦æ?¥çš„问题便是上é?¢æ??到的
  // 身份凭�无法传递的问题,解决方法请关注下篇文章。
  Request.Credentials = MyCredentialCache;

  // 指定WebDAV的SEARCH方法
  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";

  // Send the SEARCH method request and get the
  // response from the server.
  Response = (HttpWebResponse)Request.GetResponse();

  // Get the XML response stream.
  ResponseStream = Response.GetResponseStream();

  // 创建XmlDocument对象,并获�收件箱的unreadcount节点的值
  ResponseXmlDoc = new XmlDocument();
  ResponseXmlDoc.Load(ResponseStream);
  HrefNodes = ResponseXmlDoc.GetElementsByTagName("a:displayname");
  SizeNodes = ResponseXmlDoc.GetElementsByTagName("d:unreadcount");
  for(int i=0;i<HrefNodes.Count;i++)
  {
   if(HrefNodes[i].InnerText=="收件箱")
    count=int.Parse(SizeNodes[i].InnerText);
  }
  ResponseStream.Close();
  Response.Close();
}
catch(Exception)
{
  // Catch any exceptions. Any error codes from the SEARCH
  // method request on the server will be caught here, also.
  return -1;
}
return count;
}