OL: How to Programmatically Retrieve Fully Qualified E-mail Addresses (324530)



The information in this article applies to:

  • Microsoft Outlook 2002
  • Microsoft Outlook 2000
  • Microsoft Outlook 98
  • Microsoft Outlook 97

This article was previously published under Q324530

SUMMARY

This article describes how to programmatically return a fully qualified sender's address from an Outlook e-mail message.

MORE INFORMATION

The MailItem object in the Outlook object model contains a SenderName property, but this property may not contain a fully qualified e-mail address that uniquely identifies the sender of the mail message. The SenderName property contains the string value of the sender's display name. If the sender is an Exchange user, the display name generally contains that sender's first and last name. If the user is not an Exchange user, there is no guarantee what the string value of the SenderName property will be. The SenderName property may contain the first and last name of the sender, the sender's SMTP address, or a combination of both, such as the following:

FirstName (sender_e-mail@example.com)

The functionality of this property is by design in all versions of Outlook from Outlook 97 through Outlook 2002. The inability to directly return a fully qualified e-mail address is a limitation of the Outlook object model in these versions. Outlook 2003 includes a new SenderEmailAddress property that will return the fully-qualified address of an Internet recipient. This property will return the X500 address of an Exchange recipient.

For Outlook 2002 and earlier versions of Outlook, you can use one of the following approaches to retrieve fully qualified e-mail addresses from e-mail: messages:

Create a reply by using the object model

You can create a reply e-mail message and then retrieve the Address property of the recipient in the Recipients collection of the new reply message. After you have the address, you can discard the reply. The following Microsoft Visual Basic for Applications (VBA) code illustrates how to retrieve the fully qualified e-mail address of an open e-mail message:
Sub GetEmailAddressReply()
   Dim objItem As MailItem
   Dim objReply As MailItem
   Dim objRecips As Outlook.Recipients
   Dim objRecip As Outlook.Recipient
    
   Set objItem = Application.ActiveInspector.CurrentItem
   Set objReply = objItem.Reply

   Set objRecips = objReply.Recipients
   For Each objRecip In objRecips
      Debug.Print objRecip.Address
   Next
    
   Set objItem = Nothing
   Set objReply = Nothing
   Set objRecip = Nothing
End Sub
				

Use Collaboration Data Objects (CDO) 1.2x Library

You can retrieve a sender's address by using the Sender property in the CDO 1.2x object library. The following CDO automation code retrieves the Address property from the sender:
Sub GetAddressFromCDO()
   Dim objSession As MAPI.Session
   Dim objInboxMsgs As MAPI.Messages
   Dim objMsg As MAPI.Message
   Dim strAddress As String

   Const g_PR_SMTP_ADDRESS_W = &H39FE001F 
   
   Set objSession = CreateObject("MAPI.Session")
   objSession.Logon NewSession:=False
   Set objInboxMsgs = objSession.Inbox.Messages
   
   For Each objMsg In objInboxMsgs
      Debug.Print objMsg.Subject
      strAddress = objMsg.Sender.Address
      ' If the e-mail sender is an Exchange user, the e-mail address
      ' will return as an X400 address and not an SMTP address.
      ' If the Address property does not return a fully qualified
      ' address (for example, containing an @), try the &H39FE001F MAPI
      ' IMPORTANT: This property is not documented on MSDN.
      If Not InStr(strAddress, "@") Then
         On Error Resume Next
         strAddress = objMsg.Sender.Fields(g_PR_SMTP_ADDRESS_W).value
      End If
      Debug.Print strAddress
      Debug.Print
   Next
      
   objSession.Logoff
   Set objMsg = Nothing
   Set objInboxMsgs = Nothing
   Set objSession = Nothing
End Sub
				

Retrieve an SMTP address from alternate Exchange addresses

An Exchange property contains all the available addresses for a recipient in the organization. To retrieve the fully qualified address of a recipient in an Exchange organization, you can retrieve this property by using CDO 1.2x, and then parse the contents to find the SMTP address.

The following VBA code retrieves the SMTP address of an Exchange user by accessing the H800F101E MAPI property. Make sure that you reference the Microsoft CDO 1.2x object library.
Sub RetrieveAlternateAddress()
   Dim objSession As MAPI.Session
   Dim objCDOMsg As MAPI.Message
   Dim objAddEntry As MAPI.AddressEntry
   Dim objField As MAPI.Field
   Dim objMsg As MailItem
   Dim strEntryID As String
   Dim strStoreID As String
   Dim strAddress As String
   Dim fld
   
   Set objSession = CreateObject("MAPI.Session")
     
   On Error Resume Next
   objSession.Logon NewSession:=False
   Set objMsg = Application.ActiveInspector.CurrentItem
   strEntryID = objMsg.EntryID
   strStoreID = objMsg.Parent.StoreID
   
   Set objCDOMsg = objSession.getMessage(strEntryID, strStoreID)
   Set objAddEntry = objCDOMsg.Sender
       
   If objAddEntry.Type = "EX" Then
      Set objField = objAddEntry.Fields(&H800F101E)
         For Each fld In objField.Value
            If Left(fld, 5) = "SMTP:" Then
               strAddress = Mid(fld, 6)
               Debug.Print strAddress
               Exit For
            End If
        Next
   Else
       Debug.Print objAddEntry.Address
   End If
      
   objSession.Logoff
   Set objSession = Nothing
   Set objAddEntry = Nothing
   Set objCDOMsg = Nothing
   Set objMsg = Nothing
   Set objField = Nothing
End Sub
				
For more information about how to access alternate e-mail addresses in an Exchange organization, click the following article numbers to view the articles in the Microsoft Knowledge Base:

196507 HOWTO: Retrieve alternate e-mail addresses using CDO

248357 HOWTO: Retrieve alternate e-mail addresses using extended MAPI


Modification Type:MajorLast Reviewed:3/23/2006
Keywords:kbhowto KB324530