How to get an email address on the Internet from Exchange Outlook Contact programmatically?

I am trying to read an address formatted on the Internet from a connected Outlook Exchange. I read all contacts from Outlook contacts, i.e. Not from the global address book (GAB), and the problem is that for all users who are stored in Contacts from Exchange GAB Ive, only the formatted X.500 address was read, which in this case is not useful. For all manually added contacts that are not in the Exchange server domain, the Internet address is exported as expected.

Ive basically used the following code snippet to list contacts:

static void Main(string[] args) { var outlookApplication = new Application(); NameSpace mapiNamespace = outlookApplication.GetNamespace("MAPI"); MAPIFolder contacts = mapiNamespace.GetDefaultFolder(OlDefaultFolders.olFolderContacts); for (int i = 1; i < contacts.Items.Count + 1; i++) { try { ContactItem contact = (ContactItem)contacts.Items[i]; Console.WriteLine(contact.FullName); Console.WriteLine(contact.Email1Address); Console.WriteLine(contact.Email2Address); Console.WriteLine(contact.Email3Address); Console.WriteLine(); } catch (System.Exception e) { } } Console.Read(); } 

Is there a way to extract the Internet address instead of X.500?

+3
source share
1 answer

You need to convert from ContactItem to AddressEntry - one email address at a time.

To do this, you need to access the AddressEntry using the Recipient object model. The only way to get the actual recipient of the EntryID is using the PropertyAccessor ContactItem .

 const string Email1EntryIdPropertyAccessor = "http://schemas.microsoft.com/mapi/id/{00062004-0000-0000-C000-000000000046}/80850102"; string address = string.Empty; Outlook.Folder folder = this.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) as Outlook.Folder; foreach (var contact in folder.Items.Cast<Outlook.ContactItem>().Where(c=>!string.IsNullOrEmpty(c.Email1EntryID))) { Outlook.PropertyAccessor propertyAccessor = contact.PropertyAccessor; object rawPropertyValue = propertyAccessor.GetProperty(Email1EntryIdPropertyAccessor); string recipientEntryID = propertyAccessor.BinaryToString(rawPropertyValue); Outlook.Recipient recipient = this.Application.Session.GetRecipientFromID(recipientEntryID); if (recipient != null && recipient.Resolve() && recipient.AddressEntry != null) address = recipient.AddressEntry.GetExchangeUser().PrimarySmtpAddress; } 
+4
source

Source: https://habr.com/ru/post/1273034/


All Articles