A Simple Java-to-Exchange Program

Now that we have completed configuring CDO, we will run a simple Java application that interacts with Exchange. This application sends a meeting request to specified Exchange users. Here are the steps:

  1. Determine which machine to use in running the Java-to-Exchange program. For now, let's run the program in the CDO host machine. For actual deployment, the Java-Exchange program can be run in any machine where Java is supported. We will refer to the machine running the Java-to-Exchange program as the client machine.
  2. Install a Java SDK and the J-Integra® for Exchange SDK on the client machine. To do this, follow the steps provided in the installation instructions.
  3. The Java application we are going to run requires the following Java packages:
           C:\jintegra\lib\jintegra.jar
           C:\jintegra\lib\cdo.jar
  4. Cut and paste the code below and save it as SendMeetingRequest.java. Modify the code as instructed. See code for details.
    import com.linar.jintegra.Cleaner;
    import com.intrinsyc.cdo.*;
    import java.util.Date;
    
    public class SendMeetingRequest {
    
        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";
    
            // e-mail address of recipients
            String recipient1    = "[email protected]";
            String recipient2    = "[email protected]";
    
            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);
    
                // retrieve appointments collection from the Calendar
                Integer defaultCalendar = new Integer(CdoDefaultFolderTypes.CdoDefaultFolderCalendar);
                Folder calendar = new FolderProxy(session.getDefaultFolder(defaultCalendar));
                Messages appointments = new MessagesProxy(calendar.getMessages());
    
                // add a new appointment to the collection
                AppointmentItem appointment = new AppointmentItemProxy(appointments.add(null, null, null, null));
    
                // set appointment details
                appointment.setSubject("J-Integra® for Exchange Evaluation");
                appointment.setLocation("Meeting Room - 10th Floor");
                appointment.setStartTime( new Date());  // appointment starts now
                appointment.setText("Discuss viability of J-Integra®");
    
                // Set appointment recipients
                Recipients recipients = new RecipientsProxy(appointment.getRecipients());
                Recipient r1 = new RecipientProxy(recipients.add(null, null, null, null));
                Recipient r2 = new RecipientProxy(recipients.add(null, null, null, null));
                r1.setName(recipient1);
                r2.setName(recipient2);
    
                // r1 will be the 'to' address and r2 will be 'cc'-ied
                r1.setType(new Integer(CdoRecipientType.CdoTo));
                r2.setType(new Integer(CdoRecipientType.CdoCc));
    
                // Each recipient added must be resolved before it can be used.
                // 0 == false to inhibit dialog boxes from popping up
                r1.resolve(new Integer(0));
                r2.resolve(new Integer(0));
    
                // set meeting status type to CdoMeeting
                appointment.setMeetingStatus(new Integer(CdoMeetingStatusTypes.CdoMeeting));
    
                // send the appointment
                appointment.send(null, null, null);
    
                System.out.println("Meeting request has been sent to: \n \t" + recipient1 + "\n \t" + recipient2 );
    
                // logoff from the session
                session.logoff();
            } catch (Exception ex) {
                ex.printStackTrace();
            }finally{
                // release all COM references
                Cleaner.releaseAll();
            }
        }
    }
  5. Compile and run the Java program as follows:
  6. You should get an output similar to the following assuming everything went well:

    You can also verify in Outlook whether the meeting request has been sent to the specified recipients.