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.Director yServices
Public Class AD_Mail3
Inherits System.Web.UI.P age
Enum ADS_USER_FLAG_E num
ADS_UF_SCRIPT = 1
ADS_UF_ACCOUNTD ISABLE = 2
ADS_UF_HOMEDIR_ REQUIRED = 8
ADS_UF_LOCKOUT = 16
ADS_UF_PASSWD_N OTREQD = 32
ADS_UF_PASSWD_C ANT_CHANGE = 64
ADS_UF_ENCRYPTE D_TEXT_PASSWORD _ALLOWED = 128
ADS_UF_TEMP_DUP LICATE_ACCOUNT = 256
ADS_UF_NORMAL_A CCOUNT = 512
ADS_UF_INTERDOM AIN_TRUST_ACCOU NT = 2048
ADS_UF_WORKSTAT ION_TRUST_ACCOU NT = 4096
ADS_UF_SERVER_T RUST_ACCOUNT = 8192
ADS_UF_DONT_EXP IRE_PASSWD = 65536
ADS_UF_MNS_LOGO N_ACCOUNT = 131072
ADS_UF_SMARTCAR D_REQUIRED = 262144
ADS_UF_TRUSTED_ FOR_DELEGATION = 524288
ADS_UF_NOT_DELE GATED = 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_DEL EGATION = 16777216
End Enum
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
End Sub
Protected WithEvents ListBox1 As System.Web.UI.W ebControls.List Box
Protected WithEvents lblTitle As System.Web.UI.W ebControls.Labe l
Protected WithEvents lbl As System.Web.UI.W ebControls.Labe l
Protected WithEvents Label2 As System.Web.UI.W ebControls.Labe l
Protected WithEvents lblSam As System.Web.UI.W ebControls.Labe l
Protected WithEvents lblMail As System.Web.UI.W ebControls.Labe l
Protected WithEvents lblNote As System.Web.UI.W ebControls.Labe l
Protected WithEvents chkGroups As System.Web.UI.W ebControls.Chec kBox
Protected WithEvents chkActive As System.Web.UI.W ebControls.Chec kBox
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub
#End Region
Dim txtSam As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
If Not IsPostBack Then
Populate_List()
End If
End Sub
Private Sub ListBox1_Select edIndexChanged( ByVal sender As System.Object,
ByVal e As System.EventArg s) Handles ListBox1.Select edIndexChanged
'Show the user details in the other labels
Dim strSam As String
Dim strMail As String
strSam = Get_SamAcc(List Box1.SelectedVa lue)
strMail = Get_Mail(strSam )
lblSam.Text = strSam
lblMail.Text = strMail
strMail = Nothing
strSam = Nothing
End Sub
Private Function Get_SamAcc(ByVa l 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 DirectorySearch er(dEntry)
'define the filter
dSearch.Filter = "(name=" & strName & ")"
dSearch.SearchS cope = SearchScope.Sub tree
'define the properties to retrieve
dSearch.Propert iesToLoad.Add(" samAccountName" )
'Define a collection to populate
Dim cResult As DirectoryEntry
'Excute the query
cResult = dSearch.FindOne .GetDirectoryEn try
'return the result
Get_SamAcc = cResult.Propert ies("samaccount name")(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 DirectorySearch er(dEntry)
'define the filter
dSearch.Filter = "(samAccountNam e=" & strName & ")"
dSearch.SearchS cope = SearchScope.Sub tree
'define the properties to retrieve
dSearch.Propert iesToLoad.Add(" mail")
'Define a collection to populate
Dim cResult As SearchResultCol lection
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(ByV al strAccControl As String) As String
' Function to return the properties set for the UserAccountCont rol value
If ADS_USER_FLAG_E num.ADS_UF_SCRI PT And strAccControl Then Get_Attribs =
Get_Attribs & "Script"
If ADS_USER_FLAG_E num.ADS_UF_ACCO UNTDISABLE And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Disabled"
If ADS_USER_FLAG_E num.ADS_UF_HOME DIR_REQUIRED And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Disabled"
If ADS_USER_FLAG_E num.ADS_UF_LOCK OUT And strAccControl Then Get_Attribs =
Get_Attribs & vbCrLf & "Locked out"
If ADS_USER_FLAG_E num.ADS_UF_PASS WD_NOTREQD And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "No password required"
If ADS_USER_FLAG_E num.ADS_UF_PASS WD_CANT_CHANGE And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Can't change password"
If ADS_USER_FLAG_E num.ADS_UF_ENCR YPTED_TEXT_PASS WORD_ALLOWED And
strAccControl Then Get_Attribs = Get_Attribs & vbCrLf & "Encrypted text
password allowed"
If ADS_USER_FLAG_E num.ADS_UF_TEMP _DUPLICATE_ACCO UNT And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Temporary duplicate account"
If ADS_USER_FLAG_E num.ADS_UF_NORM AL_ACCOUNT And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Normal account"
If ADS_USER_FLAG_E num.ADS_UF_INTE RDOMAIN_TRUST_A CCOUNT And strAccControl
Then Get_Attribs = Get_Attribs & vbCrLf & "Inter domain account"
If ADS_USER_FLAG_E num.ADS_UF_WORK STATION_TRUST_A CCOUNT And strAccControl
Then Get_Attribs = Get_Attribs & vbCrLf & "Workstatio n trust account"
If ADS_USER_FLAG_E num.ADS_UF_SERV ER_TRUST_ACCOUN T And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Server trust account"
If ADS_USER_FLAG_E num.ADS_UF_DONT _EXPIRE_PASSWD And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Don't expire password"
If ADS_USER_FLAG_E num.ADS_UF_MNS_ LOGON_ACCOUNT And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "MNS logon account"
If ADS_USER_FLAG_E num.ADS_UF_SMAR TCARD_REQUIRED And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Smartcard requires"
If ADS_USER_FLAG_E num.ADS_UF_TRUS TED_FOR_DELEGAT ION And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Trusted for delegation"
If ADS_USER_FLAG_E num.ADS_UF_NOT_ DELEGATED And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Not delegated"
If ADS_USER_FLAG_E num.ADS_UF_USE_ DES_KEY_ONLY And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Use DES key only"
If ADS_USER_FLAG_E num.ADS_UF_DONT _REQ_PREAUTH And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Don't require preauthorisatio n"
If ADS_USER_FLAG_E num.ADS_UF_PASS WORD_EXPIRED And strAccControl Then
Get_Attribs = Get_Attribs & vbCrLf & "Password expired"
If ADS_USER_FLAG_E num.ADS_UF_TRUS TED_TO_AUTH_FOR _DELEGATION 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_E num.ADS_UF_ACCO UNTDISABLE And strAccControl Then
' Account has been disabled
Is_Active = False
Else
Is_Active = True
End If
End Function
Private Sub chkActive_Check edChanged(ByVal sender As Object, ByVal e As
System.EventArg s) Handles chkActive.Check edChanged
' Clear the list and then repopulate
ListBox1.Items. Clear()
Populate_List()
End Sub
Private Sub chkGroups_Check edChanged(ByVal sender As Object, ByVal e As
System.EventArg s) Handles chkGroups.Check edChanged
' 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 DirectorySearch er(dEntry)
Dim sUsr As Object
Dim intAcc As Integer
'define the filter
dSearch.Filter = "(&(objectCateg ory=person)(obj ectClass=user)) "
dSearch.SearchS cope = SearchScope.Sub tree
'define the properties to retrieve
dSearch.Propert iesToLoad.Add(" Name")
dSearch.Propert iesToLoad.Add(" UserAccountCont rol")
'Define the sort order
dSearch.Sort.Di rection = SortDirection.A scending
dSearch.Sort.Pr opertyName = "Name"
'Define a collection to populate
Dim cResult As SearchResultCol lection
'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 ("UserAccountCo ntrol")(0)
If chkGroups.Check ed = 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.Ch ecked Then
ListBox1.Items. Add(sUsr)
Else
If Is_Active(intAc c) 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**********@n ewsgroups.nospa m> wrote in message
news:%2******** ********@TK2MSF TNGP11.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