簡體   English   中英

C# - 查找Active Directory用戶的所有電子郵件地址

[英]C# - Find all email addresses for an Active Directory user

我正在嘗試獲取與給定AD用戶關聯的所有電子郵件地址。

對於用戶,我有域名和登錄名(例如DOMAIN \\ UserName),而AD則將電子郵件地址存儲在:

  1. 郵件屬性。
  2. proxyAddresses屬性中。

到目前為止,我不知道用什么C#API連接到AD,以及如何正確地過濾用戶以獲取所有電子郵件地址。 我正在使用.NET 3.5。

謝謝。

這是使用System.DirectoryServices命名空間中的各種類的可能解決方案。

string username = "username";
string domain = "domain";

List<string> emailAddresses = new List<string>();

PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, domain);
UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, username);

// Add the "mail" entry
emailAddresses.Add(user.EmailAddress);

// Add the "proxyaddresses" entries.
PropertyCollection properties = ((DirectoryEntry)user.GetUnderlyingObject()).Properties;
foreach (object property in properties["proxyaddresses"])
{
   emailAddresses.Add(property.ToString());
}

你看過DirectoryEntry類了嗎? 在設置了LDAP字符串的情況下,您可以從那里提取屬性。 郵件的郵件是“郵件”諷刺不是嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM