The process of building custom applications and tools that interact with Microsoft Exchange Server
A community member has associated this post with a similar question:
How to use Exchange Server Outlook interface in Java SpringBoot?
Only moderators can edit this content.
How to use Exchange Server Outlook interface in Java SpringBoot?
Hello, I previously asked a similar question, and I thought it was closed. Now, I have a new one, and we're truly unable to resolve it. Please help.
Our project background is this: We are a third-party software provider deployed on a client's server. The client requires our software platform to integrate their Outlook meeting content, enabling synchronization and binding between our software platform and Outlook meetings. Therefore, we need to integrate Outlook with our application. We've written a Java Spring Boot application to query Outlook meeting rooms. We can think of this application as Service A. For this query interface, we use the Maven dependency ews-api (version 2.0). The client's environment provides the Exchange Server address, username, and password. I've also noted the differences between Exchange Online and Exchange Server. Based on the previous question, I've verified that the client's WWW-Authenciate supports both Basic and NTLM (I can provide the curl command). My program still displays a timeout issue, and I need more information. Let me share my code with you:
/**
- Establishes a connection with the Exchange web server
- @return ExchangeService
- @throws Exception
*/
public ExchangeService createService(Map<String, String> jsonConfig) throws Exception {
/**
- @implNote: The email here is in the format email@domain
*/
String email = jsonConfig.get("email");
String password = jsonConfig.get("password");
String serverHost = jsonConfig.get("serverHost");
// Must be null
String domain = jsonConfig.get("domain");
// Establishes a connection with the Exchange web server
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
WebCredentials credentials = null;
boolean useNTLM = false;
if (StringUtils.isNotBlank(email) && email.contains("@")) {
// If the email contains an @ sign, extract the domain and use NTLM authentication.
if (StringUtils.isBlank(domain)) {
// Extract the content between the @ sign and the first dot as the domain.
int atIndex = email.indexOf('@');
int dotIndex = email.indexOf('.', atIndex + 1);
if (dotIndex > atIndex) {
domain = email.substring(atIndex + 1, dotIndex);
} else {
// If the dot is not found, extract everything after the @.
domain = email.substring(atIndex + 1);
}
}
credentials = new WebCredentials(email, password, domain);
useNTLM = true;
log.info("Using NTLM authentication - extracted domain: {}", domain);
} else {
// If the email does not contain an @, use Basic authentication.
credentials = new WebCredentials(email, password);
log.info("Using Basic authentication");
}
log.info("Parameters email:{} password:{} domain:{} serverHost:{} Authentication method:{}",
email, password, domain, serverHost, useNTLM ? "NTLM" : "Basic");
// WebCredentials credentials = new WebCredentials(email, password);
service.setCredentials(credentials);
service.setUrl(new URI("https://" + serverHost + "/ews/exchange.asmx"));
service.setTraceEnabled(true);
service.setPreAuthenticate(true);
service.getHttpHeaders().put("Connection", "close");
try {
long t0 = System.currentTimeMillis();
service.getServerTimeZones(); // Lightweight authentication: connectivity + authentication
log.info("getServerTimeZones cost={}ms", System.currentTimeMillis() - t0);
log.info("EWS authentication OK (Basic, the first packet may experience a 401 error, which is normal)");
} catch (Exception e) {
log.error("Other exceptions:", e);
}
return service;
}
and
public List<MeetingRoomDTO> listMeetingRoom(MeetingRoomInput input, Map<String, String> jsonConfig) {
List<MeetingRoomDTO> meetingRoomDTOList = new ArrayList<>();
try {
ExchangeService service = outlookApi.createService(jsonConfig);
Collection<EmailAddress> meetingRoomList = outlookApi.getMeetingRoomList(service);
meetingRoomList.forEach(room -> {
MeetingRoomDTO roomDTO = new MeetingRoomDTO()
.setRoomName(room.getName()).setRoomId(room.getAddress());
meetingRoomDTOList.add(roomDTO);
});
} catch (Exception e) {
log.error("msg:{}", e.getMessage());
throw BizException.buildException(HttpStatus.BAD_REQUEST.toString(), "OutLook failed to query the meeting room list: " + e.getMessage());
}
return meetingRoomDTOList;
}
You can see that the code is very concise. In the createService method, I've implemented a compatibility feature. If the username contains the @ special symbol, NTLM authentication is used. Otherwise, I use Basic authentication to create the ExchangeService object.
In my implementation class, I directly call the ExchangeService object's getRoomLists method (implemented in the Outlook API).
My questions are:
- Am I using the SDK correctly? Is there a problem with how I create the WebCredentials object? Does the ExchangeVersion enumeration class have any impact? Do different enumerations affect functionality?
- Having confirmed that I'm using the SDK correctly, when using Basic authentication (that is, when the username doesn't contain an @ symbol), the logs don't show any error messages, but timeout messages are still printed, as shown in the figure:
When using NTLM authentication (that is, when the username contains an @ symbol), a catchable business exception, a 401 authentication failure, occurs, as shown in the figure:
- So, having confirmed that the customer's Exchange Server service supports authentication methods, how can I troubleshoot this network connection timeout issue? Does this require verifying the customer's Exchange Server environment? For example, their network environment, and whether their username and password have the appropriate permissions? I need to understand whether this is a problem with the customer's environment or our code.
- The customer mentioned a certificate issue. Is this related to this timeout?
- Is there any official documentation available for reference on Java integration with Exchange Server?
I hope we can have a pleasant conversation. Thank you very much!