我正在为 Outlook 2007 开发一个 outlook 插件。简而言之:我需要在用户打开电子邮件时获取电子邮件发件人的事件目录用户主体对象。
我想要实现的目标:
- 获取此电子邮件的发件人
- 获取此发件人背后对应的事件目录帐号
- 获取此广告帐户的特定属性(“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.DirectoryServices
和 System. DirectoryServices.AccountManagement
.
关于c# - 从 Outlook.MailItem 获取发件人事件目录用户主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11879445/