PropertyAccessor in Exchange 2010 – issue and some workarounds

Following code works fine in Exchange 2003 and 2007. But fails in Exchange 2010, that too in Online Mode. There is an interesting discussion that happened recently in MSDN Forums on this topic. Just would like to share summary of it so that it will be helpful to them who are in need of it.

As of now, there is not work around, except changing mode of Exchange to Cache Mode. Some told that Microsoft is aware of this issue and working on it. A fix may be released with the next patches.

const string TEST_PROPERTY = @"http://schemas.microsoft.com/mapi/string/
{00020329-0000-0000-C000-000000000046}/TestProperty/00000003";
 const int TEST_VALUE = 9;
 application = new MSOutlook.ApplicationClass();
 email =
(MSOutlook.MailItem)application.CreateItem(MSOutlook.OlItemType.olMailItem);
 propertyAccessor = email.PropertyAccessor;
 propertyAccessor.SetProperty(TEST_PROPERTY, TEST_VALUE);
 object objValue = propertyAccessor.GetProperty(TEST_PROPERTY);

Here is another work around suggested by Joel…

“it looks like the it’s not completely resolved by simply using extended MAPI.  I was trying to use IMAPIProps::GetProps() and specifying a single property tag with the ID specified, but the type not specified (using PT_UNSPECIFIED).  As already noted, this works fine with Exchange 2007 online or cached mode, Exchange 2010 in cached mode, but not in Exchange 2010 online mode.

The workaround in this case is to specify the correct property type.  But, since the managed PropertyAccessor.GetProperty() method does not know what the property type is ahead of time (at least for custom properties), I guess that could be why it’s failing.  What I’m doing now is calling IMAPIProp::GetPropList() and finding the entry with matching property tag.  Since GetPropList() returns the property type as well, I can now call GetProps() with the correct property type.  This seems to work in Exchange 2010 online mode :)”

I think this is the simplest of all (which is suggested by Susanne Kapp via Wompi)…

For an easy workaround in VSTO, you can simply call the mailItem.Save() method after setting the property. But be aware of all the mails getting saved to your drafts folder if you don´t send the mail.

Hope this helps!

Getting Rules from an exchange account in Outlook

Getting Rules from an exchange account configured in Outlook is pretty easy though many developers who is working on it for the first time may confuse with the approach. I banged my head for 2 days to get the exact function and attributes to get them.

Dim olRules As Outlook.Rules = Globals.ThisAddIn.Application.Session.DefaultStore.GetRules

For Each olRule As Outlook.Rule In olRules
If olRule.Conditions.From.Recipients.Count > 0 Then
‘ CONDITION: FROM EMAIL
Dim exUser As Outlook.ExchangeUser = olRule.Conditions.From.Recipients.Item(1).AddressEntry.GetExchangeUser
Dim FromEmail As String = exUser.PrimarySmtpAddress
MessageBox.Show(“FROM:  ” & FromEmail)
ElseIf olRule.Conditions.SentTo.Enabled Then
‘ CONDITION: TO EMAIL
Dim exUser As Outlook.ExchangeUser = olRule.Conditions.To.Recipients.Item(1).AddressEntry.GetExchangeUser
Dim ToEmail As String = exUser.PrimarySmtpAddress
MessageBox.Show(“TO: ” & ToEmail)
ElseIf olRule.Conditions.Subject.Enabled Then
‘ CONDITION: SUBJECT
MessageBox.Show(“Subject: ” & olRule.Conditions.Subject.Text)
End If
‘ ACTION: MOVE TO FOLDER
If olRule.Actions.Item(1).ActionType = Outlook.OlRuleActionType.olRuleActionMoveToFolder Then
MessageBox.Show(“Move To Folder: ” & olRule.Actions.MoveToFolder.Folder.FolderPath)
End If
Next
Hope this Helps!

How to get Sender Email Address from MailItem in Exchange Accounts?

For getting Sender Email of a MailItem in POP3/SMTP accounts if pretty easy. We can get that by olMailItem.SenderEmailAddress. But, for Exchange Accounts, its  a bit different. We need to get the AddressEntry first and then get the SMTP Address from that object. Here is a sample code that I framed for simpler understanding.

Dim objSender As Outlook.Recipient = Nothing
Dim objPropXR As Outlook.PropertyAccessor = Nothing
Dim strSMTPAddress As String = “”
Dim strSenderEntryID As String = “”
Dim objNS As Outlook.NameSpace = Nothing
Dim objAE As Outlook.AddressEntry = Nothing
Dim objEU As Outlook.ExchangeUser = Nothing

objNS = Globals.ThisAddIn.Application.GetNamespace(“MAPI”)
objPropXR = objMail.PropertyAccessor
strSenderEntryID = objPropXR.BinaryToString(objPropXR.GetProperty(“http://schemas.microsoft.com/mapi/proptag/0x0C190102”))
objSender = objNS.GetRecipientFromID(strSenderEntryID)
objAE = objSender.AddressEntry
If objAE.AddressEntryUserType = Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry Then
objEU = objAE.GetExchangeUser
strSMTPAddress = objEU.PrimarySmtpAddress
Else
strSMTPAddress = objAE.Address
End If
MessageBox.Show (“Sender Email Address: ” & strSMTPAddress)

objMailItem is an object of Outlook.MailItem

Hope this helps!