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