简体   繁体   中英

getting Manager's email ID from LDAP?

I am trying to get a person's Email ID as well as his/her manager's email ID. And the following is the code I tried.

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.
   }

Can someone help me get the manager's email ID please? Thanks in advance.

You'll have to lookup the manager(s) to get his (their) email address(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()); 
    }
}

Here is my approach...

1..Connecto to LDAP

2..Search the user and Get manager name

3.. Search the LDAP with the manager name and object class as user in filter condition.

NamingEnumeration answermanager = context.search(managerAttribute.get().toString(),"(objectClass=user)",searchCtls);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM