i’m developing an outlook addin for Outlook 2007. In short: I need to get the active directory user principal object of the e-mails sender when a user opens an e-mail.
What I am trying to achieve:
- Get the sender of this e-mail
- Get the corresponding active directory account behind this sender
- Get a specific attribute of this ad-account (“physicalDeliveryOfficeName”)
I can handle step 1 and 3, but I don’t know how to get the link between the exchange-user-account and the active directory account
What I tried
string senderDisplayName = mailItem.SenderName;
Finding a user by displayname is impossible due to duplicates
string senderDistinguishedName = mailItem.SenderEmailAddress;
This will return something like “O=Company/OU=Some_OU/CN=RECIPIENTS/CN=USERNAME”
I can extract the username of this string, but this “username” is the username of the user’s mailbox or something like that. It doesn’t always match the active directory user name.
Is there a way to get the active directory user behind the sender-object?
Environment
- Outlook 2007 / C# .NET 4
- Exchange 2010
- Active Directory
The technique described below assumes the Exchange Mailbox Alias matches your AD Account ID.
First you need to create a
Recipientfrom the Exchange address, resolve theRecipientto anExchangeUser, and then integratePrincipalContextfor searching AD by account ID. Once theUserPrincipalis located, you can query theDirectoryEntryfor custom AD properties.Note: For AD integration, you need references to
System.DirectoryServicesandSystem.DirectoryServices.AccountManagement.