Saturday, May 14, 2011

Sending a message with a classification in EWS in C# and Powershell

Message classifications have been around since Exchange 2007 and are a feature of Exchange that allows you to setup your email system to add classification to messages to comply with e-mail policies and regulatory responsibilities that Governments and other bodies may require of an organization. If you want to set a message classification on a Message your sending pro-grammatically via EWS you need to first find out what the GUID of the classification you want to set is. There are a few ways of doing this the easiest is using the Get-MessageClassification cmdlet http://technet.microsoft.com/en-us/library/aa996911.aspx the value you will need for your app or script is the ClassificationID which represents the PidLidClassificationGuid Mapi property which if you’re playing around with Transport Agents this gets converted into the x-ms-exchange-organization-classification x-header. You can also grab it out of your classification xml file if you have deployed it to Outlook.

All the properties that relate to classification are documented in http://msdn.microsoft.com/en-us/library/ee217686%28v=EXCHG.80%29.aspx. At the most basic level if you want to set a classification on a message your sending you need to set the following two properties on that message.

PidLidClassificationGuid which you set to the GUID of the classification you want applied to the message

PidLidClassified which is a Boolean that tells the Exchange this message is classified.

With the EWS Managed API in C# this look like

EmailMessage emEmailMessage = new EmailMessage(service);
emEmailMessage.ToRecipients.Add("user@domaim.com");
emEmailMessage.Subject = "test Classify";
emEmailMessage.Body = new MessageBody("test");
ExtendedPropertyDefinition PidLidClassificationGuid = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Common, 34232, MapiPropertyType.String);
emEmailMessage.SetExtendedProperty(PidLidClassificationGuid, "e67e794b-f6d1-4c8f-9f63-1118d21dafa6");
ExtendedPropertyDefinition PidLidClassified = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.Common,34229, MapiPropertyType.Boolean);
emEmailMessage.SetExtendedProperty(PidLidClassified, true);
emEmailMessage.SendAndSaveCopy();

In Powershell it looks like

$MailboxName = "user@domain.com"
$dllpath = "C:\Program Files\Microsoft\Exchange\Web Services\1.1\Microsoft.Exchange.WebServices.dll"
[void][Reflection.Assembly]::LoadFile($dllpath)
$service = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010)
#$service.Credentials = New-Object System.Net.NetworkCredential("glen@domain.com","password")
$service.AutodiscoverUrl($MailboxName,{$true})
$message = New-Object Microsoft.Exchange.WebServices.Data.EmailMessage($service)
$message.Subject = "Test"
$message.Body = "Test 123"
$message.ToRecipients.Add("user@domain.com")
$PidLidClassificationGuid = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition([Microsoft.Exchange.WebServices.Data.DefaultExtendedPropertySet]::Common, 34232,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);
$message.SetExtendedProperty($PidLidClassificationGuid, "e67e794b-f6d1-4c8f-9f63-1118d21dafa6");
$PidLidClassified = New-Object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition([Microsoft.Exchange.WebServices.Data.DefaultExtendedPropertySet]::Common,34229,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Boolean);
$message.SetExtendedProperty($PidLidClassified, $true);
$message.Send.Invoke()

Monday, May 09, 2011

Making use of the PidTagStartDateEtc to calculate the age of an object for Achive and Retention policies

When your dealing with Archive and Retention policies in Exchange 2010 one of the important questions to ask is what property is used to calculate the age of an Item. While it might be tempting to use some of the easy to get to datetime properties the important property to use in relation to Archive and Retention policies is the PidTagStartDateEtc property which is documented in http://msdn.microsoft.com/en-us/library/ee179537%28v=exchg.80%29.aspx.

So as stated this is a composite property with the first 4 bytes belonging to the default retention period if it has been applied then the final 8 bytes gives the UTC Datetime from which the Message object's age is calculated. The following is a quick sample of how to extract this datetime from that property using the EWS Managed API.

ExtendedPropertyDefinition PidTagStartDateEtc = new ExtendedPropertyDefinition(12315, MapiPropertyType.Binary);
Object StartDate = null;
itItem.TryGetProperty(PidTagStartDateEtc, out StartDate);
long fileTime = BitConverter.ToInt64((Byte[])StartDate, 4);
DateTime ItemAge = DateTime.FromFileTime(fileTime);

Wednesday, May 04, 2011

Article on using EWS to search mailboxes

Just a quick note to let everyone know who is interested in reading more about the search capabilities of Exchange Web Services against Exchange Online (Office 365) and Exchange 2010 Check out my article that's just been published on MSDN http://msdn.microsoft.com/en-us/library/hh148195%28v=EXCHG.140%29.aspx