find AD mail address of a specific user?

mac-duff

Member
Joined
Dec 21, 2009
Messages
24
Programming Experience
Beginner
Well,
I am looking all the time for a solution to receive the mail address of a specific user but it dont get it to work:

VB.NET:
    Public Function GetUserEmail(ByVal userName As String) As String

        Dim domainName As String = "MichelThierry" 
        Dim domain As String = "int"

        Dim path As String = String.Format("LDAP://OU=Users,OU=MT IBERICA,DC={0},DC={1}", domainName, domain)
        Dim userEmail As String = String.Empty

        Using search As DirectorySearcher = New DirectorySearcher(path)
            search.Filter = "(SAMAccountName=" & userName & ")"
            search.PropertiesToLoad.Add("mail")
            Try
                Dim result As SearchResult = search.FindOne()
                If result IsNot Nothing Then userEmail = result.Properties("mail").ToString
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Using
        Return userEmail

    End Function

Anybody an idea what is wrong?
 
New topic post moved to new thread.
 
Back
Top