/// <summary> /// Выполняет поиск входящих писем с подтверждением об обработке файлов за указанный период /// </summary> /// <param name="begin">Дата начиная с которой будет происходить поиск писем</param> /// <returns></returns> public static ItemType[] GetMessages(DateTime begin) { ExchangeServiceBinding bind = new ExchangeServiceBinding(); bind.Credentials = new NetworkCredential(AppHelper.Configuration.Exchange.Username.GetDecryptedString(), AppHelper.Configuration.Exchange.Password.GetDecryptedString(), AppHelper.Configuration.Exchange.Domain); bind.Url = "https://" + AppHelper.Configuration.Exchange.ServerName + "/EWS/Exchange.asmx"; FindItemType findType = new FindItemType(); findType.Traversal = ItemQueryTraversalType.Shallow; findType.ItemShape = new ItemResponseShapeType(); findType.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties; DistinguishedFolderIdType folder = new DistinguishedFolderIdType(); folder.Id = DistinguishedFolderIdNameType.inbox; findType.ParentFolderIds = new BaseFolderIdType[] { folder }; IsEqualToType isEq = new IsEqualToType(); PathToUnindexedFieldType uPath = new PathToUnindexedFieldType(); uPath.FieldURI = UnindexedFieldURIType.messageFrom; FieldURIOrConstantType constType = new FieldURIOrConstantType(); ConstantValueType constValue = new ConstantValueType(); constValue.Value = AppHelper.Configuration.Exchange.SenderName; constType.Item = constValue; isEq.Item = uPath; isEq.FieldURIOrConstant = constType; IsGreaterThanOrEqualToType isGrEq = new IsGreaterThanOrEqualToType(); PathToUnindexedFieldType uPath2 = new PathToUnindexedFieldType(); uPath2.FieldURI = UnindexedFieldURIType.itemDateTimeSent; FieldURIOrConstantType constType2 = new FieldURIOrConstantType(); ConstantValueType constValue2 = new ConstantValueType(); constValue2.Value = string.Format("{0}-{1}-{2}T00:00:00Z", begin.Year, begin.Month.ToString("D2"), begin.Day.ToString("D2")); constType2.Item = constValue2; isGrEq.Item = uPath2; isGrEq.FieldURIOrConstant = constType2; AndType and = new AndType(); and.Items = new SearchExpressionType[] { isEq, isGrEq }; findType.Restriction = new RestrictionType(); findType.Restriction.Item = and; FindItemResponseType findResp = bind.FindItem(findType); FindItemResponseMessageType resMes = findResp.ResponseMessages.Items[0] as FindItemResponseMessageType; if (resMes.ResponseClass != ResponseClassType.Success) { throw new Exception("Ошибка при получении ответа от сервера Exchange:\n" + resMes.MessageText); } ItemType[] messages = (resMes.RootFolder.Item as ArrayOfRealItemsType).Items; return(messages); }
/// <summary> /// Log on to a mailbox with a specified user account and create a search folder. /// </summary> /// <param name="userName">Name of the user.</param> /// <param name="userPassword">Password of the user.</param> /// <param name="userDomain">Domain of the user.</param> /// <param name="searchFolderName">Name of the search folder.</param> /// <param name="searchText">Search text of the search folder.</param> /// <returns>If the search folder is created successfully, return true; otherwise, return false.</returns> public bool CreateSearchFolder(string userName, string userPassword, string userDomain, string searchFolderName, string searchText) { // Log on mailbox with specified user account(userName, userPassword, userDomain). bool isLoged = AdapterHelper.SwitchUser(userName, userPassword, userDomain, this.exchangeServiceBinding, this.Site); Site.Assert.IsTrue( isLoged, string.Format("Log on mailbox with the UserName: {0}, Password: {1}, Domain: {2} should be successful.", userName, userPassword, userDomain)); // Create the request. CreateFolderType createFolder = new CreateFolderType(); SearchFolderType[] folderArray = new SearchFolderType[1]; SearchFolderType searchFolder = new SearchFolderType(); // Use the following search filter to get all mail in the Inbox with the word searchText in the subject line. searchFolder.SearchParameters = new SearchParametersType(); searchFolder.SearchParameters.Traversal = SearchFolderTraversalType.Deep; searchFolder.SearchParameters.TraversalSpecified = true; searchFolder.SearchParameters.BaseFolderIds = new DistinguishedFolderIdType[4]; // Create a distinguished folder Identified of the inbox folder. DistinguishedFolderIdType inboxFolder = new DistinguishedFolderIdType(); inboxFolder.Id = new DistinguishedFolderIdNameType(); inboxFolder.Id = DistinguishedFolderIdNameType.inbox; searchFolder.SearchParameters.BaseFolderIds[0] = inboxFolder; DistinguishedFolderIdType contactType = new DistinguishedFolderIdType(); contactType.Id = new DistinguishedFolderIdNameType(); contactType.Id = DistinguishedFolderIdNameType.contacts; searchFolder.SearchParameters.BaseFolderIds[1] = contactType; DistinguishedFolderIdType calendarType = new DistinguishedFolderIdType(); calendarType.Id = new DistinguishedFolderIdNameType(); calendarType.Id = DistinguishedFolderIdNameType.calendar; searchFolder.SearchParameters.BaseFolderIds[2] = calendarType; DistinguishedFolderIdType taskType = new DistinguishedFolderIdType(); taskType.Id = new DistinguishedFolderIdNameType(); taskType.Id = DistinguishedFolderIdNameType.calendar; searchFolder.SearchParameters.BaseFolderIds[3] = taskType; // Use the following search filter. searchFolder.SearchParameters.Restriction = new RestrictionType(); PathToUnindexedFieldType path = new PathToUnindexedFieldType(); path.FieldURI = UnindexedFieldURIType.itemSubject; RestrictionType restriction = new RestrictionType(); FieldURIOrConstantType fieldURIORConstant = new FieldURIOrConstantType(); fieldURIORConstant.Item = new ConstantValueType(); (fieldURIORConstant.Item as ConstantValueType).Value = searchText; ExistsType isEqual = new ExistsType(); isEqual.Item = path; restriction.Item = isEqual; searchFolder.SearchParameters.Restriction = restriction; // Give the search folder a unique name. searchFolder.DisplayName = searchFolderName; folderArray[0] = searchFolder; // Create the search folder under the default Search Folder. TargetFolderIdType targetFolder = new TargetFolderIdType(); DistinguishedFolderIdType searchFolders = new DistinguishedFolderIdType(); searchFolders.Id = DistinguishedFolderIdNameType.searchfolders; targetFolder.Item = searchFolders; createFolder.ParentFolderId = targetFolder; createFolder.Folders = folderArray; bool isSearchFolderCreated = false; // Invoke CreateFolder operation and get the response. CreateFolderResponseType response = this.exchangeServiceBinding.CreateFolder(createFolder); if (response != null && ResponseClassType.Success == response.ResponseMessages.Items[0].ResponseClass) { // If the search folder is created successfully, return true; otherwise, return false. isSearchFolderCreated = true; searchFolder.FolderId = ((FolderInfoResponseMessageType)response.ResponseMessages.Items[0]).Folders[0].FolderId; AdapterHelper.CreatedFolders.Add(searchFolder); } return(isSearchFolderCreated); }
static FolderIdType FindFolder(ExchangeServiceBinding esb, DistinguishedFolderIdType fiFolderID, String fnFldName) { FolderIdType rvFolderID = new FolderIdType(); // Create the request and specify the travesal type FindFolderType findFolderRequest = new FindFolderType(); findFolderRequest.Traversal = FolderQueryTraversalType.Deep; // Define the properties returned in the response FolderResponseShapeType responseShape = new FolderResponseShapeType(); responseShape.BaseShape = DefaultShapeNamesType.Default; findFolderRequest.FolderShape = responseShape; // Identify which folders to search DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1]; folderIDArray[0] = new DistinguishedFolderIdType(); folderIDArray[0].Id = fiFolderID.Id; //Add Restriction for DisplayName RestrictionType ffRestriction = new RestrictionType(); IsEqualToType ieToType = new IsEqualToType(); PathToUnindexedFieldType diDisplayName = new PathToUnindexedFieldType(); diDisplayName.FieldURI = UnindexedFieldURIType.folderDisplayName; FieldURIOrConstantType ciConstantType = new FieldURIOrConstantType(); ConstantValueType cvConstantValueType = new ConstantValueType(); cvConstantValueType.Value = fnFldName; ciConstantType.Item = cvConstantValueType; ieToType.Item = diDisplayName; ieToType.FieldURIOrConstant = ciConstantType; ffRestriction.Item = ieToType; findFolderRequest.Restriction = ffRestriction; // Add the folders to search to the request findFolderRequest.ParentFolderIds = folderIDArray; try { // Send the request and get the response FindFolderResponseType findFolderResponse = esb.FindFolder(findFolderRequest); // Get the response messages ResponseMessageType[] rmta = findFolderResponse.ResponseMessages.Items; foreach (ResponseMessageType rmt in rmta) { // Cast to the correct response message type FindFolderResponseMessageType ffResponse = (FindFolderResponseMessageType)rmt; foreach (FolderType fFoundFolder in ffResponse.RootFolder.Folders) { rvFolderID = fFoundFolder.FolderId; } } } catch (Exception e) { string problem = e.Message; } return rvFolderID; }