C# Outlook 加载项 > 约会所有者 > 检索 Active Directory 帐户

C# Outlook Add-In > Appointment Owner > Retrieve Active Directory Account

在 Outlook 加载项中,我想从约会中检索相应的 AD 帐户。 Outlook 客户端可能有来自不同共享帐户的多个日历。 我生成了以下代码,但不确定以下内容:

  1. 对 link 约会所有者 -> Outlook 帐户 -> AD 帐户的引用是否正确
  2. 如何找回Outlook账号对应的AD账号

我设法提取了用​​户名和 SMTP 地址,但不确定是否可以根据这些在 AD 中进行搜索? 我评论了:

```C#
var currentAppointmentUserID = appointmentItem.Parent.Parent.Parent.Currentuser.EntryID;
// Outlook application.
Microsoft.Office.Interop.Outlook.Application oApp = Globals.ThisAddIn.Application;
// Loop through Accounts
Account currentAppointmentAccount = null;
foreach(Account acc in oApp.Session.Accounts) 
  {
  if (acc.CurrentUser.EntryID == currentAppointmentUserID) 
  {
    currentAppointmentAccount = acc;
    break;
  }
}
if (currentAppointmentAccount != null) {
  Logger.WriteLine(LogLevel.Debug, "Exchange account found: " + currentAppointmentAccount.UserName + " -> SMTP Address: " + currentAppointmentAccount.SmtpAddress);

  // Query AD for account with SMTP address
  using(PrincipalContext context = new PrincipalContext(ContextType.Domain, "MyDomain")) 
    {
    UserPrincipal userFound = UserPrincipal.FindByIdentity(context, EmailAddress);

    if (userFound != null) 
    {
      userFound.FirstName = yourUser.GivenName;
      userFound.LastName = yourUser.Surname;
    }
  }
}

好的,如果有人感兴趣的话:

string PR_MAILBOX_OWNER_ENTRYID = "http://schemas.microsoft.com/mapi/proptag/0x661B0102";

try {
   Store store = Globals.ThisAddIn.Application.Session.GetStoreFromID(appointmentItem.Parent.StoreID);
   string mailOwnerEntryId = store.PropertyAccessor.BinaryToString(store.PropertyAccessor.GetProperty(PR_MAILBOX_OWNER_ENTRYID));
   AddressEntry addr = Globals.ThisAddIn.Application.Session.GetAddressEntryFromID(mailOwnerEntryId);

   if (addr.Type == "EX") {
      ExchangeUser exUser = appointmentItem.GetOrganizer().GetExchangeUser();
      Console.WriteLine("The SamAccountName is: " + exUser.Alias);
   } else {
      Console.WriteLine("Address is not of Exchange type.");
      }
   } catch (System.Exception ex) {}