i have the groups names in the database
after getting them i get the manager from the active directory using directory searcher
like this

public string getGroupManagersMail(string groupname)
{
        DirectoryEntry drtEnt = new DirectoryEntry("LDPA=//DC=domain,DC=mil");
        DirectorySearcher ds = new DirectedorySearcher(drtEnt, "sAMAccountName" + groupname);
        ds.PropertiesToLoad.Add("managedby");
        SearchResult sr = ds.FindOne();

i do able to get his CN,OU,DC and displayd name of the manager
but i cant get his mail.
i have tried to search in the active directory by creating a new directory
entry with the params that i got in the searchresult but it didn't work
could be because the dispalys name is not in english
does anyone know the answer?
}

Dani AI

Generated

Short answer: the group’s managedBy value is the manager’s distinguished name (DN) — bind to that DN and read the manager object’s attributes (mail, proxyAddresses, userPrincipalName). The language of displayName does not matter; using the DN is the reliable way. Also make sure your LDAP path and filter are correct (for example use "LDAP://DC=domain,DC=mil" and a filter like "(sAMAccountName=theGroupName)"). This is likely why could see CN/OU parts but not the mail value.

A robust approach: search the group, fetch managedBy, create a DirectoryEntry bound to "LDAP://<managedByDN>", then check, in order, the mail attribute, the proxyAddresses list (Exchange stores addresses here; the primary address is the one that starts with "SMTP:" uppercase), and finally userPrincipalName as a last-resort fallback. Also always check that the properties exist and have a count > 0 before reading.

Example pattern (C#):

public static string GetGroupManagersMail(string domainPath, string groupName)
{
    using(var root = new DirectoryEntry(domainPath))
    using(var ds = new DirectorySearcher(root))
    {
        ds.Filter = "(&(objectClass=group)(sAMAccountName=" + EscapeLdapFilter(groupName) + "))";
        ds.PropertiesToLoad.Add("managedBy");
        var sr = ds.FindOne();
        if (sr == null || !sr.Properties.Contains("managedBy")) return null;
        var mgrDn = sr.Properties["managedBy"][0] as string;
        if (string.IsNullOrEmpty(mgrDn)) return null;

        using(var mgr = new DirectoryEntry("LDAP://" + mgrDn))
        {
            if (mgr.Properties.Contains("mail") && mgr.Properties["mail"].Count > 0)
                return mgr.Properties["mail"][0].ToString();

            if (mgr.Properties.Contains("proxyAddresses"))
            {
                // prefer primary SMTP: (uppercase)
                foreach (var a in mgr.Properties["proxyAddresses"])
                {
                    var s = a.ToString();
                    if (s.StartsWith("SMTP:", StringComparison.Ordinal))
                        return s.Substring(5);
                }
                // fallback to any smtp:
                foreach (var a in mgr.Properties["proxyAddresses"])
                {
                    var s = a.ToString();
                    if (s.StartsWith("smtp:", StringComparison.OrdinalIgnoreCase))
                        return s.Substring(5);
                }
            }

            if (mgr.Properties.Contains("userPrincipalName") && mgr.Properties["userPrincipalName"].Count > 0)
                return mgr.Properties["userPrincipalName"][0].ToString();
        }
    }
    return null;
}

Troubleshooting notes: if managedBy points to a contact or another group, there may be no mail. If mail looks missing even though an Exchange address exists, check proxyAddresses. If binding fails, confirm credentials/permissions and that the LDAP path is correct. If you intend to update mail attributes, ’s SetADProperty helper is useful for safely adding/updating attributes.

I've worked on this code

''' <param name="de">DirectoryEntry to use</param>
''' <param name="pName">Property name to set</param>
''' <param name="pValue">Value of property to set</param>
Public Shared Sub SetADProperty(ByVal de As DirectoryEntry, _
ByVal pName As String, ByVal pValue As String)
    'First make sure the property value isnt "nothing"
    If Not pValue Is Nothing Then
        'Check to see if the DirectoryEntry contains this property already
        If de.Properties.Contains(pName) Then 'The DE contains this property
            'Update the properties value
            de.Properties(pName)(0) = pValue
        Else    'Property doesnt exist
            'Add the property and set it's value
            de.Properties(pName).Add(pValue)
        End If
    End If
End Sub

Piyush

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.