4

我正在尝试从收件箱文件夹中搜索两个日期之间的邮件项目。我使用以下限制类型,但它会引发此错误:

Firmt.RootFolder = null

我究竟做错了什么?

在提及的日期之间有一些消息;-)

感谢您的建议。

        using (ExchangeServiceBinding esb = new ExchangeServiceBinding())
        {
            esb.Url = ConfigurationManager.AppSettings["ExchangeWebServicesURL"].ToString();
            esb.RequestServerVersionValue = new RequestServerVersion();
            esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2007_SP1;
            esb.PreAuthenticate = true;
            esb.Credentials = new NetworkCredential(email, password);
            FindItemType findItemRequest = new FindItemType();
            // paging
            IndexedPageViewType ipvt = new IndexedPageViewType();
            ipvt.BasePoint = IndexBasePointType.Beginning;
            ipvt.MaxEntriesReturned = nombreMessage;
            ipvt.MaxEntriesReturnedSpecified = true;
            ipvt.Offset = offset;
            findItemRequest.Item = ipvt;
            // filter by dates
            AndType andType = new AndType();
            List<SearchExpressionType> searchExps = new List<SearchExpressionType>();
            RestrictionType restriction = new RestrictionType();
            PathToUnindexedFieldType pteft = new PathToUnindexedFieldType
            {
                FieldURI = UnindexedFieldURIType.itemDateTimeSent
            };
            IsGreaterThanOrEqualToType IsGreaterThanOrEqualTo = new IsGreaterThanOrEqualToType
            {
                Item = pteft,
                FieldURIOrConstant = new FieldURIOrConstantType
                {
                    Item = new ConstantValueType
                    {
                        Value = DateTime.Today.AddDays(-6d).ToString()
                    }
                }
            };
            searchExps.Add(IsGreaterThanOrEqualTo);

            IsLessThanOrEqualToType IsLessThanOrEqualTo = new IsLessThanOrEqualToType
            {
                Item = pteft,
                FieldURIOrConstant = new FieldURIOrConstantType
                {
                    Item = new ConstantValueType
                    {
                        Value = DateTime.Today.AddDays(1d).ToString()
                    }
                }
            };
            searchExps.Add(IsLessThanOrEqualTo);
            andType.Items = searchExps.ToArray();
            restriction.Item = andType;
            findItemRequest.Restriction = restriction;
            //// Define the sort order of items.
            FieldOrderType[] fieldsOrder = new FieldOrderType[1];
            fieldsOrder[0] = new FieldOrderType();
            PathToUnindexedFieldType dateOrder = new PathToUnindexedFieldType
            {
                FieldURI = UnindexedFieldURIType.itemDateTimeReceived
            };
            fieldsOrder[0].Item = dateOrder;
            fieldsOrder[0].Order = SortDirectionType.Descending;
            findItemRequest.SortOrder = fieldsOrder;
            findItemRequest.Traversal = ItemQueryTraversalType.Shallow;
            // define which item properties are returned in the response 
            findItemRequest.ItemShape = new ItemResponseShapeType
            {
                BaseShape = DefaultShapeNamesType.IdOnly
            };
            // identify which folder to search 
            DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
            folderIDArray[0] = new DistinguishedFolderIdType { Id = DistinguishedFolderIdNameType.inbox };
            // add folders to request 
            findItemRequest.ParentFolderIds = folderIDArray;
            // find the messages 
            FindItemResponseType findItemResponse = esb.FindItem(findItemRequest);
            //-------------
            ArrayOfResponseMessagesType responseMessages = findItemResponse.ResponseMessages;
            ResponseMessageType responseMessage = responseMessages.Items[0];
            if (responseMessage is FindItemResponseMessageType)
            {
                FindItemResponseMessageType firmt = (responseMessage as FindItemResponseMessageType);
         *******FindItemParentType fipt = firmt.RootFolder;********
                object obj = fipt.Item;
                // FindItem contains an array of items.
                ArrayOfRealItemsType realitems = (obj as ArrayOfRealItemsType);
                ItemType[] items = realitems.Items;
                // if no messages were found, then return null -- we're done 
                if (items == null || items.Count() <= 0)
                    return null;
                // FindItem never gets "all" the properties, so now that we've found them all, we need to get them all. 
                BaseItemIdType[] itemIds = new BaseItemIdType[items.Count()];
                for (int i = 0; i < items.Count(); i++)
                    itemIds[i] = items[i].ItemId;
                GetItemType getItemType = new GetItemType
                {
                    ItemIds = itemIds,
                    ItemShape = new ItemResponseShapeType
                    {
                        BaseShape = DefaultShapeNamesType.AllProperties,
                        BodyType = BodyTypeResponseType.Text,
                        BodyTypeSpecified = true,
                        AdditionalProperties = new BasePathToElementType[] { 
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.itemDateTimeSent },
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageFrom },
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageIsRead },
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageSender },
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageToRecipients },
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageCcRecipients },
                            new PathToUnindexedFieldType { FieldURI = UnindexedFieldURIType.messageBccRecipients }
                        }
                    }
                };
                GetItemResponseType getItemResponse = esb.GetItem(getItemType);
                messages = ReadItems(getItemResponse, items.Count());
            }
4

3 回答 3

4

经过长时间搜索日期格式后,我自己找到了答案。

限制必须定义为:

// greater or equal to 
string dateStart = DateTime.Today.add(-6d);
string dateEnd = DateTime.Today.Add(1d);

PathToUnindexedFieldType dateSentPath = new PathToUnindexedFieldType();
dateSentPath.FieldURI = UnindexedFieldURIType.itemDateTimeSent;
IsGreaterThanOrEqualToType IsGreaterThanOrEqual = new IsGreaterThanOrEqualToType();
IsGreaterThanOrEqual.Item = dateSentPath;
FieldURIOrConstantType dateConstant = new FieldURIOrConstantType();
ConstantValueType dateConstantValue = new ConstantValueType();
dateConstantValue.Value = string.Format("{0}-{1}-{2}T00:00:00Z", dateStart.Year.ToString(), dateStart.Month.ToString(), dateStart.Day.ToString());
dateConstant.Item = dateConstantValue;
IsGreaterThanOrEqual.FieldURIOrConstant = dateConstant;

// less than or equal to
PathToUnindexedFieldType dateSentPath1 = new PathToUnindexedFieldType();
dateSentPath1.FieldURI = UnindexedFieldURIType.itemDateTimeSent;
IsLessThanOrEqualToType lessThanOrEqualTo = new IsLessThanOrEqualToType();
lessThanOrEqualTo.Item = dateSentPath1;
FieldURIOrConstantType dateConstant1 = new FieldURIOrConstantType();
ConstantValueType dateConstantValue1 = new ConstantValueType();
dateConstantValue1.Value = string.Format("{0}-{1}-{2}T00:00:00Z", dateEnd.Year.ToString(), dateEnd.Month.ToString(), dateEnd.Day.ToString());
dateConstant1.Item = dateConstantValue1;
lessThanOrEqualTo.FieldURIOrConstant = dateConstant1;

RestrictionType restriction = new RestrictionType();
AndType andType = new AndType();
andType.Items = new SearchExpressionType[] { lessThanOrEqualTo, IsGreaterThanOrEqual };
restriction.Item = andType;
findItemRequest.Restriction = restriction;

希望有一天这对某人有所帮助;-)

于 2010-03-31T07:14:02.533 回答
0

万一将来有人偶然发现这一点,EWS 对日期格式的要求更加严格。接受的答案格式适用于 2 位数月份,但不适用于个位数月份。

适用于所有情况的格式是:

DateTime.Today.AddDays(15).ToString("yyyy-MM-ddThh:mm:ssZ")
于 2014-07-23T17:50:46.930 回答
0

该限制也适用于“可排序日期/时间模式”。

Datetime.Now.ToString("s")
于 2016-03-25T18:32:33.147 回答