public static SwitchUser ( string name, string userPassword, string userDomain, |
||
name | string | Name of the user. |
userPassword | string | Password of the user. |
userDomain | string | Domain of the user. |
exchangeServiceBinding | An instance of Service Binding | |
site | ITestSite | An instance of ITestSite |
Результат | bool |
/// <summary> /// Log on to a mailbox with a specified user account and delete all the items and subfolders from Inbox, Sent Items, Calendar, Contacts, Tasks, Deleted Items and Search Folders if any. /// </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> /// <returns>If the mailbox is cleaned up successfully, return true; otherwise, return false.</returns> public bool CleanupMailBox(string userName, string userPassword, string userDomain) { // Switch to specified user mailbox. bool isSwitched = AdapterHelper.SwitchUser(userName, userPassword, userDomain, this.exchangeServiceBinding, this.Site); Site.Assert.IsTrue( isSwitched, string.Format("Logon with the UserName: {0}, Password: {1}, Domain: {2} should be successful.", userName, userPassword, userDomain)); bool isCleaned = false; if ( this.CleanupFolder(DistinguishedFolderIdNameType.inbox) && this.CleanupFolder(DistinguishedFolderIdNameType.sentitems) && this.CleanupFolder(DistinguishedFolderIdNameType.calendar) && this.CleanupFolder(DistinguishedFolderIdNameType.contacts) && this.CleanupFolder(DistinguishedFolderIdNameType.tasks) && this.CleanupFolder(DistinguishedFolderIdNameType.deleteditems) && this.CleanupFolder(DistinguishedFolderIdNameType.searchfolders)) { isCleaned = true; } return(isCleaned); }
/// <summary> /// Log on to a mailbox with a specified user account and find the specified folder, then delete it if it is found. /// </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="parentFolderName">Name of the parent folder.</param> /// <param name="subFolderName">Name of the folder which will be updated.</param> /// <returns>If the folder is deleted successfully, return true; otherwise, return false.</returns> public bool FindAndDeleteSubFolder(string userName, string userPassword, string userDomain, string parentFolderName, string subFolderName) { // Switch to specified user mailbox. bool isSwitched = AdapterHelper.SwitchUser(userName, userPassword, userDomain, this.exchangeServiceBinding, this.Site); Site.Assert.IsTrue( isSwitched, string.Format("Log on mailbox with the UserName: {0}, Password: {1}, Domain: {2} should be successful.", userName, userPassword, userDomain)); // Parse the parent folder name to DistinguishedFolderIdNameType. DistinguishedFolderIdNameType parentFolderIdName = (DistinguishedFolderIdNameType)Enum.Parse(typeof(DistinguishedFolderIdNameType), parentFolderName, true); DeleteFolderType deleteFolderRequest = new DeleteFolderType(); deleteFolderRequest.DeleteType = DisposalType.HardDelete; deleteFolderRequest.FolderIds = new BaseFolderIdType[1]; deleteFolderRequest.FolderIds[0] = this.FindSubFolder(parentFolderIdName, subFolderName); // Invoke the DeleteFolder operation and get the response. DeleteFolderResponseType deleteFolderResponse = this.exchangeServiceBinding.DeleteFolder(deleteFolderRequest); bool isDeleted = false; if (deleteFolderResponse != null && deleteFolderResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success) { // If the DeleteFolder operation succeeds, return true. isDeleted = true; } return(isDeleted); }
/// <summary> /// Log on to a mailbox with a specified user account and check whether the specified calendar item is cancelled or not. /// </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="folderName">Name of the folder which should be searched for the specified meeting message.</param> /// <param name="itemSubject">Subject of the meeting message which should exist.</param> /// <returns>If the specified calendar item exists and is canceled, return true, otherwise return false.</returns> public bool IsCalendarItemCanceled(string userName, string userPassword, string userDomain, string folderName, string itemSubject) { // Parse the parent folder name to DistinguishedFolderIdNameType. DistinguishedFolderIdNameType parentFolderIdName = (DistinguishedFolderIdNameType)Enum.Parse(typeof(DistinguishedFolderIdNameType), folderName, true); // Switch to specified user mailbox. bool isSwitched = AdapterHelper.SwitchUser(userName, userPassword, userDomain, this.exchangeServiceBinding, this.Site); Site.Assert.IsTrue( isSwitched, string.Format("Log on mailbox with the UserName: {0}, Password: {1}, Domain: {2} should be successful.", userName, userPassword, userDomain)); // Loop to find the meeting message ItemType item = this.LoopToFindItem(parentFolderIdName, itemSubject, Item.CalendarItem); if (item != null) { CalendarItemType calendarItem = item as CalendarItemType; // If the IsCancelled property of the item is true, return true if (calendarItem.IsCancelled == true) { return(true); } else { return(false); } } else { return(false); } }
/// <summary> /// Log on to a mailbox with a specified user account and find the specified item then delete it. /// </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="folderName">Name of the folder which should be searched for the specified item.</param> /// <param name="itemSubject">Subject of the item which should be deleted.</param> /// <param name="itemType">Type of the item which should be deleted.</param> /// <returns>If the specified item is deleted successfully, return true; otherwise, return false.</returns> public bool FindAndDeleteItem(string userName, string userPassword, string userDomain, string folderName, string itemSubject, string itemType) { // Switch to specified user mailbox. bool isSwitched = AdapterHelper.SwitchUser(userName, userPassword, userDomain, this.exchangeServiceBinding, this.Site); Site.Assert.IsTrue( isSwitched, string.Format("Log on mailbox with the UserName: {0}, Password: {1}, Domain: {2} should be successful.", userName, userPassword, userDomain)); // Parse the parent folder name to DistinguishedFolderIdNameType. DistinguishedFolderIdNameType parentFolderIdName = (DistinguishedFolderIdNameType)Enum.Parse(typeof(DistinguishedFolderIdNameType), folderName, true); Item item = (Item)Enum.Parse(typeof(Item), itemType, true); ItemType type = this.LoopToFindItem(parentFolderIdName, itemSubject, item); bool isDeleted = false; if (type != null) { DeleteItemType deleteItemRequest = new DeleteItemType(); deleteItemRequest.ItemIds = new BaseItemIdType[] { type.ItemId }; // Invoke the delete item operation and get the response. DeleteItemResponseType response = this.exchangeServiceBinding.DeleteItem(deleteItemRequest); if (response != null && response.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success) { // If delete operation succeeds, return true isDeleted = true; } } return(isDeleted); }
/// <summary> /// Log on to a mailbox with a specified user account and find the specified meeting message in the Inbox folder, then accept it. /// </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="itemSubject">Subject of the meeting message which should be accepted.</param> /// <param name="itemType">Type of the item which should be accepted.</param> /// <returns>If the specified meeting message is accepted successfully, return true; otherwise, return false.</returns> public bool FindAndAcceptMeetingMessage(string userName, string userPassword, string userDomain, string itemSubject, string itemType) { // Define the Inbox folder as parent folder. DistinguishedFolderIdNameType parentFolderIdName = DistinguishedFolderIdNameType.inbox; // Switch to specified user mailbox. bool isSwitched = AdapterHelper.SwitchUser(userName, userPassword, userDomain, this.exchangeServiceBinding, this.Site); Site.Assert.IsTrue( isSwitched, string.Format("Log on mailbox with the UserName: {0}, Password: {1}, Domain: {2} should be successful.", userName, userPassword, userDomain)); Item item = (Item)Enum.Parse(typeof(Item), itemType, true); // Loop to find the specified item in the specified folder. ItemType type = this.LoopToFindItem(parentFolderIdName, itemSubject, item); bool isAccepted = false; if (type != null) { MeetingRequestMessageType message = type as MeetingRequestMessageType; // Create a request for the CreateItem operation. CreateItemType createItemRequest = new CreateItemType(); // Add the CalendarItemType item to the items to be created. createItemRequest.Items = new NonEmptyArrayOfAllItemsType(); // Create an AcceptItemType item to reply to a meeting request. AcceptItemType acceptItem = new AcceptItemType(); // Set the related meeting request. acceptItem.ReferenceItemId = message.ItemId; createItemRequest.Items.Items = new ItemType[] { acceptItem }; // Set the MessageDisposition property to SendOnly. createItemRequest.MessageDisposition = MessageDispositionType.SendOnly; createItemRequest.MessageDispositionSpecified = true; // Invoke the CreateItem operation. CreateItemResponseType createItemResponse = this.exchangeServiceBinding.CreateItem(createItemRequest); if (createItemResponse != null && createItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Success) { isAccepted = true; } } return(isAccepted); }
/// <summary> /// Log on to a mailbox with a specified user account and find the specified folder then update the folder name of it. /// </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="parentFolderName">Name of the parent folder.</param> /// <param name="currentFolderName">Current name of the folder which will be updated.</param> /// <param name="newFolderName">New name of the folder which will be updated to.</param> /// <returns>If the name of the folder is updated successfully, return true; otherwise, return false.</returns> public bool FindAndUpdateFolderName(string userName, string userPassword, string userDomain, string parentFolderName, string currentFolderName, string newFolderName) { // Switch to specified user mailbox. bool isSwitched = AdapterHelper.SwitchUser(userName, userPassword, userDomain, this.exchangeServiceBinding, this.Site); Site.Assert.IsTrue( isSwitched, string.Format("Log on mailbox with the UserName: {0}, Password: {1}, Domain: {2} should be successful.", userName, userPassword, userDomain)); // Parse the parent folder name to DistinguishedFolderIdNameType. DistinguishedFolderIdNameType parentFolderIdName = (DistinguishedFolderIdNameType)Enum.Parse(typeof(DistinguishedFolderIdNameType), parentFolderName, true); // Create UpdateFolder request UpdateFolderType updateFolderRequest = new UpdateFolderType(); updateFolderRequest.FolderChanges = new FolderChangeType[1]; updateFolderRequest.FolderChanges[0] = new FolderChangeType(); updateFolderRequest.FolderChanges[0].Item = this.FindSubFolder(parentFolderIdName, currentFolderName); // Identify the field to update and the value to set for it. SetFolderFieldType displayName = new SetFolderFieldType(); PathToUnindexedFieldType displayNameProp = new PathToUnindexedFieldType(); displayNameProp.FieldURI = UnindexedFieldURIType.folderDisplayName; FolderType updatedFolder = new FolderType(); updatedFolder.DisplayName = newFolderName; displayName.Item = displayNameProp; updatedFolder.DisplayName = newFolderName; displayName.Item1 = updatedFolder; // Add a single element into the array of changes. updateFolderRequest.FolderChanges[0].Updates = new FolderChangeDescriptionType[1]; updateFolderRequest.FolderChanges[0].Updates[0] = displayName; bool isFolderNameUpdated = false; // Invoke the UpdateFolder operation and get the response. UpdateFolderResponseType updateFolderResponse = this.exchangeServiceBinding.UpdateFolder(updateFolderRequest); if (updateFolderResponse != null && ResponseClassType.Success == updateFolderResponse.ResponseMessages.Items[0].ResponseClass) { isFolderNameUpdated = true; } return(isFolderNameUpdated); }
/// <summary> /// Log on to a mailbox with a specified user account and create two different-level subfolders in the specified parent 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="parentFolderName">Name of the parent folder.</param> /// <param name="firstLevelSubFolderName">Name of the first level sub folder which will be created under the parent folder.</param> /// <param name="secondLevelSubFolderName">Name of the second level sub folder which will be created under the first level sub folder.</param> /// <returns>If the two level sub folders are created successfully, return true; otherwise, return false.</returns> public bool CreateSubFolders(string userName, string userPassword, string userDomain, string parentFolderName, string firstLevelSubFolderName, string secondLevelSubFolderName) { // 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)); // Initialize variables FolderIdType folderId = null; CreateFolderType createFolderRequest = new CreateFolderType(); string folderClassName = null; DistinguishedFolderIdNameType parentFolderIdName = (DistinguishedFolderIdNameType)Enum.Parse(typeof(DistinguishedFolderIdNameType), parentFolderName, true); // Define different folder class name according to different parent folder. switch (parentFolderIdName) { case DistinguishedFolderIdNameType.inbox: folderClassName = "IPF.Note"; break; case DistinguishedFolderIdNameType.contacts: folderClassName = "IPF.Contact"; break; case DistinguishedFolderIdNameType.calendar: folderClassName = "IPF.Appointment"; break; case DistinguishedFolderIdNameType.tasks: folderClassName = "IPF.Task"; break; default: Site.Assume.Fail(string.Format("The parent folder name '{0}' is invalid. Valid values are: inbox, contacts, calendar or tasks.", parentFolderName)); break; } // Set parent folder ID. createFolderRequest.ParentFolderId = new TargetFolderIdType(); DistinguishedFolderIdType parentFolder = new DistinguishedFolderIdType(); parentFolder.Id = parentFolderIdName; createFolderRequest.ParentFolderId.Item = parentFolder; // Set Display Name and Folder Class for the folder to be created. FolderType folderProperties = new FolderType(); folderProperties.DisplayName = firstLevelSubFolderName; folderProperties.FolderClass = folderClassName; createFolderRequest.Folders = new BaseFolderType[1]; createFolderRequest.Folders[0] = folderProperties; bool isSubFolderCreated = false; // Invoke CreateFolder operation and get the response. CreateFolderResponseType createFolderResponse = this.exchangeServiceBinding.CreateFolder(createFolderRequest); if (createFolderResponse != null && ResponseClassType.Success == createFolderResponse.ResponseMessages.Items[0].ResponseClass) { // If the first level sub folder is created successfully, save the folder ID of it. folderId = ((FolderInfoResponseMessageType)createFolderResponse.ResponseMessages.Items[0]).Folders[0].FolderId; FolderType created = new FolderType() { DisplayName = folderProperties.DisplayName, FolderClass = folderClassName, FolderId = folderId }; AdapterHelper.CreatedFolders.Add(created); } // Create another sub folder under the created folder above. if (folderId != null) { createFolderRequest.ParentFolderId.Item = folderId; folderProperties.DisplayName = secondLevelSubFolderName; createFolderResponse = this.exchangeServiceBinding.CreateFolder(createFolderRequest); if (createFolderResponse != null && ResponseClassType.Success == createFolderResponse.ResponseMessages.Items[0].ResponseClass) { // If the two level sub folders are created successfully, return true; otherwise, return false. isSubFolderCreated = true; folderId = ((FolderInfoResponseMessageType)createFolderResponse.ResponseMessages.Items[0]).Folders[0].FolderId; FolderType created = new FolderType() { DisplayName = folderProperties.DisplayName, FolderClass = folderClassName, FolderId = folderId }; AdapterHelper.CreatedFolders.Add(created); } } return(isSubFolderCreated); }
/// <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); }
/// <summary> /// Log on a mailbox with a specified user account and check whether the specified item exists. /// </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="folderName">Name of the folder which should be searched for the specified item.</param> /// <param name="itemSubject">Subject of the item which should exist.</param> /// <param name="itemType">Type of the item which should exist.</param> /// <returns>If the item exists, return true; otherwise, return false.</returns> public bool IsItemExisting(string userName, string userPassword, string userDomain, string folderName, string itemSubject, string itemType) { bool isExisting = false; // Parse the parent folder name to DistinguishedFolderIdNameType. DistinguishedFolderIdNameType parentFolderIdName = (DistinguishedFolderIdNameType)Enum.Parse(typeof(DistinguishedFolderIdNameType), folderName, true); // Switch to specified user mailbox. bool isSwitched = AdapterHelper.SwitchUser(userName, userPassword, userDomain, this.exchangeServiceBinding, this.Site); Site.Assert.IsTrue( isSwitched, string.Format("Log on mailbox with the UserName: {0}, Password: {1}, Domain: {2} should be successful.", userName, userPassword, userDomain)); Item item = (Item)Enum.Parse(typeof(Item), itemType, true); // Loop to find the specified item ItemType type = this.LoopToFindItem(parentFolderIdName, itemSubject, item); if (type != null) { switch (item) { case Item.MeetingRequest: MeetingRequestMessageType requestMessage = type as MeetingRequestMessageType; if (requestMessage != null) { isExisting = true; } break; case Item.MeetingResponse: MeetingResponseMessageType responseMessage = type as MeetingResponseMessageType; if (responseMessage != null) { isExisting = true; } break; case Item.MeetingCancellation: MeetingCancellationMessageType cancellationMessage = type as MeetingCancellationMessageType; if (cancellationMessage != null) { isExisting = true; } break; case Item.CalendarItem: CalendarItemType calendarItem = type as CalendarItemType; if (calendarItem != null) { isExisting = true; } break; } } return(isExisting); }