[EWS]查找 文件夹

摘要

有时在操作exchange的时候,需要查找用户exchange文件夹,比如用户新建了一些文件夹。

一个例子

这里以查找用户outlook邮箱中的历史对话文件夹为例。

  private const ExchangeVersion EXCHANGE_VERSION = ExchangeVersion.Exchange2010_SP2;
        private ExchangeService _service;
     
        public MyExchangeClient(UserInfo userInfo)
        {
            if (userInfo == null)
            {
                throw new ArgumentNullException("user is empty");
            }
            this._userInfo = userInfo;
            this._service = new ExchangeService(EXCHANGE_VERSION);
   _service.Credentials = new NetworkCredential(userInfo.Itcode, userInfo.Pwd, _userInfo.Domain);
                _service.Url = new Uri(ConfigManager.ExchangeServerUrl);
            
        }
 /// <summary>
        /// 查找对话历史文件夹
        /// </summary>
        /// <returns></returns>
        private Folder FindImHistoryFolder()
        {
            int _pageSize = 10;
            FolderView folderView = new FolderView(_pageSize, 0);
            folderView.PropertySet = new PropertySet(BasePropertySet.IdOnly);
            folderView.PropertySet.Add(FolderSchema.DisplayName);
            folderView.PropertySet.Add(FolderSchema.ChildFolderCount);

            folderView.Traversal = FolderTraversal.Shallow;
            Folder imHistoryFolder = null;

            FindFoldersResults findFolderResults;
            bool foundImHistoryFolder = false;
            do
            {
                findFolderResults = this._service.FindFolders(WellKnownFolderName.MsgFolderRoot, folderView);
                foreach (Folder folder in findFolderResults)
                {
                    if (folder.DisplayName.ToLower() == "conversation history" || folder.DisplayName.ToLower() == "对话历史记录")
                    {
                        imHistoryFolder = folder;
                        foundImHistoryFolder = true;
                    }
                }
                folderView.Offset += _pageSize;
            } while (findFolderResults.MoreAvailable && !foundImHistoryFolder);

            return imHistoryFolder;
        }

 

posted @ 2017-09-28 10:55  wolfy  阅读(356)  评论(0编辑  收藏  举报