從 AD 讀取 EXCHANGE 使用者 EMAIL Imports DirectoryServices



search


 Dim ReturnList As New DataTable
        With ReturnList
            .Columns.Add("Name")
            .Columns.Add("Email")
        End With

        Dim entry As DirectoryEntry = New DirectoryEntry("LDAP://Domain.com.tw", "ADAcc", "PWD")
        Dim searcher As DirectorySearcher = New DirectorySearcher("(&(mailnickname=*)(objectCategory=person))")

        searcher.SearchRoot = entry
        searcher.SearchScope = SearchScope.Subtree

        For Each result As System.DirectoryServices.SearchResult In searcher.FindAll()
            Dim properties As ResultPropertyCollection = result.Properties

            Dim name As ResultPropertyValueCollection = properties("name")
            Dim proxyAddresses As ResultPropertyValueCollection = properties("proxyAddresses")
            Dim legacyExchangeDN As ResultPropertyValueCollection = properties("legacyExchangeDN")

            Dim i As Integer
            For i = 0 To proxyAddresses.Count - 1
                Dim address As String = proxyAddresses(i).ToString()
                If (address.StartsWith("SMTP:")) Then
                    With ReturnList
                        Dim newRow As DataRow = .NewRow()
                        newRow("Name") = name(0)
                        newRow("Email") = address
                        .Rows.Add(newRow)
                    End With
                End If
            Next
        Next
        ShowMsgOnNewForm(ReturnList)


module


 Sub ShowMsgOnNewForm(ByVal tmpDT As DataTable)
        Dim nF As New Form
        With nF
            .Name = "ReturnList"
            .Text = "電子郵件資料"
            .Size = New Size(600, 400)
            .StartPosition = FormStartPosition.CenterScreen
            .FormBorderStyle = Windows.Forms.FormBorderStyle.FixedToolWindow
            .MaximizeBox = False
            .MinimizeBox = False
            .TopMost = True
            .Show()
        End With

        Dim dgvEMail As New DataGridView
        With dgvEMail
            .Name = "NewDGVEMail"
            .Location = New Point(0, 0)
            .Size = New Size(nF.Width - 5, nF.Height - 45)

            .AllowUserToAddRows = False
            .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

            .RowHeadersVisible = False
            .EditMode = DataGridViewEditMode.EditOnEnter
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .ColumnHeadersDefaultCellStyle.Font = New Font("新細明體", 11)
            .RowsDefaultCellStyle.Font = New Font("新細明體", 11)
            .RowsDefaultCellStyle.SelectionBackColor = Color.GreenYellow
            .RowsDefaultCellStyle.SelectionForeColor = Color.Black
            .MultiSelect = False
            .ReadOnly = True

            For Each DGV As DataGridViewColumn In .Columns
                DGV.SortMode = DataGridViewColumnSortMode.Automatic
            Next DGV
        End With
        nF.Controls.Add(dgvEMail)

        With dgvEMail
            .DataSource = tmpDT
            .Columns("Name").Width = 200
            .Columns("Email").Width = 400
        End With

        Dim ss As New StatusStrip
        Dim ts As New ToolStripLabel
        ts.Text = dgvEMail.Rows.Count & " rows"
        ss.Items.Add(ts)
        nF.Controls.Add(ss)
    End Sub

沒有留言:

張貼留言