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