可以通过WEBDAV的方法读取EXCHANGE的邮件信息,在E8.Net工作流架构的应用程序中有过这样开发案例,因为客户可能一开始不习惯用OUTLOOK等工具去阅读邮件,所以通过。NET程序读取exchange中的邮件信息,展示在用户的首页上。
代码如下:
/**/
/// <summary>
/// 取得未读邮件信息
/// </summary>
/// <returns></returns>
private
string
GetUnReadMailList(
string
strUserID,
string
strPassword)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
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;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
Epower.DevBase.Organization.SqlDAL.UserEntity user = new Epower.DevBase.Organization.SqlDAL.UserEntity(strUserID);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
string strEmailXml = "";
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
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>";
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//集成验证方式下代码
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;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Specify the method.
Request.Method = "SEARCH";
// Encode the body using UTF-8.
bytes = Encoding.UTF8.GetBytes((string)strQuery);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Set the content header length.. This must be
// done before writing data to the request stream.
Request.ContentLength = bytes.Length;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Get a reference to the request stream.
RequestStream = Request.GetRequestStream();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Write the SQL query to the request stream.
RequestStream.Write(bytes, 0, bytes.Length);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Close the Stream object to release the connection
// for further use.
RequestStream.Close();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Set the content type header.
Request.ContentType = "text/xml;charset="utf-8"";
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Send the SEARCH method request and get the
// response from the server.
Response = (HttpWebResponse)Request.GetResponse();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Get the XML response stream.
ResponseStream = Response.GetResponseStream();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// Create the XmlDocument object from the XML response stream.
ResponseXmlDoc = new XmlDocument();
ResponseXmlDoc.Load(ResponseStream);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
strEmailXml = ResponseXmlDoc.InnerXml;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ResponseStream.Close();
Response.Close();
}
catch (Exception e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// Catch any exceptions. Any error codes from the SEARCH
// method request on the server will be caught here, also.
return "";
}
return strEmailXml;
}
返回的也是XML串,经过一些处理便可以以一定的格式展示邮件的信息了
代码的关键是 strQuery的XML查询串
相关语法及信息可以参考:
http://msdn2.microsoft.com/en-us/library/ms527286.aspx
从这里可以找到各种字段和命名空间的意义.
通过 WebDav的方法 不但可以取得exchange邮件的信息、邮件数量,还可以与exchange日历进行交互。
在E8.Net工作流的一些合作伙伴中已经实现在 工作流的业务处理接口中,根据预计时间将待办事项写入exchange的日历中。 用户通过OUTLOOK的日历直接进入事项的相关处理。实现方法也是通过WEBDAV。 与获取邮件的方法类似。
E8.Net工作流架构,企业应用开发的利器 http://www.feifanit.com.cn/productFlow.htm