How to find a user's computer that is running Exchange Server by using a MAPI session

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

On This Page


In some cases, you may want to determine the computer that is running Microsoft Exchange Server on which a user's mailbox resides. One way to do this is to create a Collaboration Data Objects (1.2, 1.21) MAPI Session, a message and a recipient. Then, use the properties that are associated with the resolved recipient to retrieve the Home Server.


The following code sample demonstrates how to find a user's computer that is running Exchange Server by manipulating the text that is returned from a MAPI property.

Code sample

   Option Explicit
    Const PR_EMS_AB_HOME_MDB = &H8006001E
    Public Function strHomeServer() As String
        'Requires a reference to the Microsoft CDO (1.2, 1.21) Library
        Dim objSession As MAPI.Session
        Dim objOutbox As Folder
        Dim objMessage As Message
        Dim objRecipient As Recipient
        Dim strProfileInfo As String
        Dim strServer As String
        Dim strMyMailbox As String
        Dim strRawServerInfo As String
        Dim intStartOfServer As Integer
        Dim intNextSlash As Integer

        Set objSession = New MAPI.Session

        'You need to log onto a MAPI Session. To do this, you need
        'to know either a profile on the current system that is
        'owned by whoever is logged onto the system, or you need to
        'know the mailbox of the person logged on, and the name of
        'an Exchange Server on the same site as the person you want
        'to find the home server.
        'This example uses logon "on the fly" and passes the name of
        'an Exchange Server and the mailbox of the logged on user
        strServer = "AnExchangeServer"
        strMyMailbox = "MyMailbox"
        strProfileInfo = strServer & vbLf & strMyMailbox
        objSession.Logon "", "", False, True, 0, True, strProfileInfo

        'This code is more verbose than is required. You could accomplish
        'this task without creating an Outbox object. The verbose code
        'is for more clarity in what is taking place.

        'Create an Outbox object and add a message to it.
        Set objOutbox = objSession.Outbox
        Set objMessage = objOutbox.Messages.Add
        Set objRecipient = objMessage.Recipients.Add
        With objRecipient
            'Insert the UserID (alias) of the person who you want
            'to find the Home Server for and resolve the name.
            .Name = "AliasToFindHomeServerOf"

            'The string manipulation code is very verbose.
            'You could accomplish the same thing in one line
            'of code. Breaking the string manipulation out makes
            'what is happening more clear.
            'Add Debug.Print statements and step the code to
            'see what is happening.

            'Retrieve the raw information from the
            'PR_EMS_AB_HOME_MDB property.
            strRawServerInfo = .AddressEntry.Fields(PR_EMS_AB_HOME_MDB)

            'Find the position in the string where the server name starts.
            'We know that just before the server name is a certain string.
            'Look for the string just before the server name using InStr,
            'this will tell you where this string begins.
            'Then add the length of the string you are searching for
            'to find the position where the server name starts.
            intStartOfServer = InStr(1, strRawServerInfo, _
                        "/cn=Configuration/cn=Servers/cn=") + _

            'Find the position where the server name ends.
            'We know that a "/" character follows the server name, so find
            'the position of the next slash, starting from where the server
            'name starts.
            intNextSlash = InStr(intStartOfServer, _
                            strRawServerInfo, _

            'Now that we know where the server name starts and ends,
            'use Mid to extract that information.
            strHomeServer = Mid(strRawServerInfo, _
                                intStartOfServer, _
                                intNextSlash - intStartOfServer)
        End With
        Set objRecipient = Nothing
        Set objMessage = Nothing
        Set objOutbox = Nothing
        Set objSession = Nothing
    End Function


Article ID: 183917 - Last Review: May 19, 2005 - Revision: 3.0
  • Microsoft Collaboration Data Objects 1.2
  • Microsoft Collaboration Data Objects 1.21
kbfaq kbhowto kbmsg KB183917
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