public void IsLessThan()
        {
            SearchFilter filter = new SearchFilter.IsLessThan(
                MessageObjectSchema.ReceivedDateTime,
                "19-02-2019");

            Assert.AreEqual(
                filter.FilterOperator,
                FilterOperator.lt);

            Assert.AreEqual(
                "$filter=ReceivedDateTime lt 19-02-2019"
                , filter.Query);
        }
예제 #2
0
        /// <summary>
        /// Create a search filter for filtering items based on equality comparisons of property values.
        /// </summary>
        /// <param name="service">An ExchangeService object with credentials and the EWS URL.</param>
        private static void UseAnEqualitySearchFilter(ExchangeService service)
        {
            // The IsGreaterThan filter determines whether the value of a property is greater than a specific value.
            // This filter instance filters on the DateTimeReceived property, where the value is greater than a month ago.
            SearchFilter.IsGreaterThan isGreaterThan = new SearchFilter.IsGreaterThan(EmailMessageSchema.DateTimeReceived, DateTime.Now.AddMonths(-1));

            // The IsGreaterThanOrEqualTo filter determines whether the value of a property is greater than or equal to a specific value.
            // This filter instance filters on the DateTimeCreated property, where the value is greater than or equal to a week ago.
            SearchFilter.IsGreaterThanOrEqualTo isGreaterThanOrEqualTo = new SearchFilter.IsGreaterThanOrEqualTo(EmailMessageSchema.DateTimeCreated, DateTime.Now.AddDays(-7));

            // The IsLessThan filter determines whether the value of a property is less than a specific value.
            // This filter instance filters on the DateTimeReceived property, where the value is less than the time an hour ago.
            SearchFilter.IsLessThan isLessThan = new SearchFilter.IsLessThan(EmailMessageSchema.DateTimeReceived, DateTime.Now.AddHours(-1));

            // The IsLessThanOrEqualTo filter determines whether the value of a property is less than or equal to a specific value.
            // This filter instance filters on the DateTimeCreated property, where the value is less than or equal to the time two days ago.
            SearchFilter.IsLessThanOrEqualTo isLessThanOrEqualTo = new SearchFilter.IsLessThanOrEqualTo(EmailMessageSchema.DateTimeCreated, DateTime.Now.AddDays(-2));

            // The IsEqualTo filter determines whether the value of a property is equal to a specific value.
            // This filter instance filters on the Importance property where it is set to Normal.
            SearchFilter.IsEqualTo isEqualTo = new SearchFilter.IsEqualTo(EmailMessageSchema.Importance, Importance.Normal);

            // The IsNotEqualTo filter determines whether the value of a property is not equal to a specific value.
            // This filter instance filters on the IsRead property, where it is not set to true.
            SearchFilter.IsNotEqualTo isNotEqualTo = new SearchFilter.IsNotEqualTo(EmailMessageSchema.IsRead, true);

            // Create a search filter collection that will filter based on an item's Importance and IsRead flag.
            // Both conditions must pass for an item to be returned in a result set.
            SearchFilter.SearchFilterCollection secondLevelSearchFilterCollection1 = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
                                                                                                                             isEqualTo,
                                                                                                                             isNotEqualTo);

            // Create a search filter collection that will filter based on an item's DateTimeCreated and DateTimeReceived properties.
            // All four conditions must pass for an item to be returned in a result set.
            SearchFilter.SearchFilterCollection secondLevelSearchFilterCollection2 = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
                                                                                                                             isGreaterThan,
                                                                                                                             isGreaterThanOrEqualTo,
                                                                                                                             isLessThan,
                                                                                                                             isLessThanOrEqualTo);

            // The SearchFilterCollection contains a collection of search filter collections. Items must pass the search conditions
            // of either collection for an item to be returned in a result set.
            SearchFilter.SearchFilterCollection firstLevelSearchFilterCollection = new SearchFilter.SearchFilterCollection(LogicalOperator.Or,
                                                                                                                           secondLevelSearchFilterCollection1,
                                                                                                                           secondLevelSearchFilterCollection2);


            // Create a nonpaged view and add properties to the results set.
            ItemView view = new ItemView(10);

            view.PropertySet = new PropertySet(EmailMessageSchema.Subject,
                                               EmailMessageSchema.DateTimeCreated,
                                               EmailMessageSchema.DateTimeReceived,
                                               EmailMessageSchema.Importance,
                                               EmailMessageSchema.IsRead);

            try
            {
                // Search the Inbox based on the ItemView and the SearchFilterCollection. This results in a FindItem operation call
                // to EWS.
                FindItemsResults <Item> results = service.FindItems(WellKnownFolderName.Inbox,
                                                                    firstLevelSearchFilterCollection,
                                                                    view);

                foreach (Item item in results.Items)
                {
                    Console.WriteLine("\r\nSubject:\t\t{0}", item.Subject);
                    Console.WriteLine("DateTimeCreated:\t{0}", item.DateTimeCreated.ToShortDateString());
                    Console.WriteLine("DateTimeReceived:\t{0}", item.DateTimeReceived.ToShortDateString());
                    Console.WriteLine("Importance:\t\t{0}", item.Importance.ToString());
                    Console.WriteLine("IsRead:\t\t\t{0}", (item as EmailMessage).IsRead.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
            }
        }
예제 #3
0
        public void ExChangeMail()
        {
            ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010);

            service.Credentials = new WebCredentials(UserName, Password);
            //给出Exchange Server的URL
            service.Url = new Uri(ServerUrl);
            //你自己的邮件地址 [email protected]
            service.AutodiscoverUrl(Email, RedirectionCallback);
            //已读条件
            SearchFilter isRead = new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, true);
            //日期条件
            SearchFilter timeLimit        = new SearchFilter.IsGreaterThan(EmailMessageSchema.DateTimeSent, Convert.ToDateTime("2019/9/26 00:00"));
            SearchFilter timeLimitMaxTime = new SearchFilter.IsLessThan(EmailMessageSchema.DateTimeSent, Convert.ToDateTime("2019/10/25 23:59"));

            //复合条件
            SearchFilter.SearchFilterCollection searchlist = new SearchFilter.SearchFilterCollection(LogicalOperator.And, isRead, timeLimit, timeLimitMaxTime);
            //查找Inbox,加入过滤器条件,结果10条
            var IV = new ItemView(int.MaxValue);


            //MsgFolderRoot 所有邮件文件夹 ! 从所有文件夹中 找到 test文件夹
            //FolderId folder = FindFolderIdByDisplayName(service, "test", WellKnownFolderName.MsgFolderRoot);
            //DeletedItems:已删除邮件   InBox:收件箱
            //FindItemsResults<Item> findResults = service.FindItems(folder, sf, IV);

            FindItemsResults <Item> findResults = service.FindItems(WellKnownFolderName.DeletedItems, searchlist, IV);
            PropertySet             props       = new PropertySet(BasePropertySet.IdOnly);

            props.Add(ItemSchema.Subject);
            props.Add(ItemSchema.Body);

            List <NewTempModel> list = new List <NewTempModel>();

            if (findResults != null && findResults.Items != null && findResults.Items.Count > 0)
            {
                var i = 1;

                foreach (Item item in findResults.Items)
                {
                    var total = findResults.Items.Count();

                    EmailMessage email = EmailMessage.Bind(service, item.Id);
                    props.RequestedBodyType = BodyType.Text;
                    EmailMessage emailNoHtml = EmailMessage.Bind(service, item.Id, props);
                    string       emailText   = emailNoHtml.Body.Text;



                    //TempleModel tm = new TempleModel();
                    //tm.UserName = email.Sender == null ? "" : email.Sender.Name;
                    //tm.Description = emailText;
                    //tm.UserDate = email.DateTimeSent.ToShortDateString();
                    //tm.FixDate = tm.UserDate;
                    //tm.ComfiDate = tm.FixDate;
                    //tm.Month = $"{email.DateTimeSent.Year}/{email.DateTimeSent.Month}";
                    //tm = SwithCategory(tm);

                    NewTempModel tm = new NewTempModel();
                    tm.Requestor_Email   = email.Sender == null ? "" : email.Sender.Address;
                    tm.Request_Date_Time = email.DateTimeCreated.ToString();
                    tm.Issue_Description = emailText;
                    tm.Closure_Date_Time = email.LastModifiedTime.ToString();
                    tm.Application       = "F3";
                    tm.Severity_Level    = "P1";
                    tm.Feedback_Source   = "邮箱";
                    tm.Impacted_BU       = "F3";
                    tm.Category          = "资讯类";
                    tm.Sub_Category      = "后台处理";
                    tm.Caused_By         = "F3";
                    tm.Resolution_Detail = "后台处理数据";
                    tm.Description       = "转交任务";
                    tm.Status            = "Closed";
                    tm.Resolver          = "alvin";
                    tm.SLA_Meet          = "Yes";
                    //tm.Closure_Date_Time=email.to

                    if (tm != null)
                    {
                        list.Add(tm);
                        Console.WriteLine(emailText);
                        Console.WriteLine("-------------------------" + i + "/" + total + "down!--------------------------------");
                    }
                    //list.Add(tm);
                    i++;
                }
            }
            DataTable dt = ToDataTable(list);

            string path = @"C:\Users\Administrator\Desktop\wow1\1.xlsx";

            ExcelHelper.DataTableToExcel(path, dt, "Incident Pattern", false);
            Console.ReadKey();
        }