I has been using this function to find the users email address from their username (all my users are on the same domain) without problems, but now some of the users have been upgraded to Windows 10 and I'm getting an error:
The server is not operational
I have made changes to my code with reference to this question and the answer, so now I pick up the default naming context, but the error persists: System.DirectoryServices - The server is not operational
Public Function UserEmail(ByRef Username As String) As String
UserEmail = ""
Dim deRoot As New DirectoryServices.DirectoryEntry("LDAP://RootDSE")
If Not deRoot Is DBNull.Value Then
Dim defaultNamingContext As String = deRoot.Properties("defaultNamingContext").Value.ToString()
Dim path As String
path = "LDAP://" & defaultNamingContext
Dim entry As New DirectoryServices.DirectoryEntry(Path)
Dim search As New DirectoryServices.DirectorySearcher(entry)
search.Filter = "(&(objectClass=user)(anr=" + Username + "))"
search.PropertiesToLoad.Add("mail")
Dim result As DirectoryServices.SearchResult
result = search.FindOne
If IsDBNull(result) Then
UserEmail = ""
Else
UserEmail = result.Properties("mail")(0)
End If
End If
Return UserEmail
End Function
The code works fine where my users are using Windows 7 and 8.
Any suggestions?