得到ActiveDirectory的邮箱地址 [英] get mail address from ActiveDirectory

查看:116
本文介绍了得到ActiveDirectory的邮箱地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了这些问题的答案<一个href="http://www.google.co.uk/search?hl=en&q=C%23+-+search+for+email+address+in+Active+Directory+site%3Astackoverflow.com&meta="相对=nofollow>上的问题,以便

I have tried the answers to these questions Questions on SO

String account = userAccount.Replace(@"Domain\", "");
DirectoryEntry entry = new DirectoryEntry();

try {
    DirectorySearcher search = new DirectorySearcher(entry);

    search.PropertiesToLoad.Add("mail");  // e-mail addressead

    SearchResult result = search.FindOne();
    if (result != null) {
        return result.Properties["mail"][0].ToString();
    } else {
        return "Unknown User";
    }
} catch (Exception ex) {
    return ex.Message;
}

有人能看到在正确的方向上的问题,或者点?

Can anyone see the issue or point in the right direction?

推荐答案

我已经使用这个code成功(这里的账户是不带域(域\帐户的用户登录名):

I have used this code successfully (where "account" is the user logon name without the domain (domain\account):

// get a DirectorySearcher object
DirectorySearcher search = new DirectorySearcher(entry);

// specify the search filter
search.Filter = "(&(objectClass=user)(anr=" + account + "))";

// specify which property values to return in the search
search.PropertiesToLoad.Add("givenName");   // first name
search.PropertiesToLoad.Add("sn");          // last name
search.PropertiesToLoad.Add("mail");        // smtp mail address

// perform the search
SearchResult result = search.FindOne();

这篇关于得到ActiveDirectory的邮箱地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆