Getting Data From Microsoft Exchange Server in Java - GeeksforGeeks

04 Dec, 2020
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.
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: 
Read e-mails.Send e-mails.Get information from the appointments on a calendar.Insert a new appointment.Edit an existing appointment.Get information from the task list.Insert a new task.Edit an existing task.Read from a public folder.Write into a public folder.
Read e-mails.
Send e-mails.
Get information from the appointments on a calendar.
Insert a new appointment.
Edit an existing appointment.
Get information from the task list.
Insert a new task.
Edit an existing task.
Read from a public folder.
Write into a public folder.
The most traditional way to integrate with Exchange from Java used to be the Exchange Web Services (EWS). 
EWS SDK is a SOAP-based API.It launched as a part of Microsoft Exchange 2007.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.
EWS SDK is a SOAP-based API.
It launched as a part of Microsoft Exchange 2007.
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: 
Microsoft Graph data connect.Microsoft Graph API.
Microsoft Graph data connect.
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. 
There are two ways in which we can connect to an API: 
Microsoft Graph API.Integration Platform.
Microsoft Graph API.
Integration Platform.
Microsoft API
When you get to the part of actually using the API, you should start by getting:
Microsoft Account: It can be a company or school account. To get an Application ID.Register your Application: Register your application using:The Microsoft Application Registration Portal.The Azure Active Directory Management Portal.Application ID: Once you have registered, an application ID will be created for your app and displayed on your new app’s properties page.
Microsoft Account: It can be a company or school account. To get an Application ID.
Register your Application: Register your application using:The Microsoft Application Registration Portal.The Azure Active Directory Management Portal.
The Microsoft Application Registration Portal.
The Azure Active Directory Management Portal.
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
You can think of the integration platform as a thin layer you can put between you and the API.You write your code for the integration platform, which is *much* simpler.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.Example: Connect Bridge Platform
You can think of the integration platform as a thin layer you can put between you and the API.
You write your code for the integration platform, which is *much* simpler.
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.
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
Connect Bridge translates SQL statements into API calls.What you put on your code is standard SQL. You write the normal SELECT, UPDATE, INSERT and DELETE statements.It works as if you are getting data to and from a relational database using JDBC.You can even use stored procedures.
Connect Bridge translates SQL statements into API calls.
What you put on your code is standard SQL. You write the normal SELECT, UPDATE, INSERT and DELETE statements.
It works as if you are getting data to and from a relational database using JDBC.
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).
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:
JDBC
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Constructors in Java
Exceptions in Java
Functional Interfaces in Java
Different ways of Reading a text file in Java
Generics in Java
Introduction to Java
Comparator Interface in Java with Examples
Internal Working of HashMap in Java
Strings in Java