2013-03-04 49 views
0

我正在嘗試獲取某人的電子郵件ID以及其經理的電子郵件ID。以下是我嘗試的代碼。從LDAP獲取經理的電子郵件ID?

DirContext ctx = new InitialDirContext(LDAPDetails()); 
String[] attrIDs = {"sAMAccountName", "cn", "title", "mailnickname", "mail", "manager", "department", "telephoneNumber"}; 
    SearchControls ctls = new SearchControls(); 
    ctls.setReturningAttributes(attrIDs); 
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 
    String filter = "(CN=285263)"; 
    NamingEnumeration<SearchResult> answer = ctx.search("OU=users,DC=cts,DC=com", filter , ctls); 
    answer = ctx.search("OU=xyz,DC=cts,DC=com", filter , ctls); 

    while (answer.hasMore()) { 
    SearchResult sr = (SearchResult) retEnum.next(); 
    Attribute mailAttribute=sr.getAttributes().get("mail"); 
    System.out.println("Team Member's eMail: "+mailAttribute.get()); //Here I am able to get the person's email. 
    Attribute managerAttribute=sr.getAttributes().get("manager"); // this is just getting the manager's CN value. Not the email ID. 
    } 

有人可以幫我取得經理的電子郵件ID嗎?提前致謝。

+1

一旦你得到他的CN – codeMan 2013-03-04 10:56:51

回答

1

你必須來查找經理(們)讓他(們)的電子郵件地址(ES):

DirContext ctx = new InitialDirContext(LDAPDetails()); 
String[] attrIDs = { "sAMAccountName", "cn", "title", "mailnickname", "mail", "manager", "department", "telephoneNumber" }; 
SearchControls ctls = new SearchControls(); 
ctls.setReturningAttributes(attrIDs); 
ctls.setSearchScope(SearchControls.SUBTREE_SCOPE); 
String filter = "(CN=285263)"; 
NamingEnumeration<SearchResult> answer = ctx.search("OU=users,DC=cts,DC=com", filter, ctls); 

while (answer.hasMore()) { 
    SearchResult sr = (SearchResult) retEnum.next(); 
    Attribute mailAttribute = sr.getAttributes().get("mail"); 
    System.out.println("Team Member's eMail: " + mailAttribute.get()); // Here I am able to get the person's email. 
    Attribute managerAttribute = sr.getAttributes().get("manager"); // this is just getting the manager's CN value. Not the email ID. 

    // now lookup the manger 
    NamingEnumeration<SearchResult> managerAnswer = ctx.search(managerAttribute.get(), "", ctls); 
    while (answer.hasMore()) { 
     SearchResult managerSr = (SearchResult) retEnum.next(); 
     Attribute mailAttribute = sr.getAttributes().get("mail"); 
     System.out.println("Managers eMail: " + mailAttribute.get()); 
    } 
} 
+0

,你應該再次搜索經理的電子郵件它不是一個普通的CN值。它給出如下。 CN =插孔骨架,OU =用戶,OU = TCO,OU =城市,OU =國家,OU = APAC,OU = xyz,DC = cts,DC = com – 2013-03-04 11:07:39

+0

然後它應該更加容易。您應該可以使用成員「manager」屬性的值進行第二次查找。我改變了代碼。 – 2013-03-04 11:21:24

+0

刪除垃圾後,我把它作爲一個普通的CN值,並再次搜索,這工作很好。 – 2013-03-04 11:24:35

1

這裏是我的做法......

1..Connecto到LDAP

2..Search用戶和獲取管理器名稱

3 ..與經理名稱和對象類爲用戶過濾條件搜索LDAP。 NamingEnumeration answermanager = context.search(managerAttribute.get()。toString(),「(objectClass = user)」,searchCtls);}}

相關問題