Open In App

Getting Data From Microsoft Exchange Server in Java

Improve
Improve
Like Article
Like
Save
Share
Report

In the software industry, many programs require information that you need to fetch from some kind of previously existing business software. In other words, you will probably come across the need to integrate with software from Microsoft, Salesforce, SAP, and other software giants. And, although it might seem intimidating, you can do it! Let me show you how.

Need for Integrating With Microsoft Exchange Server

The most common way of integrating with all types of software is to use their API. The big software companies provide them exactly to make integration possible. 
Let’s use Microsoft Exchange as an example. Different operations that you can perform using Exchange’s API in Java are: 

  1. Read e-mails.
  2. Send e-mails.
  3. Get information from the appointments on a calendar.
  4. Insert a new appointment.
  5. Edit an existing appointment.
  6. Get information from the task list.
  7. Insert a new task.
  8. Edit an existing task.
  9. Read from a public folder.
  10. Write into a public folder.

How To Integrate With Microsoft Exchange Server?

The most traditional way to integrate with Exchange from Java used to be the Exchange Web Services (EWS)

  1. EWS SDK is a SOAP-based API.
  2. It launched as a part of Microsoft Exchange 2007.
  3. It is not easy to use, but the real problem is that, back in July 2018, Microsoft declared they would no longer do any feature updates.

Microsoft Graph

Moreover, from Oct 13, 2020, onwards, Microsoft expects everyone using Exchange Web Services for Office 365 integration to switch to Microsoft Graph. Also, on Oct 13, 2020, support for Basic Authentication in Exchange Web Services (EWS) will stop. 

Microsoft wants everything moved over to OAuth and Microsoft Graph. Microsoft Graph was launched back in 2015 with Office 365. It delegates calls to several Office 365 Cloud services via a single endpoint. Within Microsoft Graph, you have two options: 

  1. Microsoft Graph data connect.
  2. Microsoft Graph API.

Microsoft is precise about what you can and cannot do with their API, so it is best to check it out beforehand. You should look into Microsoft Graph data connect if (and only if) you want to work with Big data or need granular consent. If that is not the case, you want the regular API, which is the most common option. 

Two versions of Microsoft Graph REST API currently exist – version 1.0 and beta. You can check the beta version, but for any serious development, you should, naturally, keep to the production version 1.0. For that version, make sure you read the documentation and learn the Java SDK

Ways To Connect To An API

There are two ways in which we can connect to an API: 

  1. Microsoft Graph API.
  2. Integration Platform.

Microsoft API

When you get to the part of actually using the API, you should start by getting:

  1. Microsoft Account: It can be a company or school account. To get an Application ID.
  2. Register your Application: Register your application using:
    • The Microsoft Application Registration Portal.
    • The Azure Active Directory Management Portal.
  3. Application ID: Once you have registered, an application ID will be created for your app and displayed on your new app’s properties page.

You should use The Azure Active Directory Management Portal for enterprise-class applications.
NOTE: Microsoft Graph changes all the time. Take a look at the changelog to get an idea of the changes that have already happened (since Dec 2015). If you want to work with it, make sure to check which is the current production version of the API.

Integration Platform

  1. You can think of the integration platform as a thin layer you can put between you and the API.
  2. You write your code for the integration platform, which is *much* simpler.
  3. Then it is the integration of the platform’s job to translate and “speak” with the API. Unlike the API, you need to pay to use an integration platform, but you usually can start with a free trial.
  4. Example: Connect Bridge Platform

Connect Bridge Platform: The good thing about this specific integration platform is that it ensures forward and backward compatibility. This means that you build your Java code with Connect Bridge, and then admins can upgrade to a new Exchange version, Microsoft can launch new versions of the API. And you won’t have to worry about any of that. 

How Integration Platform Works

  1. Connect Bridge translates SQL statements into API calls.
  2. What you put on your code is standard SQL. You write the normal SELECT, UPDATE, INSERT and DELETE statements.
  3. It works as if you are getting data to and from a relational database using JDBC.
  4. You can even use stored procedures.

Note: It is all just a layer of abstraction. You are accessing the API, in fact, not Microsoft Exchange’s database.

Problems With Integration Platform

The problem with some integration platforms is that they only work in a specific environment, but in the particular case of Connect Bridge, that is not an issue because you can run it on:

  • An Azure-built SaaS platform.
  • On-premises/self-hosted (on your own virtual machines or cloud servers).

Implementation

In this section, we will see how we can use Connect Bridge Management Studio to integrate our Application to the API.

Approach

Assuming the Exchange and SharePoint instances already exist, these are the preparation steps you need to follow:

1. Have your target system login information at hand (in this case, Exchange and SharePoint).

2. Install Connect Bridge.

3. Run Connect Bridge Management Studio.

  • Add an account for SharePoint (Accounts – Add account). For adding the account, you should select the connector CBSharePointConnector and use the credentials from point 1.
  • Add an account for Exchange (Accounts – Add account).  For adding the account, you should select the connector MGEXPlugin2010 and use the credentials from point 1.
  • For each account, make sure you test the connection so that you know everything is OK with the credentials and parameters you used.

4. Open the New Query option and then the Connection Browser. Find the Exchange connector and expand it until you see the DefaultConnection. Right-click the DefaultConnection and choose the option Get Connection string. You will need it to pass it on to the script, so copy the JDBC connection string.

5. Open the New Query option and then the Connection Browser. Find the SharePoint connector and expand it until you see the DefaultConnection. Right-click the DefaultConnection and choose the option Get Connection string. You will need it to pass it on to the script, so copy the JDBC connection string.

Example: In the below example, we have discussed a java code to integrate Microsoft Exchange and SharePoint.

Java




// Java Program to demonstrate
// integration of Exchange and
// SharePoint
  
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
  
class Program {
  
    public static void main(String[] args)
        throws SQLException, ClassNotFoundException
    {
  
        Class.forName("com.cnsconnect.mgw.jdbc.MgDriver");
  
        // STEP 1:Create a JDBC connection
        // for each target server you get
        // the data for the connection string
        // from Connect Bridge
        String exchangeConnectionString
            = "jdbc:MgDriver:IMPL=CORBA;ENC=UTF8;"
              + "HOST=123.456.789.000;PORT=8087;"
              + "UID=demouser;PWD='password';"
              + "ACC=accountExchange;";
        String sharepointConnectionString
            = "jdbc:MgDriver : IMPL = CORBA; ENC = UTF8;"
              + "HOST = 123.456.789.000; PORT = 8087;"
              + "UID=demouser; PWD = 'password';"
              + "ACC = accountSharePoint;";
        Connection exchangeConn
            = DriverManager.getConnection(
                exchangeConnectionString);
        Connection sharepointConn
            = DriverManager.getConnection(
                sharepointConnectionString);
        Statement exchangeSt
            = exchangeConn.createStatement();
        System.out.println("Connecting to Exchange...");
  
        // STEP 2: Provide an appropriate
        // object  like a ResultSet +
        // STEP 3: Fill the object with data
        // from the source server
        ResultSet exchangeRs = exchangeSt.executeQuery(
            "SELECT * FROM [Appointment]");
  
        // Create a new statement for
        // inserting PreparedStatement
        // using JDBC
        sharepointSt = sharepointConn.prepareStatement(
            "INSERT INTO [Calendar] "
            + "([Title],[Description], [Location],"
            + "[StartTime], [EndTime]) "
            + "VALUES ( ?, ?, ?, ?, ?)");
  
        // STEP 4: Manipulate the data or
        // apply a workflow rule (or both)
        // in this sample: check if the
        // appointment is private,
        // if not, insert it into SharePoint
        while (exchangeRs.next()) {
            Boolean isPrivate
                = exchangeRs.getBoolean("IsPrivate");
            if (isPrivate != null & amp; & isPrivate) {
                System.out.println(
                    "Skipping '"
                    + exchangeRs.getString("Subject")
                    + "'");
                continue;
            }
  
            // Fill these parameters with values
            // for the SharePoint account
            sharepointSt.setString(
                1, exchangeRs.getString("Subject"));
            sharepointSt.setString(
                2, exchangeRs.getString("Body"));
            sharepointSt.setString(
                3, exchangeRs.getString("Location"));
            sharepointSt.setTimestamp(
                4, exchangeRs.getTimestamp("StartDate"));
            sharepointSt.setTimestamp(
                5, exchangeRs.getTimestamp("EndDate"));
  
            System.out.println(
                "Inserting '"
                + exchangeRs.getString("Subject") + "'");
  
            // STEP 5: Insert data into
            // the target server
            sharepointSt.execute();
        }
  
        // STEP 6: Clean up and
        // lose the connections
        exchangeRs.close();
        exchangeSt.close();
        sharepointSt.close();
        exchangeConn.close();
        sharepointConn.close();
    }
}


Output:



Last Updated : 04 Dec, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads