Fetching emails for a specific date in c# using Exchange Web Services

血红的双手。 提交于 2020-01-23 05:48:06

问题


I am able to fetch emails from a mailbox based on a subject. I am not sure what the format for fetching emails based on the received date?

           string message = string.Empty;
            Item item = Item.Bind(exService, messageID, PropertySet.FirstClassProperties);

            if (item is EmailMessage)
            {
                EmailMessage em = (EmailMessage)item;

                string strMsg = string.Empty;
                //strMsg = strMsg + item.Id.ToString();
                //strMsg = strMsg + item.DateTimeReceived;
                strMsg = strMsg + "*********************** New Fiscal Email received on " + item.DateTimeReceived  +" ************************************" + Environment.NewLine;

                if (em.Body.Text.Contains("BRANDON"))
                {
                    strMsg = strMsg + em.Body.Text.ToString();
                }
                strMsg = strMsg + "*********************** End of Email Body ************************************" + Environment.NewLine;
                message = strMsg;

            }

回答1:


I think the way SilverNinja told you is the right way. You should search the items like this:

DateTime searchdate = new DateTime (2012,7,6) //Year, month, day
SearchFilter greaterthanfilter = new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, searchdate );
SearchFilter lessthanfilter = new SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, searchdate.AddDays(1));
SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, greaterthanfilter, lessthanfilter);
Folder folder = Folder.Bind(this.m_Service, WellKnownFolderName.MsgFolderRoot); //Or the folder you want to search in
FindItemsResults<Item> results = folder.FindItems(filter, new ItemView(1000));

"results.Items" will return the first 1000 items which are recivied at the day you are looking for.




回答2:


Take a look at SearchFilter examples. You just need a filtering condition on ItemSchema.DateTimeReceived




回答3:


This will work.

if (em.DateTimeReceived.Equals(**Date you want to search**))
                {
                    strMsg = strMsg + em.Body.Text.ToString();
                }


来源:https://stackoverflow.com/questions/10980069/fetching-emails-for-a-specific-date-in-c-sharp-using-exchange-web-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!