如何通过程序从Exchange Outlook联系人中获取Internet电子邮件地址?

5
我正在尝试从连接到Exchange的Outlook中读取Internet格式的地址。我从Outlook联系人中读取所有联系人,即不是从全局地址簿(GAB)中读取的,问题是对于存储在Exchange GAB的联系人,我只能够读取X.500格式的地址,在这种情况下没有用处。对于所有手动添加的不在Exchange服务器域中的联系人,导出的Internet地址与预期相符。
基本上,我使用以下代码段枚举联系人:
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();
}

有没有办法提取互联网地址而不是X.500?
1个回答

6
你需要将 ContactItem 转换为 AddressEntry,一次转换一个电子邮件地址。
为此,你需要通过 Recipient 对象模型访问 AddressEntry。检索实际收件人 EntryID 的唯一方法是通过利用 ContactItem PropertyAccessor
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;
}

这个问题已经有一段时间没有回答了。您能否指导我如何修改上面的代码以获取“Email2EntryID”和“Email3EntryID”?我已经在互联网上寻找它们的GUID(看起来这可能是唯一的区别),但还没有找到。 - dotNET
没事了。我发完问题后,找到了一个微软页面,上面有两个ID。对于任何感兴趣的人,只需将最后一部分(80850102)更改为Email2的80950102和Email3的80A50102即可。 - dotNET

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接