2016-01-11

Access Office 365 Mail Box by Exchange Service Managed API


So here is the requirement from clinet: We are required to monitor several Office 365 mail boxes, and if the specific email is found, we need to do some follow up activities, e.g. re-distribute the email to a nominated user.
To access the Office 365 mail box, we can use Exchange Service Managed API. In the old way we can use method listed at here. However now we can use a much easier way to handle the request, as now Office 365 provides a direct endpoint for EWS Managed API. Below is a quick demonstration of how to access Office 365 mail box.

The first step, and the only step to prepare the project, is to reference the EWS API in your project. The API can be referenced into your project from nuget directly. Or alternatively you can obtain it from Microsoft download centre. If you download the API from Microsoft download centre, you need to manually reference the API in your project. The dll can be found at “C:\Program Files (x86)\Microsoft\Exchange\Web Services\{version no.}\Microsoft.Exchange.WebServices.dll”.



Now time to start coding. Firstly we need to declare the endpoint for the EWS API.

       string emailaddr = "{o365 account}";  
       string password = "{o365 password}";  
       ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);  
       service.Credentials = new WebCredentials(emailaddr, password);  
       Uri u = new Uri(@"https://outlook.office365.com/EWS/Exchange.asmx");  
       service.Url = u;        


To make things simple, assume we are going to search unread emails in the inbox folder. So we create a search view for the folder

       Folder inbox = Folder.Bind(service, WellKnownFolderName.Inbox);  
       SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));  
       ItemView view = new ItemView(10);  


Now there is a small trouble: we need to find the item ID from FindItems call, then we use GetItem call to get item contents.

       FindItemsResults<Item> results = service.FindItems(WellKnownFolderName.Inbox, filter, view);  
       ServiceResponseCollection<GetItemResponse> responses = service.BindToItems(results.Select(i => i.Id)  
         , new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.ToRecipients));  
       foreach (var item in responses)  
       {  
         EmailAddress fromaddress = (EmailAddress)item.Item[EmailMessageSchema.From];  
         EmailAddressCollection to = (EmailAddressCollection)item.Item[EmailMessageSchema.ToRecipients];  
         string toaddress = string.Empty;  
         foreach (EmailAddress a in to)  
         {  
           toaddress += a.Address + "; ";  
         }  
         Console.WriteLine("From: {0}", fromaddress.Address);  
         Console.WriteLine("To: {0}", toaddress);  
         Console.WriteLine("Subject: {0}", item.Item.Subject);  
         Console.WriteLine("Body:{0}", item.Item.Body == null ? string.Empty : item.Item.Body.Text);  
         Console.WriteLine();  
       }  
       Console.ReadLine();  

So that is all, quick and simple. Below is the result from above code