27117

Add new records to private Outlook distribution list

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.

Answer1:

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); } } }

Recommend

  • Access MS Outlook from Windows service
  • Open email in specific folder
  • Google MAP API V3 cannot clear the previous mutiple routes history [closed]
  • Problems with “WithEvents” in firtstrigger
  • How to find positive matches of regex in the directory and return needed exit code?
  • appendChild kmz works only in second time when I run a tour (tour is into the same kmz)
  • Custom outlook plug in not downloading as expected
  • Is it possible to filter Task list by other properties than createdBy?
  • Determine height of screen in Java
  • Set all mail items in an outlook conversation to read using VBA
  • trying to split the file download buffer to into separate threads
  • Error on a disk space usage script
  • PowerShell Flatten Directory Structure
  • 64 bit integer comparison in HAProxy acl rule
  • How to get free/busy information of not shared appointment attendee in .net?
  • Search Outlook Contact using COM?
  • Parse.com to communicate with WSDL
  • DynamoDb “Query key condition not supported”
  • Location-aware geocoding on Android (just like Google Maps)
  • Listing all emails on a shared mailbox with attachments name
  • Set Outlook's 'show this address list first' option
  • Extending the User Interface in Outlook 2010 to add to the Right Click menu
  • Get-AdComputer -filter parameter not accepting Get-Date output
  • Obtain access token for both Microsoft Graph and individual service API endpoints (Outlook REST APIs
  • Escaping single quotes in JDBC with MySql
  • SQLite connection strategies
  • How can I extract results of aggregate queries in slick?
  • Change Inet root folder for iis 7
  • Paperclip, set path outside of rails root folder
  • Debugging ASP.NET on a built-in web server suddenly stops
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • How to delete a row from a dynamic generate table using jquery?
  • InvalidAuthenticityToken between subdomains when logging in with Rails app
  • Proper folder structure for lots of source files
  • Load html files in TinyMce
  • using HTMLImports.whenReady not working in chrome
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Append folder name and increment by 1 using batch script
  • How to get NHibernate ISession to cache entity not retrieved by primary key