Get SMTP email from Exchange Account in OutLook VSTO

Yesterday, I needed to obtain the primary SMTP email account from a MailItem in a Outlook VSTO AddIn…

 

So as usual I jump into Google and start searching (learning) how to do it, and all that I found was scary and non supported solutions like using third party dll’s like Redemption or code using ExtendedMAPI…

 

But I found another way that worked perfectly well for me and it is much more simple.

 

Here’s my solution:

 

  public static List<string> GetDestinations(Outlook.MailItem mail)

        {

            try

            {

                List<string> mailAddressList = new List<string>();

               

                foreach (Recipient recipient in mail.Recipients)

                {

                    if (recipient.AddressEntry.Type == "EX")

                        mailAddressList.Add(recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress);

                    else

                        mailAddressList.Add(recipient.Address); 

                }

 

                return mailAddressList;

            }

            catch (System.Exception ex)

            {

                Logger.Error(ex, "GetDestinations");

                throw;

            }

        }

 

This method returns all the destinations of a MailItem, and check’s if it is an exchange account an uses the Outlook API method “GetExchangeUser()” to obtain the PrimarySmtpAddress of the destination.

 

Hope it helps, for me it worked perfectly so far.

 

Advertisements