c# Exchange 收件箱获取。

2021-07-04 04:28

阅读:457

标签:demo   ==   sum   levels   property   search   ges   vat   private   

        public List GetInbox()
        {
            try
            {

                List lstEmails = new List();

                FolderId folder = FindFolderIdByDisplayName(service, "L1 Support", WellKnownFolderName.Inbox);

                int offset = 0;
                int pageSize = 20;
                bool MoreItems = true;

                //search in pages
                while (MoreItems)
                {
                    ItemView view = new ItemView(20, 0, OffsetBasePoint.Beginning);
                    view.PropertySet = new PropertySet(BasePropertySet.IdOnly, ItemSchema.Subject, ItemSchema.DateTimeReceived);
                    view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
                    FindItemsResults findResults = service.FindItems(folder, SetFilter(), view);
                    foreach (Item myItem in findResults.Items)
                    {
                        //body message does not include in returned properties, get message details
                        Item item = Item.Bind(service, myItem.Id);
                        lstEmails.Add(new Email()
                        {

                            Body = item.Body.Text,
                            Mail_cc = item.DisplayCc,
                            Mail_from = item.LastModifiedName,
                            IsRead = item.IsNew,
                            Subject = item.Subject,
                            CreateOn = item.DateTimeCreated
                        });
                    }

                    //last page
                    if (!findResults.MoreAvailable)
                        MoreItems = false;

                    //next page
                    if (MoreItems)
                        offset += pageSize;
                }

                return lstEmails;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }
        public FolderId FindFolderIdByDisplayName(ExchangeService service, WellKnownFolderName SearchFolder)
        {
            // Specify the root folder to be searched.
            Folder rootFolder = Folder.Bind(service, SearchFolder);
            return rootFolder.Id;
        }
        public FolderId FindFolderIdByDisplayName(ExchangeService service, string DisplayName, WellKnownFolderName SearchFolder)
        {
            // Specify the root folder to be searched.
            Folder rootFolder = Folder.Bind(service, SearchFolder);

            // Loop through the child folders of the folder being searched.
            foreach (Folder folder in rootFolder.FindFolders(new FolderView(100)))
            {
                // If the display name of the current folder matches the specified display name, return the folder’s unique identifier.
                if (folder.DisplayName == DisplayName)
                {
                    return folder.Id;
                }
            }
            return null;
        }
        /// 
        /// 过滤器
        /// 
        /// 
        private static SearchFilter SetFilter()
        {


            SearchFilter SearchFilter1 = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "【NA Major Impact】 【P1】 System Down");
            SearchFilter SearchFilter2 = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "【NA Major Impact】 【P2】 System Slow");
            SearchFilter SearchFilter3 = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "【Brazil Major Impact】 【P1】 System Down");
            SearchFilter SearchFilter4 = new SearchFilter.ContainsSubstring(ItemSchema.Subject, "【Brazil Major Impact】 【P2】 System Slow");

            //筛选今天的邮件
            SearchFilter start = new SearchFilter.IsGreaterThanOrEqualTo(EmailMessageSchema.DateTimeCreated, Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd 00:00:00")));
            SearchFilter end = new SearchFilter.IsLessThanOrEqualTo(EmailMessageSchema.DateTimeCreated, Convert.ToDateTime(DateTime.Now.ToString()));

            SearchFilter IsRead = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false);

            SearchFilter.SearchFilterCollection secondLevelSearchFilterCollection1 = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
                                                                                                                           start,
                                                                                                                           end);

            SearchFilter.SearchFilterCollection secondLevelSearchFilterCollection2 = new SearchFilter.SearchFilterCollection(LogicalOperator.Or,
                                                                                                                         SearchFilter1, SearchFilter2, SearchFilter3, SearchFilter4);

            SearchFilter.SearchFilterCollection firstLevelSearchFilterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
                                                                                                                               secondLevelSearchFilterCollection1,
                                                                                                                               secondLevelSearchFilterCollection2);


            return firstLevelSearchFilterCollection;
        }

写这个给自己Mark 一下, 关键是这个多条件筛选,同时有OR 和AND 条件,网上资料很少,从微软官网的 Demo 里找到案例。

链接如下

https://code.msdn.microsoft.com/Exchange-2013-101-Code-3c38582c

 

c# Exchange 收件箱获取。

标签:demo   ==   sum   levels   property   search   ges   vat   private   

原文地址:https://www.cnblogs.com/tuenbo/p/9870396.html


评论


亲,登录后才可以留言!