Tuesday, April 8, 2008

Java Outlook Connector 2.0

Java Outlook Connector 2.0 Review

If you work with Java and need to access meetings, contacts, emails, etc from the users Outlook, you could have done it using Groovy and Scriptom. But to write Java code, instead of Groovy code, you can use Java Outlook Connector (JOC) from Moyosoft. JOC is basically a wrapper to COM for accessing Outlook. Once you have added JOC to your project and you have imported the JOC classes you can write code to read and write data to outlook. Here an example that reads your contacts and prints out the full name and company name for each contact…

  1. // Get Outlook Application
  2. Outlook outlook = new Outlook();
  3. // Find default Contact folder
  4. OutlookFolder contactFolder = outlook.getDefaultFolder(FolderType.CONTACTS);
  5. // Iterate over the contact items in the default contact folder
  6. for(ItemsIterator j = contactFolder.getItems().iterator(); j.hasNext(); ) {
  7. OutlookItem item = (OutlookItem)j.next();
  8. // Verify that the item is a contact
  9. if(item.getType().isContact()) {
  10. OutlookContact contact = (OutlookContact)item;
  11. // Print some contact info
  12. System.out.println("Full Name; "+contact.getFullName());
  13. System.out.println("Company; "+contact.getCompanyName());
  14. }
  15. }
  16. // Get contact subfolders
  17. FoldersCollection contactFolders = contactFolder.getFolders();
  18. for(FoldersIterator i = contactFolders.iterator(); i.hasNext(); ) {
  19. OutlookFolder folder = (OutlookFolder)i.next();
  20. for(ItemsIterator j = folder.getItems().iterator(); j.hasNext(); ) {
  21. OutlookItem item = (OutlookItem)j.next();
  22. if(item.getType().isContact()) {
  23. OutlookContact contact = (OutlookContact)item;
  24. // Print contact info
  25. System.out.println("Full Name; "+contact.getFullName());
  26. System.out.println("Company; "+contact.getCompanyName());
  27. }
  28. }
  29. }
  30. // Dispose the library
  31. outlook.dispose();

Here is another example that lists all folders (contact, meetings, inbox, etc) from Outlook in a neatly indented format. In this example I will create a function which will call itself recursively for subfolders.

  1. public static void searchFolders(FoldersCollection folders, String indent) {
  2. // Iterate over outlook foldres
  3. for (FoldersIterator fi = folders.iterator(); fi.hasNext();) {
  4. OutlookFolder folder = (OutlookFolder)fi.next();
  5. // Print the folder name
  6. System.out.println(indent+"Folder Name; "+folder.getName());
  7. // Recursively search subfolders
  8. searchFolders(folder.getFolders(), indent + " ");
  9. }
  10. }

To invoke the searchFolders method to actually print out all the folders in Outlook, I would use code like the following.

  1. // Get Outlook Application
  2. Outlook outlook = new Outlook();
  3. searchFolders(outlook.getFolders(), "");
  4. // Dispose the library
  5. outlook.dispose();

Again, for the above example to work you need to add the joc and moyocore jars to your project. Once these jars have been added you can import them. All the code in these examples throw exceptions which has been omitted to keep the samples simple.

Java Outlook Connector has support for Outlook notes, meetings, mails, contacts and more. If you are a Java developer that has to write software solutions that integrate with Outlook, you might already have discovered the usefulness of Java Outlook Connector.

1 comments:

Jack said...

Its great reviews on JAVA Outlook Connector. I was looking some good information on it and got it on your post. I must say that you did great job. Thank you!
digital signature Adobe Reader