Home : Retrieving the Global Address List (GAL) from an Exchange Server
Q82038 - HOWTO: Retrieving the Global Address List (GAL) from an Exchange Server

Retrieving the Global Address List (GAL) from an Exchange Server

The following example demonstrates how to retrieve the Global Address List (GAL) from an Exchange server using CDO. Before you try this example, make sure that you have installed J-Integra and configured the Exchange server.

For more information about the programming API of CDO, please refer to Microsoft CDO Object Model.

The related article to the right explains how to retrieving additional fields from a contact record.

import com.intrinsyc.cdo.*;

public class GALExample {
    public static void main(String[] args) {
        //TODO: Change the following parameters based on your setup and configuration
        // the logon parameters of the service account you used to configure CDO
        String domain           = "mydomain";
        String username         = "jsmith";
        String password         = "password";
        // the DNS name or IP address of the machine where you installed CDO
        String CDOmachine       = "0.0.0.0";
        // the DNS name or IP address of the Exchange Server
        String exchangeServer   = "0.0.0.0";
        // the mailbox you wish to access (NOTE: this is the same as the user's login name
        // e.g., if the username is jsmith, mailbox would also be jsmith )
        String mailbox          = "jsmith";
        try {
            // authenticate to Windows
            com.linar.jintegra.AuthInfo.setDefault(domain, username, password);
            // create a Session in the CDOmachine
            Session session = new Session(CDOmachine);
            // logon to the Exchange Server
            session.logon(null, null, new Boolean(false), new Boolean(true),
                            new Boolean(false), new Boolean(false),
                            exchangeServer + "\n" + mailbox);
            // get the Global Address List
            AddressList addressList = new AddressListProxy(session.getAddressList(
                            new Integer(CdoAddressListTypes.CdoAddressListGAL)));
            // get the collection of addresses
            AddressEntries addressEntries = new AddressEntriesProxy(
                            addressList.getAddressEntries());
            // loop through the addresses and print them out
            int count = ((Integer) addressEntries.getCount()).intValue();
            if (count < 1) {
                System.out.print("No GAL entries found.");
            } else {
                System.out.println("Found GAL entries...");
                for(int i = 1; i <= count; i++) {
                    AddressEntry addressEntry = new AddressEntryProxy(
                            addressEntries.getItem(new Integer(i)));
                    System.out.println("NAME    : " + addressEntry.getName());
                    System.out.println("ADDRESS : " + getAddress(addressEntry));
                }
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            com.linar.jintegra.Cleaner.releaseAll();
        }
    }
    // get the email address of an AddressEntry
    public static String getAddress(AddressEntry ae) throws Exception{
        final int g_PR_SMTP_ADDRESS_W = 972947487; // field ID for SMTP address
        String address = ae.getAddress().toString();
        if(address.indexOf("@") < 1 ){ // address is not in SMTP format
            Fields fields = new FieldsProxy(ae.getFields());
            Field field = new FieldProxy(fields.getItem(new Integer(g_PR_SMTP_ADDRESS_W), null));
            address = field.getValue().toString();
        }
        return address;
    }
}

If you wish to retrieve all contacts in distribution lists as well, please refer to the example below.

import com.intrinsyc.cdo.*;
public class GALExample {
    static int level = 0;
    public static void main(String[] args) {
        //TODO: Change the following parameters based on your setup and configuration
        // the logon parameters of the service account you used to configure CDO
        String domain           = "mydomain";
        String username         = "jsmith";
        String password         = "password";
        // the DNS name or IP address of the machine where you installed CDO
        String CDOmachine       = "0.0.0.0";
        // the DNS name or IP address of the Exchange Server
        String exchangeServer   = "0.0.0.0";
        // the mailbox you wish to access (NOTE: this is the same as the user's login name
        // e.g., if the username is jsmith, mailbox would also be jsmith )
        String mailbox          = "jsmith";
        try {
            // authenticate to Windows
            com.linar.jintegra.AuthInfo.setDefault(domain, username, password);
            // create a Session in the CDOmachine
            Session session = new Session(CDOmachine);
            // logon to the Exchange Server
            session.logon(null, null, new Boolean(false), new Boolean(true),
                            new Boolean(false), new Boolean(false),
                            exchangeServer + "\n" + mailbox);
            // get the Global Address List
            AddressList addressList = new AddressListProxy(session.getAddressList(
                            new Integer(CdoAddressListTypes.CdoAddressListGAL)));
            // get the collection of addresses
            AddressEntries addressEntries = new AddressEntriesProxy(
                            addressList.getAddressEntries());
            // loop through the addresses and print them out
            int count = ((Integer) addressEntries.getCount()).intValue();
            printAddress(addressEntries, count, false);
        }
        catch (Exception e) {
            e.printStackTrace();
            com.linar.jintegra.Cleaner.releaseAll();
        }
    }
    // recursive function for printing out all address and addresses in distribution lists
    public static void printAddress(AddressEntries ae, int count, Boolean distList) throws Exception {
        if (count < 1) {
            System.out.println("No address entries found.");
        }
        else {
            level = level + 1;
            try {
                for(int i = 1; i <= count; i++) {
                    AddressEntry addressEntry = new AddressEntryProxy(
                        ae.getItem(new Integer(i)));
                    if(distList) {
                        // tree appearance
                        for(int ts = 1; ts < level; ts++) {
                            System.out.print("    ");
                        }
                        System.out.println("|--> NAME    : " + addressEntry.getName());
                        // tree appearance
                        for(int ts = 1; ts < level; ts++) {
                            System.out.print("    ");
                        }
                        try {
                            System.out.println("|--> ADDRESS : " + getAddress(addressEntry));
                        }
                        catch (Exception e) {
                            System.out.println("|--> ADDRESS : Not Available");
                        }
                    }
                    else {
                        System.out.println("NAME    : " + addressEntry.getName());
                        try {
                            System.out.println("ADDRESS : " + getAddress(addressEntry));
                        }
                        catch (Exception e) {
                            System.out.println("ADDRESS : Not Available");
                        }
                    }
                    // check if the entry is a distribution list
                    if(((Integer) addressEntry.getDisplayType()).intValue() == CdoDisplayType.CdoDistList) {
                        AddressEntries addressDL = new AddressEntriesProxy(addressEntry.getMembers());
                        printAddress(addressDL, ((Integer) addressDL.getCount()).intValue(), true);
                    }
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                level = level - 1;
            }
        }
    }
    // get the email address of an AddressEntry
    public static String getAddress(AddressEntry ae) throws Exception {
        final int g_PR_SMTP_ADDRESS_W = 972947487; // field ID for SMTP address
        String address = ae.getAddress().toString();
        if(address.indexOf("@") < 1 ){ // address is not in SMTP format
            Fields fields = new FieldsProxy(ae.getFields());
            Field field = new FieldProxy(fields.getItem(new Integer(g_PR_SMTP_ADDRESS_W), null));
            address = field.getValue().toString();
        }
        return address;
    }
}

Related Articles
No Related Articles Available.

Article Attachments
No Attachments Available.

Related External Links
No Related Links Available.
Help us improve this article...
What did you think of this article?

poor 
1
2
3
4
5
6
7
8
9
10

 excellent
Tell us why you rated the content this way. (optional)
 
Approved Comments...
No user comments available for this article.
Created on 6/23/2006.
Last Modified on 5/4/2010.
Last Modified by J-Integra KB Admin.
Article has been viewed 29833 times.
Rated 3 out of 10 based on 848 votes.
Print Article
Email Article