我需要从文件或数据库中读取包含姓名和电子邮件的记录,并将它们添加到现有的Oulook分发列表中(来自私人联系人,而不是来自GAL)。
我刚看到使用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();
}
一旦我能够阅读成员,我需要能够添加更新的成员,这是更多的技巧。任何帮助将不胜感激。
答案 0 :(得分:2)
事实证明这很容易。我只是错过了对Resolve的调用,因为我认为这只是你在解决GAL时的问题:
Outlook.Recipient rcp = app.Session.CreateRecipient("Smith, John<j.smith@test.com>");
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);
}
}
}