'Trying to get SMTP Addresses from all AppointmentItem Recipients

I iterate all AppointmentItem.Recipients. For each and every Recipient I use the following utility method to retrieve the recipients "normal" SMTP email-adress:

for this i used the posting from stackoverflow: Getting email address from a Recipient object

    public static string GetSmtpAddress(Outlook.AddressEntry addressEntry)
    {
        String smtpAddress;

        if (addressEntry.Type == "EX")
        {
            if (addressEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry
                 || addressEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry)
            {
                Outlook.ExchangeUser user = addressEntry.GetExchangeUser();
                smtpAddress = user != null ? user.PrimarySmtpAddress : null;
            }
            else if (addressEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olOutlookContactAddressEntry)
            {
                //returns the actual contact but it has 3 email properties (Email1Address, Email2Address, Email3Address). 
                //How to identify which email has the user selected

                Outlook.ContactItem cont = addressEntry.GetContact();
                String OABID = addressEntry.ID;
                String typ = OABID.Substring(29 * 2, 2);
                if (typ=="00")
                {
                    smtpAddress = cont.Email1Address; <!-- Strange Emailadress
                }else if (typ=="01")
                {
                    smtpAddress = cont.Email1Address;
                } else
                {
                    smtpAddress = cont.Email2Address;
                }
            } else
            {
                smtpAddress = "";
            }
        }
        else if (addressEntry.Type == "SMTP")
        {
            smtpAddress = addressEntry.Address;
        }
        else
        {
            smtpAddress = "";
        }
        return smtpAddress;
    }

The Line which is marked with <- Strage Email retrieves a strange looking Email adress: It looks like /o=Exchange xxxxx something xxxx .. At the very end it also containts the smtp adress.

What I am searching for is a 100% robust utility function which retrieves the correct SMTP address no matter what adress typ the recipient is, Exchange User, AddressBook User, or whatever. Any help very much appreciated.

Best regards Hannes



Solution 1:[1]

If the GAL entry no longer exists, all bets are off. Before even touching Recipient.AddressEntry, check if the SMTP address is available in the recipient table - use Recipient.PropertyAccessor.GetProperty to read the PR_SMTP_ADDRESS property (DASL name "http://schemas.microsoft.com/mapi/proptag/0x39FE001F"). If it is not present, read the PR_ADDRTYPE property ("http://schemas.microsoft.com/mapi/proptag/0x3002001F") - this is equivalent to Type property on the AddressEntry object, which Recipient object unfortunately does not expose. If it is "SMTP", just use the Recipient.Address property. And only if it is not, use your function above that needs Recipient.AddressEntry.

Take a look at the appointment with OutlookSpy (I am its author) - click IMessage button, go to the GetRecipientTable tab to check if the PR_SMTP_ADDRESS property is available

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmitry Streblechenko