Using Collaboration Data Objects
Discover how CDO, a programming interface that you can use with Outlook, lets you find and display an Exchange email sender's SMTP address.
February 18, 2002
CDO lets you show an SMTP address for Exchange email senders
In "Showing the Sender's Email Address," June 2000, InstantDoc ID 8630, I demonstrated how to use Outlook VBA code to monitor your Inbox folder and, for each new item, display the sender's actual email address—not just a display name—in the folder. However, for users in the same Microsoft Exchange Server 5.5 organization, this technique shows the internal X.400 address instead of an easy-to-read SMTP address.
To display an SMTP address for Exchange senders as well as external senders, you need Collaboration Data Objects. CDO, an alternative programming interface that you can use with Outlook, provides a way to obtain many properties that the Outlook object model doesn't expose.
Preliminaries
Neither Outlook 2002 nor Outlook 2000 installs CDO by default. Before working with this article's code, determine whether the CDO component is present on your system and install it if necessary. You can use the Control Panel Add/Remove Programs applet to launch the Outlook or Office maintenance dialog box and follow the wizard to check your configuration. Look among the Outlook components for Collaboration Data Objects. You'll also need to use Tools, References in Outlook VBA to add the Microsoft CDO 1.21 library to your project.
Put the code in Listing 1 in the Outlook VBA window's ThisOutlookSession module. If you have other code that you want to run against new messages in the Inbox, include it in the colInboxItems_ItemAdd procedure, inside the If Item.Class = olMail Then ... End If block.
Restart Outlook and send yourself a message to test the code. One important note: Because the code accesses the address book, the code is subject to the controls of the Outlook E-mail Security Update. Specifically, for users who have Outlook 2000 with the security update or who have Outlook 2002, the code will display a prompt to which the user must respond before the code can proceed, unless the Exchange administrator allows unrestricted programmatic access to the address book.
How It Works
The colInboxItems_ItemAdd procedure uses the UserProperties.Add() method to add both the FromAddress and SenderPhone properties to the incoming message. You can use the Field Chooser to add these fields to the folder view. Right-click the Inbox's column headings and select Field Chooser. Select User-defined fields in Inbox, and drag the new fields to the column headings.
The code included in "Showing the Sender's Email Address" used the CDO Message.Sender.Address property to get the sender's address. The more sophisticated version here uses the Fields collection to gather two additional pieces of information from the AddressEntry object called Sender: the type of address and, for senders with Exchange addresses, the default SMTP address.
The Fields collection is simple to use. The expression objAE.Fields(CdoPR_ADDRTYPE) returns the address type (e.g., EX—Exchange Server, SMTP) from the objAE AddressEntry object. The hard part is knowing what to use as the argument for Fields. Here, I use a constant exposed by the CDO 1.21 library. The Microsoft Developer Network (MSDN) Web site provides a list of these property tags, as they're called, at http://msdn.microsoft.com/library/en-us/cdo/html/_olemsg_mapi_property_tags.asp. A tool such as mdbvu32.exe on the Exchange CD-ROM or OutlookSpy from http://www.dimastr.com is invaluable in understanding which property contains what information.
However, not all fields are directly available through constants. In these cases, if you know the field's property tag, you can declare it as a constant to make your code more readable. The field for the SMTP address is a good example. Listing 1 includes a constant declaration for CdoPR_EMAIL, whose property tag value is &H39FE001E.
To get the sender's phone number, the code uses another CDO property tag—CdoPR_OFFICE_TELEPHONE_NUMBER—to get the main telephone number from an Exchange user's Global Address List (GAL) entry. If the sender has an SMTP address rather than an Exchange address, the code uses the Find method to locate the first matching contact with that address in the Contacts folder.
More CDO
If you want to explore CDO further, the CDOLive Web site (http://www.cdolive.com/cdo10.htm) provides more documentation about how to use the CDO Fields collection and different kinds of property tags. It even shows how to use CDO to access Outlook item properties.
About the Author
You May Also Like