Solved

AD connection get mail properties problem

Posted on 2011-03-08
7
743 Views
Last Modified: 2012-05-11
Hi,

VB, VS 2010express,

Im trying to get email properties from AD, when I do

 Public Shared Sub ADconnection()

        Dim oSearcher As New DirectorySearcher
        Dim oResults As SearchResultCollection
        Dim oResult As SearchResult
        Dim mCount As Integer
        Dim mLDAPRecord, mLDAPemail, mLDAPFullname As String

        Dim ResultFields() As String = {"securityEquals", "cn"}

        Try
            With oSearcher
                .SearchRoot = New DirectoryEntry("LDAP://" & "domainserver" & _
          "/dc=xxx,dc=local")
                .PropertiesToLoad.AddRange(ResultFields)
                .Filter = "sn=" & "*"
                oResults = .FindAll()
            End With

            mCount = oResults.Count
            If mCount > 0 Then
                For Each oResult In oResults
                    mLDAPRecord = oResult.GetDirectoryEntry().Properties("cn").Value
                    mLDAPemail = oResult.GetDirectoryEntry().Properties("mail").Value
                    mLDAPFullname = oResult.GetDirectoryEntry().Properties("sn").Value
                   
                Next
            End If
        Catch e As Exception

            MsgBox("Error is " & e.Message)

        End Try

    End Sub

Open in new window



then everything is working but if I change filter="cn=username" then even if he find a record it's not taking email address
any reason why?

thanks for all advise
regards
0
Comment
Question by:gowerman
  • 4
  • 3
7 Comments
 
LVL 11

Expert Comment

by:Tasmant
ID: 35067918
could it not be possible that your recordset don't contain the expected property?
Dim ResultFields() As String = {"securityEquals", "cn"}
I would change it to:
Dim ResultFields() As String = {"securityEquals", "cn", "mail"}
and maybe more to:
Dim ResultFields() As String = {"securityEquals", "cn", "mail", "sn"}

0
 

Author Comment

by:gowerman
ID: 35068274
i change as you said but still same. I checked several records they all contain that property as when I run the first one it takes all users emails
0
 
LVL 11

Expert Comment

by:Tasmant
ID: 35068368
this attribute shouldn't be a multivalued attribute, but i've found some code
mLDAPemail = oResult.GetDirectoryEntry().Properties("mail")(0).toString()
mLDAPemail = oResult.GetDirectoryEntry().Properties("mail")(0).Value
maybe can you try ...
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:gowerman
ID: 35068810
when I tried this then I have index is out of range
0
 
LVL 11

Expert Comment

by:Tasmant
ID: 35068874
ok so this is singlevalued, that's fine. Don't know why you encounter the issue, i'll try to investigate further.
0
 

Accepted Solution

by:
gowerman earned 0 total points
ID: 35892591
I search by samaccount and that is working now
0
 

Author Closing Comment

by:gowerman
ID: 35913771
thats works for me
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now