Josh
Josh

Reputation: 1058

How to get PrimarySmtpAddress for Exchance users and distribution lists

I am trying the email address of the from, to, and cc fields. Sometimes these are AD emails, SMTP, or Distribution emails.

I found someone who had a similiar problem here but they did not have anything about distribution lists.

I modified the code slightly to try to get this value.

if (type.ToLower() == "ex")
            {
                recip = Globals.ThisAddIn.Application.GetNamespace("MAPI").CreateRecipient(address);
                if (recip.DisplayType == OlDisplayType.olDistList)
                {
                    sAddress = recip.AddressEntry.GetExchangeDistributionList().PrimarySmtpAddress;
                }
                else
                {
                    sAddress = recip.AddressEntry.GetExchangeUser().PrimarySmtpAddress;
                }
            }
            else
            {
                sAddress = address.Replace("'", "");
            }

The problem is that recip.DisplayType is null unless there is a small delay after getting a recipient and calling DisplayType on that object.

Is there a better way to do this?

I changed the code to the following but I have concerns that this will not work for all the DisplayTypes and I'm not even sure what most of the types are (The options are shown here http://msdn.microsoft.com/en-us/library/microsoft.office.interop.outlook.oldisplaytype%28v=office.14%29.aspx)

private static string GetSmtpAddress(AddressEntry addressEntry)
        {
            string address;
            if (addressEntry.Type == "ex")
            {
                if (addressEntry.DisplayType == OlDisplayType.olDistList)
                {
                    address = addressEntry.GetExchangeDistributionList().PrimarySmtpAddress;
                }
                else
                {
                    address = addressEntry.GetExchangeUser().PrimarySmtpAddress;
                }
            }
            else
            {
                address = addressEntry.Address;
            }
            return address;
        }

Upvotes: 0

Views: 2312

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66341

You need to resolve the recipient first - after calling CreateRecipient, call Recipient.Resolve.

Upvotes: 1

Related Questions