2008-12-17 28 views
2

我需要從文件或數據庫中讀取包含姓名和電子郵件的記錄,並將其添加到現有的Oulook通訊組列表中(來自私人通訊錄,而不是來自GAL)。向私人Outlook通訊組列表添加新記錄

我剛纔看到的,從使用LINQ to我已經工作了郵件和約會DASL OL讀書的例子,但我無法弄清楚如何列出一個清單DIST的內容:

private static void GetContacts() 
    { 
     Outlook.Application app = new Outlook.Application(); 
     Outlook.Folder folder = (Outlook.Folder)app.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); 
var distLists = from item in folder.Items.AsQueryable<MyDistList>() 
       where item.DLName == "My Dist List" 
       select item.Item; 

     var builder = new StringBuilder(); 

     foreach (var list in distLists) 
     { 
      builder.AppendLine(list.DLName); 
      foreach (var item in list.Members) 
      { 
      // can't figure out how to iterate through the members here 
      // compiler says Object doesn't have GeNumerator... 
      } 
     } 

     Console.WriteLine(builder.ToString()); 
     Console.ReadLine(); 
    } 

一旦我可以閱讀我需要的成員,以便能夠添加新的成員,這更加詭異。任何幫助,將不勝感激。

回答

2

原來這很容易。我只是錯過了呼叫解決,因爲我認爲這是唯一的,如果你解決了對GAL:

Outlook.Recipient rcp = app.Session.CreateRecipient("Smith, John<[email protected]>"); 
rcp.Resolve(); 
list.AddMember(rcp); 
list.Save(); 

我可以創建一個使用distList.GetMember方法的迭代器:

//裹DistListItem.GetMembers()作爲迭代器

public static class DistListItemExtensions 
{ 
    public static IEnumerable<Outlook.Recipient> Recipients(this Outlook.DistListItem distributionList) 
    { 
     for (int i = 1; i <= distributionList.MemberCount; i++) 
     { 
      yield return distributionList.GetMember(i); 
     } 
    } 
} 
相關問題