2012-08-09 101 views
3

我正在开发Outlook 2007的Outlook插件。简而言之:我需要在用户打开电子邮件时获取电子邮件发件人的活动目录用户主体对象-邮件。从Outlook.MailItem获取发件人活动目录用户主体

我想实现:

  1. 得到这个电子邮件
  2. 获取该发件人
  3. 背后相应的Active Directory帐户的发送者获取此AD-的特定属性帐户(“physicalDeliveryOfficeName”)

我可以处理步骤1和3,但我不知道如何获得exchange-user-帐户和Active Directory帐户

我试过

string senderDisplayName = mailItem.SenderName; 

查找由显示名用户因重复

string senderDistinguishedName = mailItem.SenderEmailAddress; 

这将返回类似“O =企业/ OU是不可能的= Some_OU/CN = RECIPIENTS/CN = USERNAME“ 我可以提取此字符串的用户名,但此”用户名“是用户的邮箱或类似的用户名。它并不总是匹配活动目录用户名。

有没有办法让发送者对象后面的活动目录用户?

环境

  • Outlook 2007中/ C#.NET 4
  • 的Exchange 2010
  • 的Active Directory

回答

2

下面描述的技术假设Exchange邮箱别名符合您AD帐户ID

首先,你需要创建从Exchange地址Recipient,解决RecipientExchangeUser,然后整合PrincipalContext通过帐户ID搜索AD。找到UserPrincipal后,您可以查询DirectoryEntry以获取自定义AD属性。

string deliveryOffice = string.Empty; 
Outlook.Recipient recipient = mailItem.Application.Session.CreateRecipient(mailItem.SenderEmailAddress); 
if (recipient != null && recipient.Resolve() && recipient.AddressEntry != null) 
{ 
    Outlook.ExchangeUser exUser = recipient.AddressEntry.GetExchangeUser(); 
    if (exUser != null && !string.IsNullOrEmpty(exUser.Alias)) 
    { 
     using (PrincipalContext pc = new PrincipalContext(ContextType.Domain)) 
     { 
      UserPrincipal up = UserPrincipal.FindByIdentity(pc, exUser.Alias); 
      if (up != null) 
      { 
       DirectoryEntry directoryEntry = up.GetUnderlyingObject() as DirectoryEntry; 
       if (directoryEntry.Properties.Contains("physicalDeliveryOfficeName")) 
        deliveryOffice = directoryEntry.Properties["physicalDeliveryOfficeName"].Value.ToString(); 
      } 
     } 
    } 
} 

对于AD集成,你需要System.DirectoryServicesSystem.DirectoryServices.AccountManagement引用。