0

我正在尝试获取一个人的电子邮件 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 吗?提前致谢。

4

2 回答 2

2

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

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()); 
    }
}
于 2013-03-04T10:55:01.437 回答
1

这是我的方法...

1..连接到 LDAP

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

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

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

于 2015-04-03T04:28:33.847 回答