以编程方式从Exchange Outlook联系人获取Internet电子邮件地址?

时间:2012-07-04 09:17:40

标签: outlook

我正在尝试从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 个答案:

答案 0 :(得分:5)

您需要从ContactItem转换为AddressEntry - 一次只能转换一个电子邮件地址。

为此,您需要通过AddressEntry对象模型访问Recipient。检索实际收件人EntryID的唯一方法是leveraging the PropertyAccessor of the 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;
}