从LDAP获取Manager的电子邮件ID?

时间:2013-03-04 10:38:20

标签: java ldap

我正在尝试获取某人的电子邮件ID以及他/她的经理的电子邮件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吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

您必须查找经理以获取他(他们)的电子邮件地址:

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()); 
    }
}

答案 1 :(得分:1)

这是我的方法......

1..Connecto to LDAP

2 ..搜索用户并获取经理姓名

3 ..在过滤条件中使用管理员名称和对象类作为用户搜索LDAP。

NamingEnumeration answermanager = context.search(managerAttribute.get()。toString(),&#34;(objectClass = user)&#34;,searchCtls);