Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Email of User from Active Directory

Status
Not open for further replies.

hexOffender

Programmer
Nov 6, 2006
146
US
I am trying to do an LDAP query of the AD server to get a users email address. I have seen various versions of essentially the same code all over the net so I know I am not that far off, but I keep getting an Object reference not set to an instance of an object error when I return my searchresults.
Example:

Public Function GetEmail(ByVal user As String) As String
Try
Dim search As New DirectorySearcher(GetDirectoryEntry())
search.Filter = "(&(objectCategory=Person)(objectClass=user)(SAMAccountName=" & user & "))"
search.PropertiesToLoad.Add("mail")
Dim searchResults As SearchResult = search.FindOne
Return searchResults.Properties("mail")(0).ToString 'object reference exception
Catch ex As Exception
MsgBox(ex.ToString, MsgBoxStyle.OKOnly)
End Try

End Function
Public Shared Function GetDirectoryEntry() As DirectoryEntry

Dim dirEntry As New DirectoryEntry("LDAP://SPLATAD1")
'Setting username & password to Nothing forces
'the connection to use your logon credentials
dirEntry.Username = Nothing
dirEntry.Password = Nothing
'Always use a secure connection
dirEntry.AuthenticationType = AuthenticationTypes.Secure
Return dirEntry
End Function
 
I suspect it's because you're trying to convert the value of the property to a string, when the value has already been converted to a string. You might try removing ToString, and perhaps replacing it with Values.
 
Nope that doesnt work either, still get the same error.
 
Here is a bit of something I use to read the properties from AD:

Code:
Dim DSESearcher As System.DirectoryServices.DirectorySearcher = New System.DirectoryServices.DirectorySearcher()
        Dim RootDSE As String = DSESearcher.SearchRoot.Path

        RootDSE = RootDSE.Insert(7, "ou=Users,ou=Our_Company_OU,")
        Dim myDE As System.DirectoryServices.DirectoryEntry = New System.DirectoryServices.DirectoryEntry(RootDSE)
        Dim myEntries As System.DirectoryServices.DirectoryEntries = myDE.Children

        Dim de As System.DirectoryServices.DirectoryEntry = Nothing
        Dim curUser As String = My.User.CurrentPrincipal.Identity.Name


        For Each de In myEntries
            If de.SchemaClassName = "user" Then
                Dim nm As String = de.Properties.Item("givenName").Value & " " & de.Properties.Item("sn").Value

Mine cycles through the entries to compile a complete list, but that should be easily modifiable (sp). You could replace "givenName" or "sn" with "mail" to get the mail property.
 
Here is what I use:

Code:
Public Shared Function GetEmailAddress(ByVal UserName As String) As String
        Dim entry As New DirectoryServices.DirectoryEntry()
        Dim mySearcher As New System.DirectoryServices.DirectorySearcher(entry)
        Dim result As System.DirectoryServices.SearchResult
        Dim objCollResultProperty As System.DirectoryServices.ResultPropertyCollection
        Dim objCollResultPropertyValue As System.DirectoryServices.ResultPropertyValueCollection

        mySearcher.Filter = ("(anr= " & UserName & ")")

        result = mySearcher.FindOne

        If result IsNot Nothing Then
            objCollResultProperty = result.Properties
            objCollResultPropertyValue = objCollResultProperty.Item("mail")

            Return objCollResultPropertyValue.Item(0)
        Else
            Return Nothing
        End If
    End Function

-I hate Microsoft!
-Forever and always forward.
 
Sorwen,
I cant convert result to Boolean, but it looks very similar to the code I had before.
 
I got around that by doing this :
If result Is Nothing Then
Return Nothing
Else
objCollResultProperty = result.Properties
objCollResultPropertyValue = objCollResultProperty.Item("mail")

Return objCollResultPropertyValue.Item(0)
End If

but now it always returns nothing, It is not getting any result from the AD.
 
There are several reason why it could be returning nothing, but the most likely is because of the user id submitted. What you might do for a test is not return "mail", but iterate through all the properties and see what data they have. That will also give you a good idea (if you don't already know) what information the network admin allows to be stored it the Active Directory. You might also try a find (I think that is what it is called) and do not list the full username. That should increase the chance you will pull some data to verify against. It could be usernames are stored in a strange way.


-I hate Microsoft!
-Forever and always forward.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top