Creating and Modifying Contact Items

  1. Displaying Contact Properties

  2. Creating a New Contact Item

Make sure you have completed all the configuration requirements before running any of the examples.

1. Displaying Contact Properties

The following example loops through all the properties of a Contact item and prints them out one at a time.

import com.linar.jintegra.Cleaner;
import com.intrinsyc.cdo.*;

public class ListContactProperties {

    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 Contacts folder
            Integer folderType = new Integer(CdoDefaultFolderTypes.CdoDefaultFolderContacts);
            Folder contactFolder = new FolderProxy(session.getDefaultFolder(folderType));

            // get the contents of the Contacts folder as Messages
            Messages contacts = new MessagesProxy(contactFolder.getMessages());

            // loop through collection and print out contact info
            int count = ((Integer)contacts.getCount()).intValue();
            for(int i=1; i<=count; i++){
                Message contact = new MessageProxy(contacts.getItem(new Integer(i)));
                System.out.println(i + ". -----------------------------------");
                //print out all the fields/properties for this contact
                Fields fields = new FieldsProxy(contact.getFields());
                int fieldCount = ((Integer)fields.getCount()).intValue();
                for(int j=1; j<=fieldCount; j++ ){
                    Field field = new FieldProxy(fields.getItem(new Integer(j), null));
                    System.out.print(field.getID() + " \t : ");
                    System.out.println(field.getValue());
                }
            }

            // logoff from the session
            session.logoff();
        } catch (Exception ex) {
            ex.printStackTrace();
        }finally{
            // release all COM references
            Cleaner.releaseAll();
        }
    }
}

2. Creating a New Contact Item

The following example shows you how to create a new Contact Item and save it in the default Contacts folder of Outlook. Creating a contact item involves getting/setting of CDO fields. To make things easier, we have created a wrapper class (Contact.java) that exposes methods to easily access and modify the most common properties of a Contact item. Before running the example below, make sure to save the its source code to a file called Contact.java and include it in your project.

import com.linar.jintegra.Cleaner;
import com.intrinsyc.cdo.*;

public class ListContactProperties {

    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 Contacts folder
            Integer folderType = new Integer(CdoDefaultFolderTypes.CdoDefaultFolderContacts);
            Folder contactFolder = new FolderProxy(session.getDefaultFolder(folderType));

            // get the contents of Contacts as a collection of Messages
            Messages contactsList = new MessagesProxy(contactFolder.getMessages());

            // add a new entry/activity to the collection
            MessageProxy message = new MessageProxy(contactsList.add(null, null, null, null));

            // cast Message object as Contact object
            Contact contact = new Contact(message);

            // set the personal details of the Contact item
            contact.setFirstName("John");
            contact.setMiddleName("M.");
            contact.setLastName("Doe");

            // set contact numbers
            contact.setBusinessPhoneNumber("(604) 252 2525");
            contact.setBusinessPhoneNumber2("(604) 585 5858");
            contact.setBusinessFaxNumber("(604) 123 4567");
            contact.setBusinessFaxNumber2("(604) 222 8989");
            contact.setMobilePhoneNumber("(604) 852 8521");
            contact.setHomePhoneNumber("(604) 599 9696");

            // set e-mail addresses
            contact.setEmailAddress("jdoe@intrinsyc.com");
            contact.setEmailAddress2("jdoe2@intrinsyc.com");
            contact.setEmailAddress3("jdoe3@intrinsyc.com");

            // set work-related details
            contact.setCompanyName("Intrinsyc Software International, Inc.");
            contact.setDepartmentName("R&D");
            contact.setJobTitle("Software Engineer");
            contact.setManagersName("Jane Smith");
            contact.setOfficeLocation("Building A 10th Floor");

            // set business address
            contact.setBusinessAddressStreet("700 West Pender St.");
            contact.setBusinessAddressCity("Vancouver");
            contact.setBusinessAddressState("BC");
            contact.setBusinessAddressPostalCode("V5S 3Y8");
            contact.setBusinessAddressCountry("Canada");

            // set home address
            contact.setHomeAddressStreet("123 Main St.");
            contact.setHomeAddressCity("Vancouver");
            contact.setHomeAddressState("BC");
            contact.setHomeAddressPostalCode("V6C 1G8");
            contact.setHomeAddressCountry("Canada");

            // set other fields
            contact.setFileAs("Doe, John");
            contact.setWebPage("www.intrinsyc.com");
            contact.setIMAddress("jdoe@msn.com");
            contact.setComment("Contact from Intrinsyc Software, forward all tech support " +
                    "questions to this contact");
            contact.setCategoriesEx(new String[]{"Software", "Client", "External"});
            contact.setUserField1("this text goes to user field 1");
            contact.setUserField2("this text goes to user field 2");

            // save changes to the Contact item
            contact.save();

            // print out details of the Contact item
            System.out.println("* * * A new entry has been added to the Contacts folder " +
                    "with the following details:");
            System.out.println("FIRST NAME: \t\t" + contact.getFirstName());
            System.out.println("MIDDLE NAME: \t\t" + contact.getMiddleName());
            System.out.println("LAST NAME: \t\t" + contact.getLastName());

            System.out.println("BusinessPhoneNumber: \t\t" + contact.getBusinessPhoneNumber());
            System.out.println("BusinessPhoneNumber2: \t\t" + contact.getBusinessPhoneNumber2());
            System.out.println("BusinessFaxNumber: \t\t" + contact.getBusinessFaxNumber());
            System.out.println("BusinessFaxNumber2: \t\t" + contact.getBusinessFaxNumber2());
            System.out.println("MobilePhoneNumber: \t\t" + contact.getMobilePhoneNumber());
            System.out.println("HomePhoneNumber: \t\t" + contact.getHomePhoneNumber());

            System.out.println("EmailAddress: \t\t" + contact.getEmailAddress());
            System.out.println("EmailAddress2: \t\t" + contact.getEmailAddress2());
            System.out.println("EmailAddress3: \t\t" + contact.getEmailAddress3());

            System.out.println("CompanyName: \t\t" + contact.getCompanyName());
            System.out.println("DepartmentName: \t\t" + contact.getDepartmentName());
            System.out.println("JobTitle: \t\t" + contact.getJobTitle());
            System.out.println("ManagersName: \t\t" + contact.getManagersName());
            System.out.println("OfficeLocation: \t\t" + contact.getOfficeLocation());

            System.out.println("BusinessAddressStreet: \t\t" + contact.getBusinessAddressStreet());
            System.out.println("BusinessAddressCity: \t\t" + contact.getBusinessAddressCity());
            System.out.println("BusinessAddressState: \t\t" + contact.getBusinessAddressState());
            System.out.println("BusinessAddressPostalCode: \t\t" + contact.getBusinessAddressPostalCode());
            System.out.println("BusinessAddressCountry: \t\t" + contact.getBusinessAddressCountry());

            System.out.println("HomeAddressStreet: \t\t" + contact.getHomeAddressStreet());
            System.out.println("HomeAddressCity: \t\t" + contact.getHomeAddressCity());
            System.out.println("HomeAddressState: \t\t" + contact.getHomeAddressState());
            System.out.println("HomeAddressPostalCode: \t\t" + contact.getHomeAddressPostalCode());
            System.out.println("HomeAddressCountry: \t\t" + contact.getHomeAddressCountry());

            // logoff from the session
            session.logoff();
        } catch (Exception ex) {
            ex.printStackTrace();
        }finally{
            // release all COM references
            Cleaner.releaseAll();
        }
    }
}