webdav 搜索查询 exchang 的未读邮件数
代码
private int GetUnReadMailCount(string userName, string passWord)
{
string url=""; //指定Exchange服务器地址
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
string strUserName = userName; //指定登录的用户名
string strRootURI = url+strUserName; //得到要访问邮箱的WebDAV地址
string strPassword = passWord; //指定该用户的密码
//string strDomain = ""; //指定域名
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
{
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>";
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add(new Uri(strRootURI), "Basic", new NetworkCredential(strUserName, strPassword));
Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI);
Request.Credentials = MyCredentialCache;
Request.Method = "SEARCH";
bytes = Encoding.UTF8.GetBytes((string)strQuery);
Request.ContentLength = bytes.Length;
RequestStream = Request.GetRequestStream();
RequestStream.Write(bytes, 0, bytes.Length);
RequestStream.Close();
Request.ContentType = "text/xml";
Response = (HttpWebResponse)Request.GetResponse();
ResponseStream = Response.GetResponseStream();
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)
{
return -1;
}
return count;
}
{
string url=""; //指定Exchange服务器地址
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
string strUserName = userName; //指定登录的用户名
string strRootURI = url+strUserName; //得到要访问邮箱的WebDAV地址
string strPassword = passWord; //指定该用户的密码
//string strDomain = ""; //指定域名
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
{
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>";
MyCredentialCache = new System.Net.CredentialCache();
MyCredentialCache.Add(new Uri(strRootURI), "Basic", new NetworkCredential(strUserName, strPassword));
Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(strRootURI);
Request.Credentials = MyCredentialCache;
Request.Method = "SEARCH";
bytes = Encoding.UTF8.GetBytes((string)strQuery);
Request.ContentLength = bytes.Length;
RequestStream = Request.GetRequestStream();
RequestStream.Write(bytes, 0, bytes.Length);
RequestStream.Close();
Request.ContentType = "text/xml";
Response = (HttpWebResponse)Request.GetResponse();
ResponseStream = Response.GetResponseStream();
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)
{
return -1;
}
return count;
}