2

I am currently trying to pull the distribution list(s) emails from my Global Address List. I have it partially functioning right now, by partially I mean that I am currently able to successfully pull the name of the distribution list, but not the email. This is what I have so far:

    public class DistributionListDetails
    {
        public string DistributionListId { get; set; }
        public string DistributionListEmail { get; set; }
    }

    public List<DistributionListDetails> DistributionListInformtion { get; set; }


    [WebMethod]
    public static List<DistributionListDetails> GetDistributionLists()
    {
        List<DistributionListDetails> distributionLists = new List<DistributionListDetails>();

        //create Outlook application. 
        Outlook.Application oApp = new Outlook.Application();

        //get Mapi NameSpace and Logon. 
        Outlook.NameSpace oNS = oApp.GetNamespace("mapi");

        //get Global Address List. 
        Outlook.AddressLists oDLs = oNS.AddressLists;
        Outlook.AddressList oGal = oDLs["Global Address List"];

        //get a specific distribution list. 
        string sDL = "TestDL";
        Outlook.AddressEntries oEntries = oGal.AddressEntries;
        Outlook.AddressEntry oDL = oEntries[sDL];

        if (oDL.Manager != null)
        distributionLists.Add(new DistributionListDetails
            {
                DistributionListId = oDL.Name,
                DistributionListEmail = oDL.Manager.ToString()
            });

        //get all of the members of the distribution list. 
        oEntries = oDL.Members;
        Outlook.AddressEntry oEntry = default(Outlook.AddressEntry);

        //adding distribution lists to list
        distributionLists.AddRange(oGal.AddressEntries.Cast<Outlook.AddressEntry>().Select(
            x => new DistributionListDetails
                 {
                     DistributionListId = x.Name,
                     DistributionListEmail = x.Name
                 }).Take(400));

        //log off. 
        oNS.Logoff();

        //clean up. 
        oApp = null;
        oNS = null;
        oDLs = null;
        oGal = null;
        oEntries = null;
        oEntry = null; 

        return distributionLists;
}

I am basically using the Interop Outlook service (which I am not thrilled about), to open up Outlook and retrieve the name of the distribution lists from the Global Address List. What I thought I would be able to do to get the DL email address would be something like this in my LINQ query:

DistributionListID = x.Email

Or something of that nature, but it does not give me any type of option like that. My end product is I would like to email the distribution lists from a web application (hence why I need the email address). I thought I would be able to just strictly use the name since I am using Interop and it would be smart enough to email it, but I was wrong.

Currently I am throwing it this :

My Distribution List

But it is expecting this (this does work, I have tested it in debugging):

My Distribution List <MyDistributionListEmail@mycompany.com>

So after all of that, does anyone have any advice on how I can pull the email addresses of the DL's?

Code
  • 1,969
  • 3
  • 18
  • 33
  • when you run this code does it return a List ` distributionLists.AddRange(oGal.AddressEntries.Cast().Select( x => new DistributionListDetails { DistributionListId = x.Name, DistributionListEmail = x.Name }).Take(400));` ..? if not what if you add `.ToList()` at the end.. – MethodMan Dec 15 '15 at 21:48
  • Yeah, it is returning exactly what it should at the moment. I just want it to return the email address instead of name... – Code Dec 15 '15 at 21:50

1 Answers1

2

Use AddressEntry.Address. If you want SMTP adders, use AddressEntry.GetExchangeUser().PrimarySmtpAddress.

Do not use LINQ with OOM objects, use a good old "for" loop.

Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78