Поделиться через


HOWTO: OOM: Getting Primary SMTP Address from X500

Getting primary address from X500 address was always a pain when using OOM. Thank to these guys who realized this pain and built in this functionality in Outlook 2007 Object Model. I have figured out a way to get this value from within OOM for Outlook 2003 & below as well.

How it works

============

Uses native OOM support for 2007 & above else use the following workaround

1) It will create a new contact item

2) Set it's email address to the value passed by you, it could be X500 or SMTP address

3) We will assign a random key to this contact item and save it in its Fullname to search it later

4) Next we will save it to local contacts folder

5) Outlook will try to resolve the email address & make AD call if required else take the Primary SMTP address from its cache and append it to Display name

6) The display name will be something like this " ( email.address@server.com )"

7) Now we need to parse the Display name and delete the contact from contacts folder

8) Once the contact is deleted it will go to Deleted Items folder, after searching the contact using the unique random key generated in step 3

9) We then need to delete it from Deleted Items folder as well, to clean all the traces

DISCLAIMER: I have tested it only on 2003 & 2007, although logically it should work on all versions of Outlook.

 Function GetSMTPAddress(ByVal strAddress As String) 
Dim oCon As ContactItem 
Dim strKey As String 
Dim oRec As Recipient 
Dim strRet As String 

    'IF OUTLOOK VERSION IS >= 2007 THEN USES NATIVE OOM PROPERTIES AND METHODS 

    If CInt(Left(Application.Version, 2)) >= 12 Then 
        Set oRec = Session.CreateRecipient(strAddress) 
        If oRec.Resolve Then 
            strRet = oRec.AddressEntry.GetExchangeUser.PrimarySmtpAddress 
        End If 
    End If 

    If Not strRet = "" Then GoTo ReturnValue 

    'IF OUTLOOK VERSION IS < 2007 THEN USES LITTLE HACK 

    'How it works 
    '============ 
    '1) It will create a new contact item 
    '2) Set it's email address to the value passed by you, it could be X500,X400 or any type of email address stored in the AD 
    '3) We will assign a random key to this contact item and save it in its Fullname to search it later 
    '4) Next we will save it to local contacts folder 
    '5) Outlook will try to resolve the email address & make AD call if required else take the Primary SMTP address from its cache and append it to Display name 
    '6) The display name will be something like this " ( email.address@server.com )" 
    '7) Now we need to parse the Display name and delete the contact from contacts folder 
    '8) Once the contact is deleted it will go to Deleted Items folder, after searching the contact using the unique random key generated in step 3 
    '9) We then need to delete it from Deleted Items folder as well, to clean all the traces 

    Set oCon = Application.CreateItem(olContactItem) 
    oCon.Email1Address = strAddress 
    strKey = "_" & Replace(Rnd * 100000 & Format(Now, "DDMMYYYYHmmss"), ".", "") 
    oCon.FullName = strKey 
    oCon.Save 
    strRet = Trim(Replace(Replace(Replace(oCon.Email1DisplayName, "(", ""), ")", ""), strKey, "")) 
    oCon.Delete 
    Set oCon = Nothing 

    Set oCon = Session.GetDefaultFolder(olFolderDeletedItems).Items.Find("[Subject]=" & strKey) 
    If Not oCon Is Nothing Then oCon.Delete 

    ReturnValue: 
    GetSMTPAddress = strRet 
End Function

References:

If you prefer then, you can do this with Exchange Web Services as well

https://blogs.msdn.com/vikas/archive/2007/09/05/howto-ews-get-smtp-address-from-x500-address.aspx

Keywords: GetExchangeUser , Exchange Web Services, Outlook Object Model, X500 to SMTP

Comments

  • Anonymous
    January 15, 2009
    Thank you for posting this, I found it extremely helpful in a project.

  • Anonymous
    February 04, 2010
    This post was incredibly helpful. Many thanks.

  • Anonymous
    February 25, 2010
    Fantastic, thanks very much. It's just a shame that this doesn't appear higher up in the Google searches I've been making - otherwise I would have found it a lot quicker!

  • Anonymous
    May 05, 2011
    The solution for outlook < 2007 does not work, if the Adress is in X500 Format

  • Anonymous
    June 09, 2011
    This is fantastic thank-you. Did anyone else get this running with Outlook 2007.  I am finding the .resolve does not resolve the address when it is passed the X400 or X500 address ?