/// <remarks/>
 public void FindItemAsync(FindItemType FindItem1, object userState)
 {
     if ((this.FindItemOperationCompleted == null))
     {
         this.FindItemOperationCompleted = new System.Threading.SendOrPostCallback(this.OnFindItemOperationCompleted);
     }
     this.InvokeAsync("FindItem", new object[] {
             FindItem1}, this.FindItemOperationCompleted, userState);
 }
 /// <remarks/>
 public void FindItemAsync(FindItemType FindItem1)
 {
     this.FindItemAsync(FindItem1, null);
 }
 /// <remarks/>
 public System.IAsyncResult BeginFindItem(FindItemType FindItem1, System.AsyncCallback callback, object asyncState)
 {
     return this.BeginInvoke("FindItem", new object[] {
             FindItem1}, callback, asyncState);
 }
        /// <summary>
        /// Gets the nr of items in a given folder
        /// </summary>
        /// <returns></returns>
        public long GetNrItemsInFolder(ChannelFolder folder)
        {
            var binding = ChannelHelper.BuildChannel(hostname, username, password);

            var findItemRequest = new FindItemType { Traversal = ItemQueryTraversalType.Shallow };
            var itemProperties = new ItemResponseShapeType { BaseShape = DefaultShapeNamesType.AllProperties };
            findItemRequest.ItemShape = itemProperties;

            var folderIdArray = new DistinguishedFolderIdType[2];
            folderIdArray[0] = new DistinguishedFolderIdType { Id = DistinguishedFolderIdNameType.inbox };

            findItemRequest.ParentFolderIds = folderIdArray;

            FindItemResponseType findItemResponse = binding.FindItem(findItemRequest);

            // Determine whether the request was a success.
            if (findItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)
                throw new Exception(findItemResponse.ResponseMessages.Items[0].MessageText);

            var responseMessage =
                (FindItemResponseMessageType) findItemResponse.ResponseMessages.Items[0];

            var mailboxItems = (ArrayOfRealItemsType)responseMessage.RootFolder.Item;

            if (mailboxItems.Items == null) return 0;

            return mailboxItems.Items.Length;
        }
        /// <summary>
        /// Gets a list of all the items in the mailbox with all their properties.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<MessageType> GetHeaders(ChannelFolder folder)
        {
            var binding = ChannelHelper.BuildChannel(hostname, username, password);

            var findItemRequest = new FindItemType { Traversal = ItemQueryTraversalType.Shallow };
            var itemProperties = new ItemResponseShapeType { BaseShape = DefaultShapeNamesType.AllProperties };

            findItemRequest.ItemShape = itemProperties;
            findItemRequest.ParentFolderIds = new BaseFolderIdType[] { new FolderIdType { Id = folder.FolderId } };

            FindItemResponseType findItemResponse = binding.FindItem(findItemRequest);

            foreach (FindItemResponseMessageType responseMessage in findItemResponse.ResponseMessages.Items)
            {
                if (responseMessage.ResponseClass == ResponseClassType.Success)
                {
                    ArrayOfRealItemsType mailboxItems = (ArrayOfRealItemsType) responseMessage.RootFolder.Item;

                    if (mailboxItems.Items == null)
                        yield break;

                    foreach (MessageType inboxItem in mailboxItems.Items)
                        yield return inboxItem;
                }
            }
        }
        /// <summary>
        /// Retrieves contacts from exchange database.
        /// </summary>
        /// <returns></returns>
        public IEnumerable<ContactItemType> GetContacts()
        {
            var binding = ChannelHelper.BuildChannel(hostname, username, password);

            var findItemRequest = new FindItemType
              	{
              		ItemShape = new ItemResponseShapeType {BaseShape = DefaultShapeNamesType.AllProperties},
              		ParentFolderIds = new[] { new DistinguishedFolderIdType { Id = DistinguishedFolderIdNameType.contacts } }
              	};

            FindItemResponseType findItemResponse = binding.FindItem(findItemRequest);

            // Determine whether the request was a success.
            if (findItemResponse.ResponseMessages.Items[0].ResponseClass == ResponseClassType.Error)
                throw new Exception(findItemResponse.ResponseMessages.Items[0].MessageText);

            var responseMessage = (FindItemResponseMessageType)findItemResponse.ResponseMessages.Items[0];
            var contactItems = (ArrayOfRealItemsType)responseMessage.RootFolder.Item;

            return contactItems.Items.Cast<ContactItemType>();
        }