3

Outlook 2007 用の Outlook アドインを開発しています。要するに、ユーザーが電子メールを開いたときに、電子メールの送信者のアクティブ ディレクトリ ユーザー プリンシパル オブジェクトを取得する必要があります。

私が達成しようとしていること:

  1. この電子メールの送信者を取得する
  2. この送信者の背後にある対応する Active Directory アカウントを取得します
  3. この広告アカウントの特定の属性を取得します ("physicalDeliveryOfficeName")

ステップ 1 と 3 は処理できますが、exchange-user-account と Active Directory アカウントの間のリンクを取得する方法がわかりません。

私が試したこと

string senderDisplayName = mailItem.SenderName;

重複があるため、表示名でユーザーを見つけることはできません

string senderDistinguishedName = mailItem.SenderEmailAddress;

これは、「O=Company/OU=Some_OU/CN=RECIPIENTS/CN=USERNAME」のようなものを返します。この文字列のユーザー名を抽出できますが、この「ユーザー名」はユーザーのメールボックスのユーザー名などです。Active Directory のユーザー名と常に一致するとは限りません。

送信者オブジェクトの背後にある Active Directory ユーザーを取得する方法はありますか?

環境

  • Outlook 2007 / C# .NET 4
  • エクスチェンジ 2010
  • アクティブ ディレクトリ
4

1 に答える 1

2

以下で説明する手法は、Exchange メールボックス エイリアスがAD アカウント IDと一致することを前提としています。

まずRecipient、Exchange アドレスから を作成し、 を に解決してRecipientから、AD をアカウント ID で検索するためExchangeUserに統合する必要があります。PrincipalContextが見つかったら、カスタム AD プロパティをUserPrincipal照会できます。DirectoryEntry

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();
            }
        }
    }
}

:System.DirectoryServices AD 統合については、およびへの参照が必要ですSystem.DirectoryServices.AccountManagement

于 2012-08-09T12:53:47.570 に答える