问题
I need to read records containing name and email from a file or database and add them to an existing Oulook distribution list (from the private contacts, not from the GAL).
I just saw examples of reading from OL using LINQ to DASL which I have working for mail and appointments, but I can't figure out how to list the contents of a dist list:
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();
}
Once I can read the members I need to be able to add new ones which is even more trick. Any help would be appreciated.
回答1:
Turns out it is easy enough. I was simply missing the call to Resolve as I thought that was only if you were resolving against the GAL:
Outlook.Recipient rcp = app.Session.CreateRecipient("Smith, John<j.smith@test.com>");
rcp.Resolve();
list.AddMember(rcp);
list.Save();
And I can create an iterator that uses the distList.GetMember method:
// Wrap DistListItem.GetMembers() as an iterator
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);
}
}
}
来源:https://stackoverflow.com/questions/375148/add-new-records-to-private-outlook-distribution-list