How To Use CDO (1.x) to Loop Through All Mailboxes in the GAL

Article translations Article translations
Article ID: 203019 - View products that this article applies to.
This article was previously published under Q203019
Expand all | Collapse all


Using CDO (1.2, 1.21), you can obtain a list of mailboxes from the Global Address List (GAL), and then using the ProfileInfo parameter of the Session.Logon method, you can loop through all of the mailboxes. Within that loop, you can either get information from the mailbox or perform maintenance on the mailboxes. This article demonstrates how to do this.


NOTE: To be able to log onto each mailbox, you must have sufficient permissions on each of the mailboxes. The section of the code that retrieves the mailbox names from the GAL needs permission only to view the GAL (as noted in the code), which most users can do. The section of code that loops through the mailboxes in the array needs Service Account Administrator or greater permissions. Service Account Administrator permissions are required due to the fact that the application logs on to all of the mailboxes. Required permissions may also depend on what actions are being performed on each mailbox.

Here are the steps to create an application that will logon to all of the mailboxes in the Global Address List:

  1. Create a new project (Standard EXE) in Visual Basic.
  2. Remove the form that is automatically added to the project.
  3. Add a new module to the project.
  4. Add a project reference to the Microsoft CDO 1.2 (or higher) Library
  5. Add the following code to the module:
    Const CdoPR_EMS_AB_HOME_MTA = &H8007001E
    Sub GetMailboxList (objSession As MAPI.Session, _
                        aMailbox() As String, _
                        aServer() As String)
       Dim oGAL             As AddressList
       Dim oAdrEntries      As AddressEntries
       Dim oAdrEntry        As AddressEntry
       Dim iCnt             As Integer
       Dim strRawServerInfo As String
       Dim iStartServerName As Integer
       Dim iEndServerName   As Integer
       Dim strHomeServer    As String
       Dim strMailboxName   As String
       Dim strFindServer    As String
       strFindServer = "/cn=Configuration/cn=Servers/cn="
       Set oGAL = objSession.AddressLists("Global Address List")
       Set oAdrEntries = oGAL.AddressEntries
       iCnt = 0
       For Each oAdrEntry In oAdrEntries
          'Ensure that only mailboxes are included, not custom recipients,
          'distribution lists, or public folder entries in the GAL
          If oAdrEntry.DisplayType = CdoUser Then
             'Get the Home Server information from the AddressEntry 
             'Fields collection
             strRawServerInfo = oAdrEntry.Fields(CdoPR_EMS_AB_HOME_MTA)
             iStartServerName = InStr(1, strRawServerInfo, _
                                strFindServer) + Len(strFindServer)
             iEndServerName = InStr(iStartServerName, strRawServerInfo, "/")
             strHomeServer = Mid(StrRawServerInfo, iStartServerName, _
                             iEndServerName - iStartServerName)
             'Get the mailbox name of AddressEntry
             strMailboxName = oAdrEntry.Fields(CdoPR_ACCOUNT).Value
             Debug.Print "Server: " & strHomeServer
             Debug.Print "Mailbox: " & strMailboxName
             iCnt = iCnt + 1
             ReDim Preserve aMailbox(iCnt)
             ReDim Preserve aServer(iCnt)
             aServer(iCnt) = strHomeServer
             aMailbox(iCnt) = strMailboxName
          End If
       Set oAdrEntry = Nothing
       Set oAdrEntries = Nothing
       Set oGAL = Nothing
    End Sub
    Private Sub Main()
       Dim oSession As MAPI.Session
       ReDim aMailboxList(1) As String
       ReDim aserverlist(1) As String
       Dim iBigLoop As Integer
       Dim sServerName As String
       Dim sProfileInfo As String
       'TO DO: Change "ServerName" to the name of your Exchange Server
       sServerName = "ServerName"
       'TO DO: Change "MailboxName" to the name of a mailbox on the
       '  server specified above
       sProfileInfo = sServerName & vbLf & "MailboxName"
       Set oSession = CreateObject("MAPI.Session")
       oSession.Logon profileinfo:=sProfileInfo
       GetMailboxList oSession, aMailboxList, aserverlist
       For iBigLoop = 1 To UBound(aMailboxList)
          sProfileInfo = aserverlist(iBigLoop) & vbLf & aMailboxList(iBigLoop)
          oSession.Logon profileinfo:=sProfileInfo
          Debug.Print oSession.CurrentUser.Name
          ' Do any processing here for each mailbox
       Next iBigLoop
       Set oSession = Nothing
    End Sub
  6. Compile and run the project.


For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
171440 INFO: Where to acquire the CDO libraries (all versions)
177851 How To Build a CDO (1.x) messaging application to run from a service
200018 INFO: Differences between CDO, Simple MAPI, and Extended MAPI
262054 XADM: How to get service account access to all mailboxes in Exchange 2000
291169 OL2002: General information about CDO 1.21s


Article ID: 203019 - Last Review: July 13, 2004 - Revision: 2.4
  • Microsoft Collaboration Data Objects 1.2
  • Microsoft Collaboration Data Objects 1.21
kbhowto kbmsg KB203019
Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

Give Feedback


Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from