【问题标题】:Get the senders active directory user principal from Outlook.MailItem从 Outlook.MailItem 获取发件人活动目录用户主体
【发布时间】:2012-08-06 10:39:37
【问题描述】:

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

我想要达到的目标:

  1. 获取此电子邮件的发件人
  2. 获取此发件人背后对应的活动目录帐号
  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”的内容 我可以提取这个字符串的用户名,但是这个“用户名”是用户邮箱的用户名或类似的东西。它并不总是与活动目录用户名匹配。

有没有办法让活动目录用户在 sender-object 后面?

环境

  • Outlook 2007 / C# .NET 4
  • Exchange 2010
  • 活动目录

【问题讨论】:

    标签: c# active-directory outlook-addin outlook-2007


    【解决方案1】:

    下面描述的技术假设 Exchange 邮箱别名 与您的 AD 帐户 ID 匹配。

    首先您需要从 Exchange 地址创建一个Recipient,将Recipient 解析为ExchangeUser,然后集成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

    【讨论】:

      猜你喜欢
      • 2015-04-20
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-26
      • 2017-12-03
      • 2022-01-26
      • 2011-07-06
      相关资源
      最近更新 更多