2012-08-09 102 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引用。