设为首页 收藏本站
查看: 1258|回复: 0

[经验分享] wedav读取exchange(2003)邮件信息

[复制链接]

尚未签到

发表于 2015-9-10 13:26:17 | 显示全部楼层 |阅读模式
  第一次做webdav方法读取excahnge邮件方面的东西,将网上查找到的代码改成了.ascx控件,后台代码如下:
  

DSC0000.gif DSC0001.gif 代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Data;
using System.Xml;
namespace SharePoint.Exchange2003
{
    public class unreadmailcontrol : UserControl
    {
        GridView GridView1;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                GridView1 = (GridView)this.FindControl("GridView1");
                GridView1.DataSource = GetUnReadMail();
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
            }
        }
        private DataTable GetUnReadMail()
        {
            string url = "http://ip/exchange/"; //指定Exchange服务器地址
            System.Net.HttpWebRequest Request;
            System.Net.WebResponse Response;
            System.Net.CredentialCache MyCredentialCache;
            string strUserName = "administrator"; //指定登录的用户名
            string strRootURI = url + strUserName.Trim() + "/收件箱/"; //得到要访问邮箱的WebDAV地址
            string strPassword = "test"; //指定该用户的密码
            string strDomain = "test"; //指定域名
            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:subject\",\"urn:schemas:httpmail:unreadcount\" FROM \"" + strRootURI + "\""
                // + "</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>";

                // 创建新的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.InnerText == "收件箱")
                //        count = int.Parse(SizeNodes.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();

                return dt;
            }
            catch
            {
                return null;
            }
        }
    }
}  
  前台展示页面(.ascx)如下:
  

代码

<%@ Control Language="C#"  Inherits="SharePoint.Exchange2003.unreadmailcontrol, SharePoint.Exchange2003, Version=1.0.0.0, Culture=neutral, PublicKeyToken=7da4c70ffa3a2438" %>
  <style type="text/css">
        img
        {
            border-style: none;
        }
        body
        {
            font-size: 12px;
        }
        .td
        {
        }
        a
        {
            text-decoration: none;
            color: #616161;
        }
        #header
        {
            width: 225px;
            height: 20px;
            background-repeat: no-repeat;
            background-image: url(/_layouts/images/header.png);
            padding-left: 180px;
            padding-top: 4px;
        }
        #header a
        {
            color: #B88A00;
        }
        #dataTable
        {
            height: 259px;
            width: 223px;
            border-style: solid;
            border-width: 1px;
            border-color: Gray;
        }
        #mainDiv
        {
            
        }
    </style>
   
  <div id="mainDiv">
        <div id="header">
            <a href="http://ip/exchange/" target="_blank">更多...</a></div>
        <div id="dataTable">
            <asp:GridView ID="GridView1" runat="server" ShowHeader="False" AutoGenerateColumns="False"
                Width="223px" RowStyle-Height="23px" RowStyle-CssClass="td" AllowPaging="True"
                BorderColor="#CCCCCC" BorderStyle="Groove">
                <PagerSettings Visible="False" />
                <RowStyle CssClass="td" Height="23px"></RowStyle>
                <Columns>
                    <asp:TemplateField ShowHeader="False">
                        <ItemTemplate>
                            <img alt="" src="/_layouts/images/email.jpg" />
                        </ItemTemplate>
                        
                    </asp:TemplateField>
                    <asp:TemplateField ShowHeader="False" ItemStyle-Height="23">
                        <ItemTemplate>
                            <div style="text-overflow: ellipsis; white-space: nowrap; overflow: hidden; width: 145px;
                                padding-left: 3px;">
                                <a id="LinkButton1" runat="server" href='<%# DataBinder.Eval(Container.DataItem, "Href") %> '
                                    target="_blank">
                                    <%# DataBinder.Eval(Container.DataItem, "Subject") %>
                                </a>
                            </div>
                        </ItemTemplate>
                        <ItemStyle Height="23px"></ItemStyle>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Time" DataFormatString="{0:MM-dd}">
                        <ItemStyle Width="45px" ForeColor="Gray" Font-Names="Verdana" HorizontalAlign=" Center"
                            Font-Size="10px"></ItemStyle>
                    </asp:BoundField>
                </Columns>
            </asp:GridView>
        </div>
    </div>
  
  可以方便的以上代码封装成webpart或者扩张其功能。
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-111994-1-1.html 上篇帖子: POJ 1860, Currency Exchange 下篇帖子: 用cdo的Exchange的一点点东西
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表