HOWTO: Find a User's Home Exchange Server from a MAPI Session

ID: Q183917

The information in this article applies to:

SUMMARY

In some cases, you may want to determine on which Exchange Server a user's mailbox resides. One way to accomplish this is to create a Collaboration Data Objects (CDO) MAPI Session, a message and a recipient, and then use the properties associated with the resolved recipient to retrieve the Home Server.

MORE INFORMATION

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

Sample Code

   Option Explicit
    Const PR_EMS_AB_HOME_MTA = &H8007001E
    Public Function strHomeServer() As String
        'Requires a reference to the Microsoft CDO 1.2 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"
            .Resolve

            '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_MTA property.
            strRawServerInfo = .AddressEntry.Fields(PR_EMS_AB_HOME_MTA)

            '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=") + _
                        Len("/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
        objSession.Logoff
        Set objRecipient = Nothing
        Set objMessage = Nothing
        Set objOutbox = Nothing
        Set objSession = Nothing
    End Function

Keywords          : kbCDO120 kbMAPI kbMsg kbVBp kbGrpMsg 
Version           : WINDOWS:1.2
Platform          : WINDOWS
Issue type        : kbhowto

Last Reviewed: April 7, 1999