GSandro_Strongs
GSandro_Strongs

Reputation: 841

How to get contacts from Global Address List using c#

I'm trying to get the lists of contacts from Outlook Global Address list (From my company emails, I think from Exchange) but I had an exception. I did this code:

Outlook.Application OutlookApplication = new Outlook.Application();
Outlook.AddressLists addrLists = OutlookApplication.Session.AddressLists;
Outlook.AddressList gal = addrLists["Global Address List"];
foreach (Outlook.AddressEntry myentry in gal.AddressEntries)
{
    Outlook.ExchangeDistributionList listex = myentry.GetExchangeDistributionList();
    Outlook.AddressEntries entradas = listex.GetExchangeDistributionListMembers();
    for (int i = 0; i < entradas.Count; i++)
    {
        Outlook.AddressEntry exchDLMember = entradas[i];
        MessageBox.Show(exchDLMember.Name, "Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
}

I got this exception, Object reference not established as an instance of an object, in the line:

Outlook.AddressEntries entradas = listex.GetExchangeDistributionListMembers();

Thanks in advance.

Upvotes: 0

Views: 817

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66235

The error means that GetExchangeDistributionList() method returns null, and you never check for that. This behavior is expected as it only returns a valid object if you are dealing with a GAL distribution list.

For non-DL GAL entries, you need to use GetExchangeUser method. You must also always check that you get back a valid object rather than null.

Upvotes: 1

Related Questions