How Does Outlook Resolve Email Addresses (1 Viewer)

GC2010

Registered User.
Local time
Today, 13:54
Joined
Jun 3, 2019
Messages
120
In working on some VBA code I have become curious as to how Outlook resolves an email address

For example, internal contacts within my company show as LastName, FirstName
But what is throwing me is that I have yet to see a consistency with external emails. These emails are not saved in my address book, however sometimes they resolve as ‘LastName, FirstName’ while other times they will resolve as the actual email address like ‘LastName.FirstName@company.com’

How or what determines how the Outlook.MailItem.To will display ?
 

GC2010

Registered User.
Local time
Today, 13:54
Joined
Jun 3, 2019
Messages
120
Hi. See if the answer to your question is somewhere in this other discussion. Good luck!

That definately explains it, thank you for that insight!

Any idea how I would access those elements from VBA? If I try the below, Intellisense does not provide Contact Name or Display Name as an option.

Code:
Dim olMail As Outlook.MailItem

olMail.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:54
Joined
Oct 29, 2018
Messages
21,454
That definately explains it, thank you for that insight!

Any idea how I would access those elements from VBA? If I try the below, Intellisense does not provide Contact Name or Display Name as an option.

Code:
Dim olMail As Outlook.MailItem

olMail.
Hi. Try to go lower in the object model and maybe investigate the Recipients collection of the MailItem object.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:54
Joined
Feb 28, 2001
Messages
27,146
Here is a link to the MailItem object model.

https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem

In the "Properties" section you will find your answers:

* SenderName - a.k.a. Display name
* Sender - a.k.a. From
* SenderEmailAddress - a third option that is often shown in <> as "<foo@bar.com>"
* SentOnBehalfOfName - a rarity, requiring the person using this option to be appropriately marked as having proxy rights for the named user.
* SendUsingAccount - a way for you, having a proxy, to send mail on behalf of someone else

And a whole bunch of other goodies. If you do as theDBguy says so that you can expose the properties of a mail message, there is a pot-load of good things to see. (You can of course decide for yourself which kind of pot we are discussing...)
 

GC2010

Registered User.
Local time
Today, 13:54
Joined
Jun 3, 2019
Messages
120
Here is a link to the MailItem object model.

https://docs.microsoft.com/en-us/office/vba/api/outlook.mailitem

In the "Properties" section you will find your answers:

* SenderName - a.k.a. Display name
* Sender - a.k.a. From
* SenderEmailAddress - a third option that is often shown in <> as "<foo@bar.com>"
* SentOnBehalfOfName - a rarity, requiring the person using this option to be appropriately marked as having proxy rights for the named user.
* SendUsingAccount - a way for you, having a proxy, to send mail on behalf of someone else

And a whole bunch of other goodies. If you do as theDBguy says so that you can expose the properties of a mail message, there is a pot-load of good things to see. (You can of course decide for yourself which kind of pot we are discussing...)

But I am the sender - I’m wanting to look at the To and CC properties. But pull the SMTP email address
 

GC2010

Registered User.
Local time
Today, 13:54
Joined
Jun 3, 2019
Messages
120
Hi. Try to go lower in the object model and maybe investigate the Recipients collection of the MailItem object.

If I look at the Recipients Object (Outlook) the only properties I see are:
Application
Class
Count
Parent
Session

(Looking at MSDN link)
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:54
Joined
Feb 28, 2001
Messages
27,146
How or what determines how the Outlook.MailItem.To will display ?

Oh, I get it. The answer is that there is a hidden protocol exchange between you as sender and some other server as receiver. Basic SMTP doesn't do this, but there are many extensions that do. When you are composing the message in Outlook, you identify the TO recipient and CC and BCC recipients, then have a message body and deal with attachments and such. For instance, when I was with the U.S.Navy, our mail went through an Exchange server. If we entered the e-mail, it would query the master address list to determine if that person was known to us. If so, the Display Name would be filled in, usually within seconds. If the person was NOT on the local Exchange server, there would be a longer delay to verify the destination address, and if the address could not be confirmed we would quickly learn that fact (i.e. "Unknown recipient: xxxx" messages in the TO, CC or BCC boxes on the SEND form.)

Therefore, the answer is that your mysterious resolution depends on whether the person is in the master address list for whatever service you are using, such as Exchange server. That is an OUTLOOK function, not an SMTP function, so the information is not usually available in the message for SMTP transmission. But it might well be there for OUTLOOK transmission.
 

GC2010

Registered User.
Local time
Today, 13:54
Joined
Jun 3, 2019
Messages
120
We are using Exchange. Is there a way to use VBA to query this Master Exvhange list you reference so You would know if you need to search by an email address or a name?
 

Gasman

Enthusiastic Amateur
Local time
Today, 21:54
Joined
Sep 21, 2011
Messages
14,238
We are using Exchange. Is there a way to use VBA to query this Master Exvhange list you reference so You would know if you need to search by an email address or a name?

I use this when I am sending emails, though it is only on a personal account, not an Exchange server.

Code:
    Dim objOutlookRecip As Outlook.Recipient
...
            For Each objOutlookRecip In .Recipients
                objOutlookRecip.Resolve
            Next

HTH
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:54
Joined
Feb 28, 2001
Messages
27,146
Is there a way to use VBA to query this Master Exvhange list you reference

Here is a reference to the AddressList object, which is a member of the AddressLists collection. As I understand it, there is a hierarchy of lists. The local one is the default.

https://docs.microsoft.com/en-us/office/vba/api/outlook.addresslist

You should be able to get some mileage out of searching for "Search Outlook AddressList" as a web search or in this forum. I have to be a bit vague because I never needed to do this at the VBA level. It is possible to open address books from the Outlook ribbon, and that is as far as I ever had to go. But I know they are there.
 

GC2010

Registered User.
Local time
Today, 13:54
Joined
Jun 3, 2019
Messages
120
Here is a reference to the AddressList object, which is a member of the AddressLists collection. As I understand it, there is a hierarchy of lists. The local one is the default.

https://docs.microsoft.com/en-us/office/vba/api/outlook.addresslist

You should be able to get some mileage out of searching for "Search Outlook AddressList" as a web search or in this forum. I have to be a bit vague because I never needed to do this at the VBA level. It is possible to open address books from the Outlook ribbon, and that is as far as I ever had to go. But I know they are there.

I have tried the below VBA code, but am getting an error of -----
Run-time error '91':

Object Variable or With block variable not set

Did I mis-understand what you said?

Code:
Dim olReply As Outlook.MailItem

For Each Recipient In olReply.recipients
    Debug.Print Recipient.Address
Next Recipient

I have also tried this syntax, but same error

Code:
Dim recips As Outlook.recipients
Dim recip As Outlook.Recipient
Dim mail As Outlook.MailItem

For Each recipient in mail.recipients
    Debug.Print recip.Name
    Debug.Print recip.Address
Next
 
Last edited:

GC2010

Registered User.
Local time
Today, 13:54
Joined
Jun 3, 2019
Messages
120
Okay, I jumped the gun with my above post. I have the below which works but my issue is how do I resolve an internal email to an actual email address?

Currently the internal emails show

/o=Company Name/ou=NA/cn=Recipients/can=Windows username
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:54
Joined
Feb 28, 2001
Messages
27,146
Those qualifiers relate to various qualifiers on users. "/o=" is the organization, "/ou=" is the organizational unit, and those other items are qualifiers to help characterization of each recipient.

However, I am now out of my depth because I never had to search at that level. I had taken the step of having each user of my DB send me an e-mail application, from which I was able to extract a digital signature certificate, e-mail address, and a few other things I wanted. So I never had to search for that at the VBA level. Perhaps you need to search the forum a bit more.
 

sxschech

Registered User.
Local time
Today, 13:54
Joined
Mar 2, 2010
Messages
792
Here is a function I use for resolving email addresses.

Code:
Public Function Outlook_Resolve(sendto As String)
'Resolve and return Outlook email address
'Code copied from Outlook_ReplyAll, so may have
'unneed items.  Additional code from
'https://docs.microsoft.com/en-us/office/vba/api/outlook.recipient.resolved
'20190102
    Dim OlApp As Object                 'Late
    Dim Inbox As Object
    Dim InboxItems As Object
    Dim Mailobject As Object
    Dim ResolveAddress As Object
    Dim flds As Variant
    Dim stResolve As String
    Dim i As Integer
    
    On Error Resume Next
    Set OlApp = GetObject(, "Outlook.Application")      'Outlook Running
    If Err.Number <> 0 Then
        Err.Clear
        Set OlApp = CreateObject("Outlook.Application") 'Outlook Not Running
    End If
    
    'Doc Control Inbox
    Set Inbox = OlApp.GetNamespace("Mapi").Folders("Doc Control").Folders("Inbox") 'Processed") 'Inbox")
    Set InboxItems = Inbox.Items
    Set Mailobject = OlApp.CreateItem(0)
    
    flds = Split(sendto, ";")
    With Mailobject
        For i = 0 To UBound(flds)
            Set ResolveAddress = .Recipients.Add(flds(i))
            ResolveAddress.resolve
            If ResolveAddress.resolved Then
                stResolve = stResolve & ResolveAddress & ";"
            Else
                stResolve = stResolve & flds(i) & ";"
            End If
        Next
    End With
    Outlook_Resolve = Left(stResolve, Len(stResolve) - 1)
Finished:
    Set OlApp = Nothing
    Set Inbox = Nothing
    Set InboxItems = Nothing
    Set Mailobject = Nothing
    Set ResolveAddress = Nothing
End Function

I run it from a sub by passing it a semicolon delimited list. Once complete, stto would have the "underlined" email list for outlook.

'stto contains emailnamelast, emailnamefirst; anotherlast, anotherfirst;...
'could also be emailfirst emaillast; anotherfirst another last;...
stto = Outlook_Resolve(stto)

Depending on your outlook, you may also be able to resolve just with a last name. I tested it with a couple of last names and in one case it came up with the full email, the other it didn't
 

Users who are viewing this thread

Top Bottom