Since it’s inception, Microsoft Exchange has always provided a means to programmatically check email using either CDO, Outlook Objects or what not. Since Exchange 2007, Exchange web services has gained more visibility as the cleanest alternative to access objects from Microsoft Exchange such as your Inbox, contacts, appointments, tasks, etc.

In this entry, I’ll show you how to retrieve you inbox messages using c#. The idea would be to gain access to the objects using an object that is returned by the Exchange Web Service. This object is called the Exchange Service Binding (ExchangeServiceBinding). The function below will return that object, which we will use to get the itemids of the message objects from the inbox.

First, create a service reference in Visual Studio to your Exchange Server’s Web Service: The objects I am using here such as the ExchangeServiceBinding Object are all derived from the web service.

        public static ExchangeServiceBinding GetExchangeServiceBindingObject(string user, string password, string domain, string exchangehost)
            ExchangeServiceBinding esb = new ExchangeServiceBinding();
            esb.Credentials = new System.Net.NetworkCredential(user, password, domain);
            esb.Url = "https://" + exchangehost + "/EWS/Exchange.asmx";
            return esb;

With a live ExchangeServiceBinding
Object, you can call it’s FindItem method to get an array of itemids. Each one of these itemids are unique Microsoft Exchange Ids for the mail messages in your inbox.

        public static BaseItemIdType[] GetInboxItemIDs(ExchangeServiceBinding esb)
            BaseItemIdType[] msgIDArray = null;

            // Form the FindItem request.
            FindItemType findItemRequest = new FindItemType();
            findItemRequest.Traversal = ItemQueryTraversalType.Shallow;

            // Define which item properties are returned in the response.
            ItemResponseShapeType itemProperties = new ItemResponseShapeType();
            itemProperties.BaseShape = DefaultShapeNamesType.IdOnly;
            itemProperties.BodyType = BodyTypeResponseType.Text;

            // Add properties shape to the request.
            findItemRequest.ItemShape = itemProperties;

            // Identify which folders to search to find items.
            DistinguishedFolderIdType[] folderIDArray = new DistinguishedFolderIdType[1];
            folderIDArray[0] = new DistinguishedFolderIdType();
            folderIDArray[0].Id = DistinguishedFolderIdNameType.inbox;

            // Add folders to the request.
            findItemRequest.ParentFolderIds = folderIDArray;

            // Send the request and get the response.
            FindItemResponseType findItemResponse = esb.FindItem(findItemRequest);

            // Get the response messages.
            ResponseMessageType[] rmta = findItemResponse.ResponseMessages.Items;

            //Prepare the ItemID Array
            msgIDArray = new BaseItemIdType[rmta.Length];

            foreach (ResponseMessageType rmt in rmta)
                if (((FindItemResponseMessageType)rmt).ResponseClass == ResponseClassType.Success)
                    foreach (MessageType messagetype in ((ArrayOfRealItemsType)((FindItemParentType)((FindItemResponseMessageType)rmt).RootFolder).Item).Items)
                        msgIDArray[0] = messagetype.ItemId;

            return msgIDArray;

Now that we have an array of ItemIds representing the unique id’s of the messages in our inbox, we can proceed to retrieve the actual message object.

        public static ItemType[] GetMessages(ExchangeServiceBinding esb, BaseItemIdType[] msgIDArray)
            GetItemType git = new GetItemType();
            ItemResponseShapeType irst = new ItemResponseShapeType();
            irst.BaseShape = DefaultShapeNamesType.AllProperties;
            irst.IncludeMimeContent = false;
            git.ItemShape = irst;
            git.ItemIds = msgIDArray;

            GetItemResponseType responsetype = esb.GetItem(git);

            return ((ItemInfoResponseMessageType)((ArrayOfResponseMessagesType)responsetype.ResponseMessages).Items[0]).Items.Items;

So now let’s put these functions together to perform a specific task. The task is to display the body if the latest message from your Microsoft Exchange 2010 Inbox. To do this using the functions above, we run 4 lines of code as below:

        public static string GetLastestBody()
            ExchangeServiceBinding esb = CheckExchange2010MailFlow.ExchangeAPI.GetExchangeServiceBindingObject("", "WouldntYouLikeToKnow2?", "youractivedirectorydomain.local", "");
            BaseItemIdType[] ids = GetInboxItemIDs(esb);
            ItemType[] messages = GetMessages(esb, ids);
            return messages[0].Body.Value;

Of course if you have thousands of messages in your inbox, this code is hardly efficient. You should use the code as a guide to prove out your own concept of what you want to do.

In our case, I put this together to create an “Exchange Email Flow Checker” Job. The job sends an email to a designated mailbox in the target Exchange Server via a mail relay server located on a different physical network. After 10 seconds, the job will use the code above to check for that very message, and return success if it finds the message. The job then wipes that inbox clean getting it ready for the next run. Of course if anything errors out during the process, we will get notified and we can dig deeper – but what this provides for us is a constant active problem detection system that checks to make sure that our Exchange Server can receive emails from external sources.