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

Email address from AD

P: n/a
I have a domain cluster with AD running, and I want to lookup a users email
address (exchange 2000 server is integrated with the AD system) so i can
email the user based on their user name. does anyone know how to look up the
email address? i would just use the user name as the alias but not all our
user names match their internal email addresses.. thanks
Nov 21 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Hi Brian,

We can use DirectoryService to do the job.
Here is the sample code you may take a look
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim de As DirectoryEntry = New DirectoryEntry("LDAP://CN=Peter
Huang ,CN=Users,DC=mydomain")

System.Diagnostics.Debug.WriteLine(de.Properties(" mail").Value.ToString())
End Sub
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #2

P: n/a
Brian,

I too have been attempting the same sort of task and have developed the
following. It's not espicially clean code, and if anyone else has a better
way of doing it i'd be happy to see it, but this does work on my net:

Imports System.DirectoryServices

Public Class AD_Mail3

Inherits System.Web.UI.Page

Enum ADS_USER_FLAG_Enum

ADS_UF_SCRIPT = 1

ADS_UF_ACCOUNTDISABLE = 2

ADS_UF_HOMEDIR_REQUIRED = 8

ADS_UF_LOCKOUT = 16

ADS_UF_PASSWD_NOTREQD = 32

ADS_UF_PASSWD_CANT_CHANGE = 64

ADS_UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 128

ADS_UF_TEMP_DUPLICATE_ACCOUNT = 256

ADS_UF_NORMAL_ACCOUNT = 512

ADS_UF_INTERDOMAIN_TRUST_ACCOUNT = 2048

ADS_UF_WORKSTATION_TRUST_ACCOUNT = 4096

ADS_UF_SERVER_TRUST_ACCOUNT = 8192

ADS_UF_DONT_EXPIRE_PASSWD = 65536

ADS_UF_MNS_LOGON_ACCOUNT = 131072

ADS_UF_SMARTCARD_REQUIRED = 262144

ADS_UF_TRUSTED_FOR_DELEGATION = 524288

ADS_UF_NOT_DELEGATED = 1048576

ADS_UF_USE_DES_KEY_ONLY = 2097152

ADS_UF_DONT_REQ_PREAUTH = 4194304

ADS_UF_PASSWORD_EXPIRED = 8388608

ADS_UF_TRUSTED_TO_AUTH_FOR_DELEGATION = 16777216

End Enum

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.

<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

End Sub

Protected WithEvents ListBox1 As System.Web.UI.WebControls.ListBox

Protected WithEvents lblTitle As System.Web.UI.WebControls.Label

Protected WithEvents lbl As System.Web.UI.WebControls.Label

Protected WithEvents Label2 As System.Web.UI.WebControls.Label

Protected WithEvents lblSam As System.Web.UI.WebControls.Label

Protected WithEvents lblMail As System.Web.UI.WebControls.Label

Protected WithEvents lblNote As System.Web.UI.WebControls.Label

Protected WithEvents chkGroups As System.Web.UI.WebControls.CheckBox

Protected WithEvents chkActive As System.Web.UI.WebControls.CheckBox

'NOTE: The following placeholder declaration is required by the Web Form
Designer.

'Do not delete or move it.

Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init

'CODEGEN: This method call is required by the Web Form Designer

'Do not modify it using the code editor.

InitializeComponent()

End Sub

#End Region

Dim txtSam As String

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

If Not IsPostBack Then

Populate_List()

End If
End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

'Show the user details in the other labels

Dim strSam As String

Dim strMail As String

strSam = Get_SamAcc(ListBox1.SelectedValue)

strMail = Get_Mail(strSam)

lblSam.Text = strSam

lblMail.Text = strMail

strMail = Nothing

strSam = Nothing

End Sub

Private Function Get_SamAcc(ByVal strName As String) As String

' Return the first SamAccountName value found in Active Directory

' whose name matches that supplied.

'bind the directory entry to the root of the domain

Dim dEntry As New DirectoryEntry(LDAP://<your server name here>)

Dim dSearch As New DirectorySearcher(dEntry)

'define the filter

dSearch.Filter = "(name=" & strName & ")"

dSearch.SearchScope = SearchScope.Subtree

'define the properties to retrieve

dSearch.PropertiesToLoad.Add("samAccountName")

'Define a collection to populate

Dim cResult As DirectoryEntry

'Excute the query

cResult = dSearch.FindOne.GetDirectoryEntry

'return the result

Get_SamAcc = cResult.Properties("samaccountname")(0)

cResult = Nothing

dSearch = Nothing

dEntry = Nothing

End Function

Private Function Get_Mail(ByVal strName As String) As String

' Return the first Mail address value found in Active Directory

' whose name matches that supplied.

'bind the directory entry to the root of the domain

Dim dEntry As New DirectoryEntry(LDAP://<your server name here>)

Dim dSearch As New DirectorySearcher(dEntry)

'define the filter

dSearch.Filter = "(samAccountName=" & strName & ")"

dSearch.SearchScope = SearchScope.Subtree

'define the properties to retrieve

dSearch.PropertiesToLoad.Add("mail")

'Define a collection to populate

Dim cResult As SearchResultCollection

Dim oRes As SearchResult

'Excute the query

cResult = dSearch.FindAll

'Add the user mail address to the label

Try

For Each oRes In cResult

Get_Mail = Get_Mail & vbCrLf & ores.Properties("mail")(0)

Next

Catch ex As Exception

Get_Mail = "No address assigned"

End Try

ores = Nothing

cResult = Nothing

dSearch = Nothing

dEntry = Nothing

End Function

Private Function Get_Attribs(ByVal strAccControl As String) As String

' Function to return the properties set for the UserAccountControl value

If ADS_USER_FLAG_Enum.ADS_UF_SCRIPT And strAccControl Then Get_Attribs =
Get_Attribs & "Script"

If ADS_USER_FLAG_Enum.ADS_UF_ACCOUNTDISABLE And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Disabled"

If ADS_USER_FLAG_Enum.ADS_UF_HOMEDIR_REQUIRED And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Disabled"

If ADS_USER_FLAG_Enum.ADS_UF_LOCKOUT And strAccControl Then Get_Attribs =
Get_Attribs & vbCrLf & "Locked out"

If ADS_USER_FLAG_Enum.ADS_UF_PASSWD_NOTREQD And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "No password required"

If ADS_USER_FLAG_Enum.ADS_UF_PASSWD_CANT_CHANGE And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Can't change password"

If ADS_USER_FLAG_Enum.ADS_UF_ENCRYPTED_TEXT_PASSWORD_ ALLOWED And
strAccControl Then Get_Attribs = Get_Attribs & vbCrLf & "Encrypted text
password allowed"

If ADS_USER_FLAG_Enum.ADS_UF_TEMP_DUPLICATE_ACCOUNT And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Temporary duplicate account"

If ADS_USER_FLAG_Enum.ADS_UF_NORMAL_ACCOUNT And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Normal account"

If ADS_USER_FLAG_Enum.ADS_UF_INTERDOMAIN_TRUST_ACCOUN T And strAccControl
Then Get_Attribs = Get_Attribs & vbCrLf & "Inter domain account"

If ADS_USER_FLAG_Enum.ADS_UF_WORKSTATION_TRUST_ACCOUN T And strAccControl
Then Get_Attribs = Get_Attribs & vbCrLf & "Workstation trust account"

If ADS_USER_FLAG_Enum.ADS_UF_SERVER_TRUST_ACCOUNT And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Server trust account"

If ADS_USER_FLAG_Enum.ADS_UF_DONT_EXPIRE_PASSWD And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Don't expire password"

If ADS_USER_FLAG_Enum.ADS_UF_MNS_LOGON_ACCOUNT And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "MNS logon account"

If ADS_USER_FLAG_Enum.ADS_UF_SMARTCARD_REQUIRED And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Smartcard requires"

If ADS_USER_FLAG_Enum.ADS_UF_TRUSTED_FOR_DELEGATION And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Trusted for delegation"

If ADS_USER_FLAG_Enum.ADS_UF_NOT_DELEGATED And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Not delegated"

If ADS_USER_FLAG_Enum.ADS_UF_USE_DES_KEY_ONLY And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Use DES key only"

If ADS_USER_FLAG_Enum.ADS_UF_DONT_REQ_PREAUTH And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Don't require preauthorisation"

If ADS_USER_FLAG_Enum.ADS_UF_PASSWORD_EXPIRED And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Password expired"

If ADS_USER_FLAG_Enum.ADS_UF_TRUSTED_TO_AUTH_FOR_DELE GATION And
strAccControl Then Get_Attribs = Get_Attribs & vbCrLf & "Trusted to
authorise for delegation"

End Function

Private Function Is_Active(ByVal strAccControl As Integer) As Boolean

' Function to test if user Account control code indicates if disabled

If ADS_USER_FLAG_Enum.ADS_UF_ACCOUNTDISABLE And strAccControl Then

' Account has been disabled

Is_Active = False

Else

Is_Active = True

End If

End Function

Private Sub chkActive_CheckedChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles chkActive.CheckedChanged

' Clear the list and then repopulate

ListBox1.Items.Clear()

Populate_List()

End Sub

Private Sub chkGroups_CheckedChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles chkGroups.CheckedChanged

' Clear the list and then repopulate

ListBox1.Items.Clear()

Populate_List()

End Sub

Private Sub Populate_List()

'Populate the list box, but initially exclude inactive and special accounts

'bind the directory entry to the root of the domain

Dim dEntry As New DirectoryEntry(LDAP://<your server name here>)

Dim dSearch As New DirectorySearcher(dEntry)

Dim sUsr As Object

Dim intAcc As Integer

'define the filter

dSearch.Filter = "(&(objectCategory=person)(objectClass=user))"

dSearch.SearchScope = SearchScope.Subtree

'define the properties to retrieve

dSearch.PropertiesToLoad.Add("Name")

dSearch.PropertiesToLoad.Add("UserAccountControl")

'Define the sort order

dSearch.Sort.Direction = SortDirection.Ascending

dSearch.Sort.PropertyName = "Name"

'Define a collection to populate

Dim cResult As SearchResultCollection

'Excute the query

cResult = dSearch.FindAll

Dim oRes As SearchResult

'query the collection and add each user name to the combo

For Each oRes In cResult

sUsr = oRes.Properties("name")(0)

intAcc = oRes.Properties("UserAccountControl")(0)

If chkGroups.Checked = False Then

' Exclude the names shown in the select case statement

Select Case True 'sUsr

Case sUsr = "Guest", sUsr = "TsInternetUser", sUsr = "Simon"

' don't show

Case sUsr.startswith("SystemMailbox")

' don't show

Case sUsr.startswith("IWAM")

' don't show

Case sUsr.startswith("IUSR")

' don't show

Case Else

If Me.chkActive.Checked Then

ListBox1.Items.Add(sUsr)

Else

If Is_Active(intAcc) Then

ListBox1.Items.Add(sUsr)

End If

End If

End Select

Else

' Show all names

ListBox1.Items.Add(sUsr)

End If

Next

oRes = Nothing

cResult = Nothing

intAcc = Nothing

sUsr = Nothing

dSearch = Nothing

dEntry = Nothing

End Sub

End Class



HTH

<M>ike

"Brian Henry" <br**********@newsgroups.nospam> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I have a domain cluster with AD running, and I want to lookup a users email address (exchange 2000 server is integrated with the AD system) so i can
email the user based on their user name. does anyone know how to look up the email address? i would just use the user name as the alias but not all our
user names match their internal email addresses.. thanks

Nov 21 '05 #3

P: n/a
thanks

""Peter Huang"" <v-******@online.microsoft.com> wrote in message
news:nP*************@cpmsftngxa06.phx.gbl...
Hi Brian,

We can use DirectoryService to do the job.
Here is the sample code you may take a look
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim de As DirectoryEntry = New DirectoryEntry("LDAP://CN=Peter
Huang ,CN=Users,DC=mydomain")

System.Diagnostics.Debug.WriteLine(de.Properties(" mail").Value.ToString())
End Sub
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 21 '05 #4

P: n/a
hi peter, I tried the following

Dim de As DirectoryEntry = New DirectoryEntry("LDAP://CN=brian_henry
,CN=Users,DC=ourdomain")

and i got an exception back when i tried to execute the

de.properties("mail").value.tostring and the exception said

"a reference was returned" and that was it... nothing more descriptive, what
would cause this? thanks
""Peter Huang"" <v-******@online.microsoft.com> wrote in message
news:nP*************@cpmsftngxa06.phx.gbl...
Hi Brian,

We can use DirectoryService to do the job.
Here is the sample code you may take a look
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim de As DirectoryEntry = New DirectoryEntry("LDAP://CN=Peter
Huang ,CN=Users,DC=mydomain")

System.Diagnostics.Debug.WriteLine(de.Properties(" mail").Value.ToString())
End Sub
Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 21 '05 #5

P: n/a
Hi Brian,

I think the problem may be caused by we did not specified the full path to
the specified user.
Is the outdomain your root domain where the user brian_henry in?
e.g. I am a user in the submain1.submain2.microsoft.com, and my display
name in the outlook is Peter Huang.
Then my Ldap path is as below.
LDAP://CN=Peter Huang,CN=Users,DC=submain1,DC=submain2, DC=microsoft,DC=com

You may change your ldap path according to your scenario to see if that
works.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #6

P: n/a
our domain is pretty much the only one there is, its all at the root domain,
there isn't anything really below it. the only thing below is an
Organization unit which holds the users called Employees and the users are
placed into that OU

""Peter Huang"" <v-******@online.microsoft.com> wrote in message
news:ME*************@cpmsftngxa06.phx.gbl...
Hi Brian,

I think the problem may be caused by we did not specified the full path to
the specified user.
Is the outdomain your root domain where the user brian_henry in?
e.g. I am a user in the submain1.submain2.microsoft.com, and my display
name in the outlook is Peter Huang.
Then my Ldap path is as below.
LDAP://CN=Peter Huang,CN=Users,DC=submain1,DC=submain2,
DC=microsoft,DC=com

You may change your ldap path according to your scenario to see if that
works.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 21 '05 #7

P: n/a
Hi Brian,

I think we may try to use the ldp.exe tool to get the Distinguished name of
the user.
The tool can be found in the support.cab which is located at the OS
installation disck's \support\tools directory.
Here the OS means windows 2000/XP/2003.

Here are some articles about how to use the ldp tool.
XADM: How to Use the Windows 2000 LDP Support Tool to View the BaseDN
http://support.microsoft.com/?id=278422

How to Use Ldp.exe to View Entire Directory Tree and Locate the Microsoft
Exchange Container
http://support.microsoft.com/?id=252335

After connect and bind to the AD server, we can right click on the tree
root and select search.
In the filter section, we can input (name=Peter huang,changed it according
to your scenario) and select the Subtree in the Scope.
Click OK, we should find the result on the right panel.
The distinguishedName is what we want.

If you can not connect or bind to the server, I think you may not have
enough permission to do the job, you may need to contact your AD
administrator.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #8

P: n/a
this seems to work fine for full name queries, but I need to query it based
on the user name to get the email address back, i got the connection working
just fine now to LDAP.
""Peter Huang"" <v-******@online.microsoft.com> wrote in message
news:Ag**************@cpmsftngxa06.phx.gbl...
Hi Brian,

I think we may try to use the ldp.exe tool to get the Distinguished name
of
the user.
The tool can be found in the support.cab which is located at the OS
installation disck's \support\tools directory.
Here the OS means windows 2000/XP/2003.

Here are some articles about how to use the ldp tool.
XADM: How to Use the Windows 2000 LDP Support Tool to View the BaseDN
http://support.microsoft.com/?id=278422

How to Use Ldp.exe to View Entire Directory Tree and Locate the Microsoft
Exchange Container
http://support.microsoft.com/?id=252335

After connect and bind to the AD server, we can right click on the tree
root and select search.
In the filter section, we can input (name=Peter huang,changed it according
to your scenario) and select the Subtree in the Scope.
Click OK, we should find the result on the right panel.
The distinguishedName is what we want.

If you can not connect or bind to the server, I think you may not have
enough permission to do the job, you may need to contact your AD
administrator.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 21 '05 #9

P: n/a
Hi Brian,

If so, I think we can use the directoryseacher class to get the user object
and then get the mail property.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim s As DirectorySearcher = New
DirectorySearcher("(sAMAccountName=v-phuang)")
Dim r As DirectoryEntry = s.FindOne().GetDirectoryEntry()
Debug.WriteLine(r.Properties("mail").Value.ToStrin g())
End Sub

SearchResult Class
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemdirectoryservicessearchresultclasstopic .asp

You may have a try.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #10

P: n/a
that worked thanks

""Peter Huang"" <v-******@online.microsoft.com> wrote in message
news:Em**************@cpmsftngxa06.phx.gbl...
Hi Brian,

If so, I think we can use the directoryseacher class to get the user
object
and then get the mail property.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim s As DirectorySearcher = New
DirectorySearcher("(sAMAccountName=v-phuang)")
Dim r As DirectoryEntry = s.FindOne().GetDirectoryEntry()
Debug.WriteLine(r.Properties("mail").Value.ToStrin g())
End Sub

SearchResult Class
http://msdn.microsoft.com/library/de...us/cpref/html/
frlrfsystemdirectoryservicessearchresultclasstopic .asp

You may have a try.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no
rights.

Nov 21 '05 #11

P: n/a
Hi,

I am glad the problem resolved.
Cheers!

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 21 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.