2012-07-04 86 views
3

我试图从Exchange连接的Outlook中读出Internet格式的地址。我从Outlook联系人中读取所有联系人,即不从全局地址簿(GAB)中读取所有联系人,问题在于对于存储在Exchange GAB联系人中的所有用户,我只设法读出X.500格式化在这种情况下无用的地址。对于不在Exchange服务器域中的所有手动添加联系人,Internet地址按预期导出。以编程方式从Exchange Outlook联系人获取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?

回答

4

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

为此,您需要通过Recipient对象模型访问AddressEntry。检索实际收件人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; 
} 
+0

已经有相当一段时间了,因为这被回答。你可以plz指导我如何修改上面的代码来获得'Email2EntryID'和'Email3EntryID'?我一直在寻找所有的互联网上的GUID(看起来这将是唯一的区别),但还没有找到它们。 – dotNET

+0

没关系。当我发布我的问题时,我找到了一个有两个ID的微软页面。对于任何对此感兴趣的人,只需将Email2的80950102和Email3的80A50102更改为最后一部分(80850102)即可。 – dotNET

相关问题