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

[经验分享] 读取Exchange邮件或任务((第二种方法)

[复制链接]

尚未签到

发表于 2017-12-8 17:35:18 | 显示全部楼层 |阅读模式
  上篇文章中介绍了通过Exchange Web Service来读取用户邮件或任务信息,大家可以看到需要写的代码很多且也比较复杂。接下介绍读取Exchange邮件或任务的第二种方法---通过Exchange Web Service Managed API 1.1.

  Exchange Web Service Managed API:是微软提供的通过Exchange Web Service开发客户端应用程序来快速读取Exchange邮件、任务、发送邮件、删除邮件等的管理接口。
  使用API的好处:
  1、不用需要引用Exchange Web Service
  2、代码量大大减少,提高开发效率
  3、提高性能且更简单容易的对Exchange进行管理和操作
  下载地址:http://www.microsoft.com/downloads/en/details.aspx?FamilyID=c3342fb3-fbcc-4127-becf-872c746840e1
  通过API来读取Exchange的邮件或任务的实现过程,如下:
  1、引用如下命名空间:
  using Microsoft.Exchange.WebServices.Data;
  using System.Net;
  using System.Net.Security;
  using System.Security.Authentication;
  using System.Security.Cryptography.X509Certificates;
  2、实现代码
  


DSC0000.gif
        /// <summary>
        /// 读取所有Exchange对象数目,不使用模拟功能
        /// </summary>
        /// <param name="serverName">服务器名称</param>
        /// <param name="useAutodiscover">是否自动发现服务器URL</param>
        public void EWSImpersonations(string serverName, bool useAutodiscover)
        {
            try
            {
                //Call SSL Web service must added!!!
                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);//如果是Exchange 2010,则切换到2010
            //建立信任连接
                //ICredentials creds = CredentialCache.DefaultNetworkCredentials;
                    ICredentials creds = new NetworkCredential("username", "password", "domain");
                service.Credentials = new WebCredentials(creds);

                if (!useAutodiscover)
                {
                    string[] servers = serverName.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    for (int i = 0; i < servers.Length; i++)
                    {
                        try
                        {
                            service.Url = new Uri("https://" + servers + "/ews/exchange.asmx");
                            break;
                        }
                        catch
                        {
                        }
                    }
                }

                service.PreAuthenticate = true;
            
            //创建读取信息视图
                ItemView view = new ItemView(10);
                view.OrderBy.Add(ItemSchema.DateTimeReceived, Microsoft.Exchange.WebServices.Data.SortDirection.Descending);

                #region ReadEmail

                Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);//Inbox文件夹,不包括子文件夹
                int unread = inbox.UnreadCount;

                Microsoft.Exchange.WebServices.Data.EmailMessage em = null;

                FindItemsResults<Item> findMailResults = service.FindItems(WellKnownFolderName.Inbox, view);
                Response.Write("<br>unread mail subject: <br><br>");

                foreach (Item m in findMailResults)
                {
                    em = (Microsoft.Exchange.WebServices.Data.EmailMessage)m;
                    if (!em.IsRead)
                    {
                        Response.Write(em.Subject + string.Format("  发件人: {0} <br>", em.From.Name.ToString()));
                    }
                }

                //find all child folders mail message
                FindInboxChildFolders(service, view, serverName, ref unread);

                Response.Write("unread mail : " + unread.ToString() + "<br>");

                #endregion

                Response.Write("==================================================");

                #region ReadTask

                int newTasks = 0;//unread task number
                FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Tasks,view); //读取任务文件夹,不包括子文件夹
                Microsoft.Exchange.WebServices.Data.Task t = null;

                Response.Write("<br>unread task subject:<br>");

                foreach (Item item in findResults.Items)
                {
                    t = (Microsoft.Exchange.WebServices.Data.Task)(item);
                    if (!t.IsComplete)
                    {
                        newTasks++;

                        Response.Write(t.Subject);

                        Response.Write("<br>status :" + t.Status);
                    }
                }

                //Find all task child folder
                FindTaskChildFolders(service, view, serverName, ref newTasks);

                Response.Write("<br><br> unread task :" + newTasks.ToString());

                #endregion
            }
            catch (Exception ex)
            {
                Response.Write(errorMessage);
            }
        }

        /// <summary>
        /// Find all child folder mail message
        /// </summary>
        /// <param name="service"></param>
        /// <param name="view"></param>
        /// <param name="serverName"></param>
        /// <param name="unread"></param>
        private void FindInboxChildFolders(ExchangeService service, ItemView view, string serverName, ref int unread)
        {
            try
            {
                FindFoldersResults findResults = service.FindFolders(WellKnownFolderName.Inbox,new FolderView(int.MaxValue));
                Microsoft.Exchange.WebServices.Data.EmailMessage em = null;
                foreach (Folder folder in findResults.Folders)
                {
                    FindItemsResults<Item> findMailResults = folder.FindItems(view);
                    unread += folder.UnreadCount;
                    foreach (Item m in findMailResults)
                    {
                        em = (Microsoft.Exchange.WebServices.Data.EmailMessage)m;
                        if (!em.IsRead)
                        {
                            Response.Write(em.Subject + string.Format("  发件人: {0} <br>", em.From.Name.ToString()));
                        }
                    }
                }
            }
            catch
            {
            }

        }

        /// <summary>
        /// Find all task child folder
        /// </summary>
        /// <param name="service"></param>
        /// <param name="view"></param>
        /// <param name="serverName"></param>
        /// <param name="newTasks"></param>
        private void FindTaskChildFolders(ExchangeService service, ItemView view, string serverName, ref int newTasks)
        {
            try
            {
                FindFoldersResults findResults = service.FindFolders(WellKnownFolderName.Tasks,new FolderView(int.MaxValue));

                Microsoft.Exchange.WebServices.Data.Task t = null;

                foreach (Folder folder in findResults.Folders)
                {
                    FindItemsResults<Item> findTaskResults = folder.FindItems(view);
                    foreach (Item task in findTaskResults)
                    {
                        t = (Microsoft.Exchange.WebServices.Data.Task)(task);
                        if (!t.IsComplete)
                        {
                            newTasks++;

                            Response.Write( t.Subject );

                            Response.Write("<br>status :" + t.Status);
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        #endregion

        /// <summary>
        /// 忽略SSL证书请求
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="certificate"></param>
        /// <param name="chain"></param>
        /// <param name="errors"></param>
        /// <returns></returns>
        public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {
            return true;
        }

运维网声明 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-422226-1-1.html 上篇帖子: 删除Exchange Server IIS 15天日志 下篇帖子: [EWS]如何: 通过使用 Exchange 中的 EWS 流有关邮箱事件的通知
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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