Get SMTP address not working when you add an exchange user to outlook contacts

Add-in Express™ Support Service
That's what is more important than anything else

Get SMTP address not working when you add an exchange user to outlook contacts
 
Bert Sinnema




Posts: 55
Joined: 2015-04-16
Hi,

Our customers have been complaining that a we have a feature that is not a 100% bulletproof. It's a feature that checks if there are any external recipients. If so I show a dialog with the list of all the recipients email addresses that the user has to confirm before send.

It seems that sometimes a null address is show. After revisiting the code I devided to do a small Lab based on the post:
https://www.add-in-express.com/creating-addins-blog/2009/05/08/outlook-exchange-email-address-smtp/

The thing is I found out this isn't always working and I found a way to reproduce this. When you add an Exchange user to your local Outlook contacts, (OlAddressEntryUserType.olOutlookContactAddressEntry) The Recipients AddressEntry type is EX and the Address is still an Exchange type address string.

During conversion in GetSMTPAddressViaOutlookObjectModel the user variable yields null. Can you guys check it out?

Thanks
Posted 04 Jun, 2018 03:03:00 Top
Bert Sinnema




Posts: 55
Joined: 2015-04-16
I think I got it,

I created an overload for GetSMTPAddressViaOutlookObjectModel that takes an Exchange address, e.g.: /o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF53S6DLT)/cn=Recipients/cn=c8798e034xxxxxxac8a3a69904bc2cf24-username

Note: This is specifically when we are trying to extract an SMTP address out of an Exchange address that is stored in an Outlook Contact card


        /// <summary>
        /// Finds the SMTP addres based on an exchange string inside an outlook contact
        /// </summary>
        /// <param name="">Has to be an exchange address string</param>
        /// <returns></returns>
        private string GetSMTPAddressViaOutlookObjectModel(string exchangeAddress)
        {
            // Start with an empty string
            var address = string.Empty;

            // Create a recipients with the exchange address
            // E.g. .....
            Outlook.Recipient recipient = Module.OutlookApp.Session.CreateRecipient(exchangeAddress);

            // Resolve the recipient
            recipient.Resolve();

            // When the recipient is resolved
            if (recipient.Resolved)
            {
                // Get the user behind the resolved address
                Outlook.ExchangeUser user = recipient.AddressEntry.GetExchangeUser();

                // If the user is found
                if (user != null)
                {
                    // set the address to the primary SMTP address of the exchange user
                    address = user.PrimarySmtpAddress;
                    // Release the user object
                    Marshal.ReleaseComObject(user);
                }

                // Release the recipient object
                Marshal.ReleaseComObject(recipient);
            }

            // return the address
            return address;
        }


This seems to work and seems to be missing from the post:
https://www.add-in-express.com/creating-addins-blog/2009/05/08/outlook-exchange-email-address-smtp/

Please let me know what you think.

Cheers!
Posted 04 Jun, 2018 04:58:25 Top
Andrei Smolin


Add-in Express team


Posts: 15919
Joined: 2006-05-11
Hello Bert,

Thank you for providing your solution for this scenario! The code is okay. There's one issue, though: recipient.AddressEntry in your code creates a COM object and you need to release it: save this object to a variable and release the variable after use.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 04 Jun, 2018 06:50:46 Top
Bert Sinnema




Posts: 55
Joined: 2015-04-16
Thank you Andrei!

Yes you are right. Thanks!
Posted 04 Jun, 2018 07:00:55 Top
Andrei Smolin


Add-in Express team


Posts: 15919
Joined: 2006-05-11
You are welcome!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 04 Jun, 2018 07:10:16 Top