Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>marc_s's deleted answer proved to be the most useful; but here's the answer to the question in pseudo-code:</p> <pre><code>public GetUserEmailAddress(String accountName): String; { //Get the distinguished name of the current domain String dn = GetDefaultDistinguishedName(); //e.g. "dc=stackoverflow,dc=com" //Construct the ldap table name (e.g. "LDAP://dc=stackoverflow,dc=com") String ldapTableName := "LDAP://"+dc; //ADO connection string String connectionString := "Provider=ADsDSOObject;Mode=Read;Bind Flags=0;ADSI Flag=-2147483648"; //The sql query to execute String sql := "SELECT mail"+CRLF+ "FROM "+QuotedStr(ldapTableName)+CRLF+ "WHERE objectClass = "+QuotedStr("user")+CRLF+ "AND sAMAccountName = "+QuotedStr(userName); ADOConnection conn := new ADOConnection(connectionString); try Recordset rs := conn.Execute(sql); try if (rs.Eof) return ""; return rs["mail"].Value; finally rs.Free; end; finally conn.Free; end; } </code></pre> <p>The real secret is talking to "the domain", and not any particular server:</p> <pre><code>//get the distinguished name of the current domain public GetDefaultDistinguishedName(): string; { String path := "LDAP://rootDSE"; IADs ads; ADsGetObject(PWideChar(path), IADs, out ads); //e.g. on the "stackoverflow.com" domain, returns "DC=stackoverflow,DC=com" return (String)ads.Get("defaultNamingContext"); } </code></pre> <blockquote> <p><strong>Note</strong>: Any code is released into the public domain. No attribution required.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload