Java Servlet to Outlook Example

Summary

In this example you will learn how to use J-Integra® to access Microsoft Outlook from a Java Servlet on your local machine using only Java classes to access Outlook. The example will retrieve information from your Outlook contacts folder and display it in your web browser.

Introduction

Accessing Outlook on a remote or local machine from a Java Servlet can be extremely difficult for Java programmers. J-Integra® makes this easy by providing Java proxies that enable you to access all Outlook interfaces methods with no knowledge of COM or DCOM.

Java Servlets running inside any Web server that supports a standard JVM are able to use J-Integra® to send and retrieve data to and from Outlook. The Web server can itself of course be running on any platform.

Prerequisites

  1. You should be familiar with Java Servlets and be able to install and configure your web server for access to the sample Java Servlet.

  2. Download and install J-Integra®. On this example we assume you have installed it under c:\jintegra.

  3. Make sure the jintegra.jar is included in your CLASSPATH environment variable.

  4. Ensure that you have installed the J-Integra® license.
  5. Download and install JDK 1.2.x or higher.

  6. Read the Java™ Servlet to COM JSP/Servlet to Excel Example, since it introduces some basic concepts that are assumed here.

  7. You should have Microsoft Outlook installed on your network. For more information about Microsoft Outlook programming, please refer to the Microsoft Office Outlook VBA Language Reference.
  8. This example assumes you are using Windows 2000 with Office 2000 installed.

The Steps Involved

  1. Generate the Java proxies for Outlook Library using J-Integra®
  2. Copy the Java Servlet code, compile and copy the proxy class files to the web server
  3. Configure the DCOM settings on your computer.
  4. Deploy the example
  5. Run the example
  6. Remote Access

Generate the Java proxies for Outlook Library using J-Integra®

  1. Create a sub-directory in the target directory and call this sub-directory outlook
    Note:
    the name is case sensitive
  2. Start the J-Integra® com2java tool to generate the proxies for MSOUTL9.dll.
  3. Enter the target directory into the Output Dir
  4. Enter outlook into the Java Package field

Copy the Java Servlet code, compile and copy the proxy class files to the web server

  1. Copy the following code in a file and call it "OutlookExample.java". This file is included in your jintegra\examples\servlet-com/ServletToOutlook directory when you download J-Integra®.

  2. import java.io.*;
    import java.text.*;
    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    import outlook.*;
    
    public class OutlookExample extends HttpServlet {
      public void doGet(HttpServletRequest request,
                           HttpServletResponse response) throws IOException, ServletException {
        PrintWriter out = null;
        out = response.getWriter();
        try {
          com.linar.jintegra.Cleaner.trackObjectsInCurrentThread();
          response.setContentType("text/html");
          out.println("<html>");
          out.println("<head>");
          out.println("<title>");
          out.println("Demonstration of Simple Java to Outlook";
          out.println("</title>");
          // No need to do this if running under Windows, and J-Integra® 'bin' directory is in
          // your PATH or if you require a specific used to be used for authentification.
          //com.linar.jintegra.AuthInfo.setDefault("Domain", "username", "password");
          String hostname = "localhost";
          // Connect outlook.
          Application app = new Application(hostname);
          _NameSpace namespace = app.getNamespace("MAPI");
          MAPIFolder contacts = namespace.getDefaultFolder(OlDefaultFolders.olFolderContacts);
          _Items items = contacts.getItems();
          items.sort("[LastName]", new Boolean(true));
          out.println("<br>");
          out.println("<table BORDER=7 CELLPADDING=10>");
          out.println("<tr><TH>Surname</TH><TH>Given Name</TH><TH>Business Address</TH><TH>email Address</TH><TH>Address Type</TH></tr>");
          //output all entries in the contact folder
          for(int i = 1; i <= items.getCount() && i <= 15; i++) {
            ContactItem contactItem = new ContactItem(items.item(new Integer(i)));
            // Outlook raises 'exceptions' for any objects
            // which are not contact items, so we catch and continue.
            try{
              String fullName = contactItem.getFullName();
              String address = contactItem.getBusinessAddress();
              String emailAddress = contactItem.getEmail1Address();
              out.println("<tr>";
              out.println("<td>" + contactItem.getLastName() + "</td>");
              out.println("<td>" + contactItem.getFirstName() + "</td>");
              out.println("<td>" + contactItem.getBusinessAddress() + "</td>");
              out.println("<td>" + contactItem.getEmail1Address() + "</td>");
              out.println("<td>" + contactItem.getEmail1AddressType() + "</td>");
              out.println("</tr>");
            } catch (java.lang.Exception e) {
              PrintWriter pw = new PrintWriter(out);
              e.printStackTrace(pw);
              pw.flush();
            }
          }
          out.println("</table>");
          out.println("<br>");
          out.println("</head>");
          out.println("<body bgcolor=\"white\">");
          out.println("</body>");
        } catch (java.lang.Exception e) {
          PrintWriter pw = new PrintWriter(out);
          e.printStackTrace(pw);
          pw.flush();
        } finally {
          com.linar.jintegra.Cleaner.releaseAllInCurrentThread();
        }
      }
    }

  3. Compile "OutlookExample.java" along with the J-Integra® generated proxies using your java development tool or using the JDK javac tool on command line. (Remember that you will need to have servlet.jar and jintegra.jar to compile the sample Java Servlet and J-Integra® proxy source files).

  4. In order to have access to a DCOM object, you need to be authenticated. Please see Security/Authentication for information on authentication and security.

  5. Copy the generated OutlookExample.class file and all the J-Integra® Java proxy classes in the outlook directory to your chosen Servlet installation directory for your installed web server.

Configure the DCOM settings on your computer

  1. The example uses the default DCOM mode, so it may be necessary to make some changes to your DCOM settings. Click the Start button and then click Run. Type dcomcnfg to run the dcomcnfg utility. In the Distributed COM Configuration window, click the Applications tab and then select Outlook Message Attachment.

  2. Click Properties and click the Identity tab. Select The interactive user.

  3. Click OK and then OK again to exit Distributed COM Configuration. Ensure that the user account that is used to authenticate for DCOM is the same as the interactive user on your computer, otherwise Outlook will not allow a connection to be made.

Deploying the Example

For information on deploying your servlet or JSP onto your Java Application Server or Web Container, see the following procedures.

Deploying Servlets

Deploying Servlets to J2EE

Deploying Servlets to JRun

Deploying Servlets to TomCat

Deploying Servlets to WebSphere

Deploying JSPs

Deploying JSPs to J2EE

Deploying JSPs to JRun

Deploying JSPs to TomCat

Deploying JSPs to WebSphere

Run the example

  1. Depending on your web server configuration, you may need to change the path to the OutlookExample. You should see items in the contact folder for the interactive user in Outlook.

Remote Access

When running under Windows, J-Integra® can use native code to pick up your current login identity. If you would rather that J-Integra® did not do this, or if you are not logged in to an NT domain (for example if your Java code is on a UNIX box), then you must configure DCOM access to Outlook, and you must specify the NT domain, user and password to be used by the J-Integra® runtime.

Proceed to Configuring DCOM for Remote Access

Conclusion

Using J-Integra® you have enabled a Java Servlet to interact with your Outlook client. The Servlet has read information from your Outlook contact folder and displayed the results.

Although this is a very trivial example, it demonstrates the power of the J-Integra®. Remember that you can also access other Outlook folders such as Inbox, Outbox, Calendar, Notes ,and Tasks from your local or remote Windows computer or access Outlook remotely from a non-Windows computer.