Skip to main content

Using MailTips in EWS to get the OOF (Out of Office) Status of users with C# and Powershell

MailTips is one of the really useful new features of Exchange 2010 that can be exceedingly useful to anybody using Exchange Web Services. It provides access to Information such as the OOF status of a user or users within one WebService call and without the need for Full Access or elevated rights you would normally need with a GetUserOofSettings operations or using the Remote powershell cmdlets such as Get-MailboxAutoReplyConfiguration.

The other thing MailTips are useful for apart from the other standard Mail-tips listed http://msdn.microsoft.com/en-us/library/dd877060%28v=exchg.140%29.aspx (things like the Max message size is useful for anybody sending email via EWS etc)  is the ability to create Custom MailTips. This gives you the ability to now retrieve a custom property that is stored within Active Directory via EWS. Which provides a workaround to not being able to access the information in the AD Custom Attributes http://technet.microsoft.com/en-us/library/ee423541.aspx currently in EWS.

To use the EWS Mailtips operations you need to use WSDL Proxy objects or Custom SOAP code as currently these operations aren't available for use within the EWS Managed API. If you want to script it I would still combine it with EWS Managed API code because of ease of access to things like Autodiscover this gives you.

I've created a powershell and C# sample of using the GetMailtips operation the C# looks like


  1. ExchangeServiceBinding esb = new ExchangeServiceBinding();   
  2. esb.Credentials = new NetworkCredential("user@domain.com""password""");   
  3. esb.Url = "https://ch1prd0302.outlook.com/EWS/Exchange.asmx";   
  4. esb.RequestServerVersionValue = new RequestServerVersion();   
  5. esb.RequestServerVersionValue.Version = ExchangeVersionType.Exchange2010_SP1;   
  6. GetMessageTrackingReportRequestType gmt = new GetMessageTrackingReportRequestType();   
  7. GetMailTipsType gmType = new GetMailTipsType();   
  8. gmType.MailTipsRequested = new MailTipTypes();   
  9. gmType.MailTipsRequested = MailTipTypes.OutOfOfficeMessage;   
  10. gmType.Recipients = new EmailAddressType[1];   
  11. EmailAddressType rcip = new EmailAddressType();   
  12. rcip.EmailAddress = "user@domain.com";   
  13. gmType.Recipients[0] = rcip;   
  14. EmailAddressType sendAs = new EmailAddressType();   
  15. sendAs.EmailAddress = "sender@domain.com";   
  16. gmType.SendingAs = sendAs;   
  17.   
  18. GetMailTipsResponseMessageType gmResponse = esb.GetMailTips(gmType);   
  19. if (gmResponse.ResponseClass == ResponseClassType.Success) {   
  20.     if (gmResponse.ResponseMessages[0].MailTips.OutOfOffice.ReplyBody.Message != "")   
  21.     {   
  22.         //User Out   
  23.         Console.WriteLine(gmResponse.ResponseMessages[0].MailTips.OutOfOffice.ReplyBody.Message);   
  24.     }   
  25.     else {    
  26.         //user In   
  27.     }   
  28.                      
  29. }  
With the Powershell example the following information needs to be configured

$MailboxName = "sender@domain.com"

Mailbox which will do the check from
 
$Mailboxes = @("user1@domain.com","user2@domain.com")

Mailbox you want to check the OOF of (this is just an array or Strings)  
 
$cred = New-Object System.Net.NetworkCredential("user1@domain.com","password@#")

Credentials to use

I've put a download of the script here the code itself looks like


  1. $MailboxName = "sender@domain.com"  
  2.   
  3. $Mailboxes = @("user1@domain.com","user2@domain.com")   
  4.   
  5. $cred = New-Object System.Net.NetworkCredential("user1@domain.com","password@#")   
  6.   
  7. $dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"  
  8. [void][Reflection.Assembly]::LoadFile($dllpath)   
  9. $service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP1)   
  10. $service.TraceEnabled = $false  
  11.   
  12. $service.Credentials = $cred  
  13. $service.autodiscoverurl($MailboxName,{$true})   
  14.   
  15.   
  16. $expRequest = @"  
  17. <?xml version="1.0" encoding="utf-8"?>  
  18. <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  
  19. <soap:Header><RequestServerVersion Version="Exchange2010_SP1" xmlns="http://schemas.microsoft.com/exchange/services/2006/types" />  
  20. </soap:Header>  
  21. <soap:Body>  
  22. <GetMailTips xmlns="http://schemas.microsoft.com/exchange/services/2006/messages">  
  23. <SendingAs>  
  24. <EmailAddress xmlns="http://schemas.microsoft.com/exchange/services/2006/types">$MailboxName</EmailAddress>  
  25. </SendingAs>  
  26. <Recipients>  
  27. "@   
  28.   
  29. foreach($mbMailbox in $Mailboxes){   
  30.     $expRequest = $expRequest + "<Mailbox xmlns=`"http://schemas.microsoft.com/exchange/services/2006/types`"><EmailAddress>$mbMailbox</EmailAddress></Mailbox>"    
  31. }   
  32.   
  33. $expRequest = $expRequest + "</Recipients><MailTipsRequested>OutOfOfficeMessage</MailTipsRequested></GetMailTips></soap:Body></soap:Envelope>"  
  34. $mbMailboxFolderURI = New-Object System.Uri($service.url)   
  35. $wrWebRequest = [System.Net.WebRequest]::Create($mbMailboxFolderURI)   
  36. $wrWebRequest.KeepAlive = $false;   
  37. $wrWebRequest.Headers.Set("Pragma""no-cache");   
  38. $wrWebRequest.Headers.Set("Translate""f");   
  39. $wrWebRequest.Headers.Set("Depth""0");   
  40. $wrWebRequest.ContentType = "text/xml";   
  41. $wrWebRequest.ContentLength = $expRequest.Length;   
  42. $wrWebRequest.Timeout = 60000;   
  43. $wrWebRequest.Method = "POST";   
  44. $wrWebRequest.Credentials = $cred  
  45. $bqByteQuery = [System.Text.Encoding]::ASCII.GetBytes($expRequest);   
  46. $wrWebRequest.ContentLength = $bqByteQuery.Length;   
  47. $rsRequestStream = $wrWebRequest.GetRequestStream();   
  48. $rsRequestStream.Write($bqByteQuery, 0, $bqByteQuery.Length);   
  49. $rsRequestStream.Close();   
  50. $wrWebResponse = $wrWebRequest.GetResponse();   
  51. $rsResponseStream = $wrWebResponse.GetResponseStream()   
  52. $sr = new-object System.IO.StreamReader($rsResponseStream);   
  53. $rdResponseDocument = New-Object System.Xml.XmlDocument   
  54. $rdResponseDocument.LoadXml($sr.ReadToEnd());   
  55. $RecipientNodes = @($rdResponseDocument.getElementsByTagName("t:RecipientAddress"))   
  56. $Datanodes = @($rdResponseDocument.getElementsByTagName("t:OutOfOffice"))   
  57. for($ic=0;$ic -lt $RecipientNodes.length;$ic++){   
  58.     if($Datanodes[$ic].ReplyBody.Message -eq ""){   
  59.         $RecipientNodes[$ic].EmailAddress + " : In the Office"  
  60.     }   
  61.     else{   
  62.         $RecipientNodes[$ic].EmailAddress + " : Out of Office"  
  63.     }   
  64. }   

Popular posts from this blog

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Gr...

Exporting and Uploading Mailbox Items using Exchange Web Services using the new ExportItems and UploadItems operations in Exchange 2010 SP1

Two new EWS Operations ExportItems and UploadItems where introduced in Exchange 2010 SP1 that allowed you to do a number of useful things that where previously not possible using Exchange Web Services. Any object that Exchange stores is basically a collection of properties for example a message object is a collection of Message properties, Recipient properties and Attachment properties with a few meta properties that describe the underlying storage thrown in. Normally when using EWS you can access these properties in a number of a ways eg one example is using the strongly type objects such as emailmessage that presents the underlying properties in an intuitive way that's easy to use. Another way is using Extended Properties to access the underlying properties directly. However previously in EWS there was no method to access every property of a message hence there is no way to export or import an item and maintain full fidelity of every property on that item (you could export the...

Sending a Message in Exchange Online via REST from an Arduino MKR1000

This is part 2 of my MKR1000 article, in this previous post  I looked at sending a Message via EWS using Basic Authentication.  In this Post I'll look at using the new Outlook REST API  which requires using OAuth authentication to get an Access Token. The prerequisites for this sketch are the same as in the other post with the addition of the ArduinoJson library  https://github.com/bblanchon/ArduinoJson  which is used to parse the Authentication Results to extract the Access Token. Also the SSL certificates for the login.windows.net  and outlook.office365.com need to be uploaded to the devices using the wifi101 Firmware updater. To use Token Authentication you need to register an Application in Azure https://msdn.microsoft.com/en-us/office/office365/howto/add-common-consent-manually  with the Mail.Send permission. The application should be a Native Client app that use the Out of Band Callback urn:ietf:wg:oauth:2.0:oob. You ...
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.