By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,168 Members | 2,511 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,168 IT Pros & Developers. It's quick & easy.

Accessing LDAP with VB.

P: n/a
Hi,

I am trying to get someones email address out of the win 2003 server
Directory using there logon name and the following code.

Dim DSEntry As New System.DirectoryServices.DirectoryEntry("LDAP:" &
System.Environment.GetEnvironmentVariable("LOGONSE RVER") & "/CN=" &
System.Enviroment.Username & ",CN=Users,DC=laxeypartners,DC=com")
EmailAddress = DSEntry.Properties("mail").Value.ToString()

However, for some users the directory display name is not the same as the
logon name. e.g. Directory name is John Smith and logon name is
John.Smith.

When I use the logon name in the above code it does not find an entry in the
directory.

Does anyone know of a method that i can use to extract the default email
address from the logon name.

Many thanks,

Steve Lloyd
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Something like this should work

Private Function GetEmailAddress() As String
Dim Root As New DirectoryEntry("LDAP://RootDSE")
Dim DomainNC As String = Root.Properties("defaultNamingContext").Value
Root = New DirectoryEntry("LDAP://" & DomainNC)
Dim Searcher As New DirectorySearcher(Root)
With Searcher
.Filter = "(sAMAccountName=" & Environment.UserName & ")"
.SearchScope = SearchScope.Subtree
.PropertiesToLoad.Add("mail")
End With
Dim Result As SearchResult = Searcher.FindOne
Dim EMailAddress As String
If Not Result Is Nothing Then
EMailAddress = Result.GetDirectoryEntry.Properties("mail").Value
End If
Return EMailAddress
End Function

"Steve Lloyd" <Re*************@laxeyRemovepartners.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

I am trying to get someones email address out of the win 2003 server
Directory using there logon name and the following code.

Dim DSEntry As New System.DirectoryServices.DirectoryEntry("LDAP:" &
System.Environment.GetEnvironmentVariable("LOGONSE RVER") & "/CN=" &
System.Enviroment.Username & ",CN=Users,DC=laxeypartners,DC=com")
EmailAddress = DSEntry.Properties("mail").Value.ToString()

However, for some users the directory display name is not the same as the
logon name. e.g. Directory name is John Smith and logon name is
John.Smith.

When I use the logon name in the above code it does not find an entry in
the
directory.

Does anyone know of a method that i can use to extract the default email
address from the logon name.

Many thanks,

Steve Lloyd

Nov 20 '05 #2

P: n/a
Perfect, thank you very much...
"Jared" <VB***********@email.com> wrote in message
news:10*************@corp.supernews.com...
Something like this should work

Private Function GetEmailAddress() As String
Dim Root As New DirectoryEntry("LDAP://RootDSE")
Dim DomainNC As String = Root.Properties("defaultNamingContext").Value
Root = New DirectoryEntry("LDAP://" & DomainNC)
Dim Searcher As New DirectorySearcher(Root)
With Searcher
.Filter = "(sAMAccountName=" & Environment.UserName & ")"
.SearchScope = SearchScope.Subtree
.PropertiesToLoad.Add("mail")
End With
Dim Result As SearchResult = Searcher.FindOne
Dim EMailAddress As String
If Not Result Is Nothing Then
EMailAddress = Result.GetDirectoryEntry.Properties("mail").Value
End If
Return EMailAddress
End Function

"Steve Lloyd" <Re*************@laxeyRemovepartners.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi,

I am trying to get someones email address out of the win 2003 server
Directory using there logon name and the following code.

Dim DSEntry As New System.DirectoryServices.DirectoryEntry("LDAP:" &
System.Environment.GetEnvironmentVariable("LOGONSE RVER") & "/CN=" &
System.Enviroment.Username & ",CN=Users,DC=laxeypartners,DC=com")
EmailAddress = DSEntry.Properties("mail").Value.ToString()

However, for some users the directory display name is not the same as the logon name. e.g. Directory name is John Smith and logon name is
John.Smith.

When I use the logon name in the above code it does not find an entry in
the
directory.

Does anyone know of a method that i can use to extract the default email
address from the logon name.

Many thanks,

Steve Lloyd


Nov 20 '05 #3

P: n/a
You're welcome

"Steve Lloyd" <Re*************@laxeyRemovepartners.com> wrote in message
news:uE****************@TK2MSFTNGP09.phx.gbl...
Perfect, thank you very much...
"Jared" <VB***********@email.com> wrote in message
news:10*************@corp.supernews.com...
Something like this should work

Private Function GetEmailAddress() As String
Dim Root As New DirectoryEntry("LDAP://RootDSE")
Dim DomainNC As String =
Root.Properties("defaultNamingContext").Value
Root = New DirectoryEntry("LDAP://" & DomainNC)
Dim Searcher As New DirectorySearcher(Root)
With Searcher
.Filter = "(sAMAccountName=" & Environment.UserName & ")"
.SearchScope = SearchScope.Subtree
.PropertiesToLoad.Add("mail")
End With
Dim Result As SearchResult = Searcher.FindOne
Dim EMailAddress As String
If Not Result Is Nothing Then
EMailAddress = Result.GetDirectoryEntry.Properties("mail").Value
End If
Return EMailAddress
End Function

"Steve Lloyd" <Re*************@laxeyRemovepartners.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
> Hi,
>
> I am trying to get someones email address out of the win 2003 server
> Directory using there logon name and the following code.
>
> Dim DSEntry As New System.DirectoryServices.DirectoryEntry("LDAP:" &
> System.Environment.GetEnvironmentVariable("LOGONSE RVER") & "/CN=" &
> System.Enviroment.Username & ",CN=Users,DC=laxeypartners,DC=com")
> EmailAddress = DSEntry.Properties("mail").Value.ToString()
>
> However, for some users the directory display name is not the same as the > logon name. e.g. Directory name is John Smith and logon name is
> John.Smith.
>
> When I use the logon name in the above code it does not find an entry
> in
> the
> directory.
>
> Does anyone know of a method that i can use to extract the default
> email
> address from the logon name.
>
> Many thanks,
>
> Steve Lloyd
>
>



Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.