I am writing this in hopes of helping others out there.
I recently needed to connect to a Microsoft Exchange box from Domino to retrieve mail. I was not allowed to forward the mail to a Notes mail in database for this purpose. I looked at a few options and settled on Microsoft's EWS services api for java. The version I am using is a bit old as of this writing (ews-java-api-2.1.jar) but it works for what I need. I just need to read incoming messages from a specified mailbox and render them to a web front end in Domino.
there is a tool called EWS Editor that helped me get thecorrect exchange URL. Use this to get all your parameters you are not sure of.
You will need the following jar files:
ews-java-api-2.1.jar
httpclient-4.5.2.jar
httpcore-4.4.4.jar
commons-codec-1.10.jar
commons-io-2.5.jar
commons-lang3-3.4.jar
comons-logging-1.2.jar
joda-time-2.9.4.jar
Here is my basic code, you can build from this as it creates the connection, authenticates and returns the mail.
import java.net.URI;
import static java.lang.System.*;
import java.io.PrintWriter;
import lotus.domino.AgentBase;
import lotus.domino.AgentContext;
import lotus.domino.Session;
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
import microsoft.exchange.webservices.data.core.service.item.*;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.InternetMessageHeader;
import microsoft.exchange.webservices.data.property.complex.InternetMessageHeaderCollection;
import microsoft.exchange.webservices.data.property.complex.MessageBody;
import microsoft.exchange.webservices.data.search.FindItemsResults;
import microsoft.exchange.webservices.data.search.ItemView;
import microsoft.exchange.webservices.data.*;
public class JavaAgent extends AgentBase {
private static ExchangeService service;
public void NotesMain() {
try {
Session session = getSession();
AgentContext agentContext = session.getAgentContext();
// (Your code goes here)
PrintWriter pw = getAgentOutput();
// EWSDemo exch=new EWSDemo();
System.out.println("Exchange Test Start");
service = new ExchangeService();
// Provide Crendentials
ExchangeCredentials credentials = new WebCredentials("username", "password"); //You do not need to supple the domain, does not work if you do.
service.setCredentials(credentials);
// Set Exchange WebSevice URL
String sURL="https://yourexchangemailserver/ews/exchange.asmx";
System.out.println(sURL);
service.setUrl(new URI(sURL));
// Get five items from mail box
ItemView view = new ItemView(5);
System.out.println("setURL");
// Search Inbox
FindItemsResults<Item> findResults = service.findItems(WellKnownFolderName.Inbox, view);
System.out.println("FindItemsResults");
//iterate thru items
for (Item item : findResults.getItems()) {
item.load();
String subject=item.getSubject();
//Read Body
String body = MessageBody.getStringFromMessageBody(item.getBody());
//System.out.println(subject);
// System.out.println(body);
pw.println("<br>" + subject);
pw.println("<br>" + body);
pw.println("--------------------------------------------");
//Reading Header
InternetMessageHeaderCollection headers = item.getInternetMessageHeaders();
for(InternetMessageHeader header:headers){
//System.out.println("Header Name: "+header.getName()+" Header Value: "+header.getValue() );
//pw.println("Header Name: "+header.getName()+" Header Value: "+header.getValue() );
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
}
Hope this is a help.