Add new records to private Outlook distribution list

橙三吉。 提交于 2019-12-24 00:36:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!