/// <summary> /// Retrieves the mails from mail box. /// </summary> /// <param name="startIndex">Firts mail to retrieve.</param> /// <param name="maxItems"></param> /// <param name="nextItem"></param> /// <returns></returns> public List<ItemType> GetItems(int startIndex, int maxItems, out int nextItem) { // // Here we setup the paging IndexedPageViewType pagedView = new IndexedPageViewType(); pagedView.BasePoint = IndexBasePointType.Beginning; pagedView.MaxEntriesReturned = maxItems; pagedView.MaxEntriesReturnedSpecified = true; pagedView.Offset = startIndex; // // Here we decide to read inbox. DistinguishedFolderIdType inbox = new DistinguishedFolderIdType(); inbox.Id = DistinguishedFolderIdNameType.inbox; // // We want to retrieve mails with more or less all properties FindItemType findReq = new FindItemType(); findReq.ItemShape = new ItemResponseShapeType(); findReq.ItemShape.BaseShape = DefaultShapeNamesType.AllProperties; // Retrieve by using of paging. findReq.Item = pagedView; // Retrieve the inbox items findReq.ParentFolderIds = new BaseFolderIdType[] { inbox }; findReq.Traversal = ItemQueryTraversalType.Shallow; // Send request FindItemResponseType response = this.serviceProxy.FindItem(findReq); FindItemResponseMessageType responseMessage = response.ResponseMessages.Items[0] as FindItemResponseMessageType; if (responseMessage.ResponseCode != ResponseCodeType.NoError) { throw new Exception(responseMessage.MessageText); } else { // // Here we read mails. // nextItem = responseMessage.RootFolder.IncludesLastItemInRange ? -1 : responseMessage.RootFolder.IndexedPagingOffset; ArrayOfRealItemsType items = responseMessage.RootFolder.Item as ArrayOfRealItemsType; if (items.Items == null) { return new List<ItemType>(); } List<ItemType> mailList = new List<ItemType>(items.Items.Length); foreach (ItemType item in items.Items) { mailList.Add(item); } return mailList; } } |
If you want to retrieve mails from mailbox of an user in context of that user use following code to create service binding [ASMX (not WCF) service proxy]:
private ExchangeServiceBinding serviceProxy = new ExchangeServiceBinding(); this.credentials = new NetworkCredential(“userName@domainname”, “pwd”); this.serviceProxy.Url = “http://exchangehost/EWS/Exchange.asmx"; this.serviceProxy.Credentials = this.credentials; GetItems(). . . |
However if you want to read mails of some user in context of some other user (i.e. administrator reads mails from one user), the you will get following error if you use proxy create as shown above:
”The server to which the application is connected cannot impersonate the requested user due to insufficient permission.”
To make this working execute following power shell scripts, which will
Get-ExchangeServer | Where {$_.ServerRole -match "ClientAccess"} | Add-ADPermission -User "User Name" -ExtendedRights ms-Exch-EPI-Impersonation -InheritanceType None
Get-MailboxDatabase | Add-ADPermission -User "Administrator" -ExtendedRights ms-Exch-EPI-May-Impersonate -InheritanceType All
These scripts set up ms-Exch-EPI-Impersonation and ms-Exch-EPI-May-Impersonate for all users with client access permission on that Exchange Server.
After that, proxy has to be created as follows:
this.credentials = new NetworkCredential(Administrator@domain, "pwd"); this.serviceProxy.Url = ServiceUrl; this.serviceProxy.Credentials = this.credentials; ExchangeImpersonationType exExchangeImpersonation = new ExchangeImpersonationType(); ConnectingSIDType csConnectingSid = new ConnectingSIDType(); csConnectingSid.PrimarySmtpAddress = “userX@domain”; exExchangeImpersonation.ConnectingSID = csConnectingSid; this.serviceProxy.ExchangeImpersonation = exExchangeImpersonation; GetItems(). . . |
This code impersonate reads mailbox of user ‘userX‘ in contyext of user ‘Administrator’.