在MOSS开发中,我们往往会集成Exchange服务器中的邮件等数据,采用WebPart或其他方式。
Exchange 2007中提供了一套简易的Web Service。Exchange 2003中却没有。
考虑到目前很多企业使用的都是Exchange 2003,下面我给出一套实用WebDAV读取Exchange的通用方法。
为了让更多的人使用,示例代码使用普通的asp.net版本。请需要做MOSS的朋友自己改为Web Part。
界面截图
首先,定义访问Exchange的基本信息
Code
string url = "http://10.10.0.1/exchange/"; //指定Exchange服务器地址
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
string strUserName = "L1"; //指定登录的用户名
string strRootURI = url + strUserName.Trim() + "/收件箱/"; //得到要访问邮箱的WebDAV地址
string strPassword = "!QAZ1qaz"; //指定该用户的密码
string strDomain = "huahua.com"; //指定域名
string url = "http://10.10.0.1/exchange/"; //指定Exchange服务器地址
System.Net.HttpWebRequest Request;
System.Net.WebResponse Response;
System.Net.CredentialCache MyCredentialCache;
string strUserName = "L1"; //指定登录的用户名
string strRootURI = url + strUserName.Trim() + "/收件箱/"; //得到要访问邮箱的WebDAV地址
string strPassword = "!QAZ1qaz"; //指定该用户的密码
string strDomain = "huahua.com"; //指定域名
然后,构造查询字符串:
Code
strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >"
+ "<D:sql>SELECT \"urn:schemas:httpmail:importance\",\"urn:schemas:httpmail:from\",\"urn:schemas:httpmail:read\",\"urn:schemas:httpmail:datereceived\",\"urn:schemas:httpmail:subject\",\"urn:schemas:httpmail:hasattachment\","
+ "\"DAV:contentclass\",\"DAV:getcontentlength\",\"DAV:displayname\""
+ "FROM \"" + strRootURI + "\""
+ "WHERE \"DAV:ishidden\" = false AND \"DAV:isfolder\" = false"
+ "</D:sql></D:searchrequest>";
strQuery = "<?xml version=\"1.0\"?><D:searchrequest xmlns:D = \"DAV:\" >"
+ "<D:sql>SELECT \"urn:schemas:httpmail:importance\",\"urn:schemas:httpmail:from\",\"urn:schemas:httpmail:read\",\"urn:schemas:httpmail:datereceived\",\"urn:schemas:httpmail:subject\",\"urn:schemas:httpmail:hasattachment\","
+ "\"DAV:contentclass\",\"DAV:getcontentlength\",\"DAV:displayname\""
+ "FROM \"" + strRootURI + "\""
+ "WHERE \"DAV:ishidden\" = false AND \"DAV:isfolder\" = false"
+ "</D:sql></D:searchrequest>";
接下来,构造身份凭证、执行WebDAV的Search
Code
// 创建新的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();
ResponseXmlDoc = new XmlDocument();
ResponseXmlDoc.Load(ResponseStream);
// 创建新的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();
ResponseXmlDoc = new XmlDocument();
ResponseXmlDoc.Load(ResponseStream);
最后,也就是最激动人心的一步:通过解析Xml节点来取得想要的数据。比如:
Code
XmlNodeList idNodes = ResponseXmlDoc.GetElementsByTagName("a:displayname");
XmlNodeList SenderNodes = ResponseXmlDoc.GetElementsByTagName("d:from");
XmlNodeList importanceNodes = ResponseXmlDoc.GetElementsByTagName("d:importance");
XmlNodeList contextclassNodes = ResponseXmlDoc.GetElementsByTagName("a:contentclass");
XmlNodeList readNodes = ResponseXmlDoc.GetElementsByTagName("d:read");
XmlNodeList datareceiveNodes = ResponseXmlDoc.GetElementsByTagName("d:datereceived");
XmlNodeList subjectNodes = ResponseXmlDoc.GetElementsByTagName("d:subject");
XmlNodeList getcontentlengthNodes = ResponseXmlDoc.GetElementsByTagName("a:getcontentlength");
XmlNodeList hasattachmentNodes = ResponseXmlDoc.GetElementsByTagName("d:hasattachment");
XmlNodeList hrefNodes = ResponseXmlDoc.GetElementsByTagName("a:href");
DataTable dt = new DataTable();
dt.Columns.Add("Subject");
dt.Columns.Add("Href");
dt.Columns.Add("Time");
dt.Columns["Time"].DataType = Type.GetType("System.DateTime");
for (int j = 0; j < readNodes.Count; j++)
{
// if (readNodes[j].InnerText == "1")continue;//只取未读邮件
dt.Rows.Add(subjectNodes[j].InnerText,hrefNodes[j].InnerText, DateTime.Parse(datareceiveNodes[j].InnerText));
}
ResponseStream.Close();
Response.Close();
XmlNodeList idNodes = ResponseXmlDoc.GetElementsByTagName("a:displayname");
XmlNodeList SenderNodes = ResponseXmlDoc.GetElementsByTagName("d:from");
XmlNodeList importanceNodes = ResponseXmlDoc.GetElementsByTagName("d:importance");
XmlNodeList contextclassNodes = ResponseXmlDoc.GetElementsByTagName("a:contentclass");
XmlNodeList readNodes = ResponseXmlDoc.GetElementsByTagName("d:read");
XmlNodeList datareceiveNodes = ResponseXmlDoc.GetElementsByTagName("d:datereceived");
XmlNodeList subjectNodes = ResponseXmlDoc.GetElementsByTagName("d:subject");
XmlNodeList getcontentlengthNodes = ResponseXmlDoc.GetElementsByTagName("a:getcontentlength");
XmlNodeList hasattachmentNodes = ResponseXmlDoc.GetElementsByTagName("d:hasattachment");
XmlNodeList hrefNodes = ResponseXmlDoc.GetElementsByTagName("a:href");
DataTable dt = new DataTable();
dt.Columns.Add("Subject");
dt.Columns.Add("Href");
dt.Columns.Add("Time");
dt.Columns["Time"].DataType = Type.GetType("System.DateTime");
for (int j = 0; j < readNodes.Count; j++)
{
// if (readNodes[j].InnerText == "1")continue;//只取未读邮件
dt.Rows.Add(subjectNodes[j].InnerText,hrefNodes[j].InnerText, DateTime.Parse(datareceiveNodes[j].InnerText));
}
ResponseStream.Close();
Response.Close();
其他代码在这里就不一一列举了。 附件是完整的Project。