Search code examples
python-3.xemailoutlookexchange-server

What makes Outlook/Exchange able to resolve a recipient or not?


I am trying to test whether staff are still employed in the organization.

The best approach I have found so far has to try send them an email.

So, using Outlook, Exchange and Python, I put the email addresses into a new email and try "Resolve" the recipient.

The behavior seems erratic. In most cases, this will produce a result, or "None".

for recipient in msg.Recipients:
    try:
        exchangeSays = recipient.AddressEntry.GetExchangeUser() 

But now in my test case, I have a person [email protected] and whilst the person does exist, is real and present, the entry will not resolve.

I've also noticed that sometimes email addresses resolve to "John Smith" and other recipients will resolve to "[email protected]". I know it's resolved when the name is underlined in the email window.

What's going on here? Some questions:

  1. Is this the best way of checking whether an person is still current?
  2. Am i doing something wrong in my attempt to resolve the recipient?

Solution

  • Firstly, there is no need to create a message and send it - use Application.Session.CreateRecipient / Recipient.Resolve. If Recipient.Resolve returns true, check Recipient.AddressEntry.Type, If it is "EX", the name was resolved to a GAL user. You can however run into problems if you have a contact with the same name in your Contacts folder, and OOM does not allow to resolve against a particular container (e.g. GAL).

    Note that Outlook Object Model cannot handle ambiguous names (e.g. "John Smith" will also match "John Smither") - it will simply return an error. If using Redemption is an option (I am its author), it exposes RDOSession.AddressBook.ResolveNameEx, which returns a list of matches.

    On the Extended MAPI level (PR_ANR restriction) you can also resolve against a particular container, but OOM does not expose that functionality. In Redemption, you can use RDOSession.AddressBook.GAL.ResolveName/ResolveNameEx. Note however that in this case GAL provider won't resolve an SMTP address of a GAL user (go figure).

    Outlook (UI only) also exposes search containers (PR_SEARCH in MAPI, you can see the one exposed by GAL if you click Ctrl + Shift + B to open the address book, then click Tools | Find). You can specify one or more fields to match (e.g. the whole display name or first and last names separately), but OOM, again, does not expose it. In Redemption, you can use RDOSession.Addressbook.GAL.Search:

    set Session = CreateObject("Redemption.RDOSession")
    Session.MAPIOBJECT = Application.Session.MAPIOBJECT
    set AddrList = Session.Addressbook.GAL
    set Search = AddrList.Search
    Search.FirstName = "John"
    Search.LastName = "Smith"
    set AddressEntries = Search.GetResults
    for each AddressEntry in AddressEntries
        MsgBox AddressEntry.Name
    next