c# - 从 Outlook.MailItem 获取发件人事件目录用户主体

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

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

我想要实现的目标:

  1. 获取此电子邮件的发件人
  2. 获取此发件人背后对应的事件目录帐号
  3. 获取此广告帐户的特定属性(“physicalDeliveryOfficeName”)

我可以处理第 1 步和第 3 步,但我不知道如何获取交换用户帐户和事件目录帐户之间的链接

我尝试过的

string senderDisplayName = mailItem.SenderName;

由于重复,无法通过显示名查找用户

string senderDistinguishedName = mailItem.SenderEmailAddress;

这将返回类似“O=Company/OU=Some_OU/CN=RECIPIENTS/CN=USERNAME”的内容 我可以提取这个字符串的用户名,但是这个“用户名”是用户邮箱的用户名或类似的东西。它并不总是与 Active Directory 用户名匹配。

有没有办法让发件人对象后面的事件目录用户?

环境

  • 展望 2007/C#.NET 4
  • 交流 2010
  • 事件目录

最佳答案

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

首先你需要创建一个Recipient从 Exchange 地址,将 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.

关于c# - 从 Outlook.MailItem 获取发件人事件目录用户主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11879445/

相关文章:

powershell - 仅在使用 ISE 时才会出现确认对话框

outlook - 如何在 Outlook 中启用/禁用我的功能区按钮

c# - 如何将 'quick steps' 样式控件添加到Outlook 2010 功能区?

c# - 使用 SSL 向 GMail 发送邮件时身份验证或解密失败

c# - 以下在c#中使用expression-bodied语法的方式有区别吗?

c# - 在 Excel VBA 中使用来自 COM 插件的 CLR 类?

powershell - 使用DirectorySearcher的Powershell LDAP过滤器

c# - 在 .NET 中使用不同的用户凭据对共享 Windows 文件夹进行 RW 访问

javascript - Outlook AddIn GetAsync 成功但不返回任何内容

c# - 使用响应式扩展 (rx) 枚举及时发生的事件