Microsoft Exchange is a library that provides an object-oriented model for the communication between applications that interact
Microsoft Exchange is a library that provides an object-oriented model for the communication between applications that interact with a Microsoft Exchange server, saving the developer from having to manually enter the required XML messages to communicate normally.
How to obtain it?
What versions of Microsoft Exchange I can use it with?
- Office 365
- Exchange Online
- Microsoft Exchange Server, from MSE 2007 (SP1) onwards
Here are some basic examples of use, to give an overview of what to do with the API.
Connecting to Exchange
After the instance of the class ExchangeService is configured, the connection is set, but that does not imply it is left open from that point on. When the Exchange query functions are used you miss this object, the connection is made, the requested data is retrieved and it closes automatically so there is no method to close a connection manually.
import java.net.URI; import microsoft.exchange.webservices.data.ExchangeCredentials; import microsoft.exchange.webservices.data.ExchangeService; import microsoft.exchange.webservices.data.ExchangeVersion; import microsoft.exchange.webservices.data.WebCredentials; ExchangeService exchangeService = new ExchangeService(ExchangeVersion.Exchange2010_SP1); ExchangeCredentials exchangeCredentials = new WebCredentials("exchange user", "exchange password"); exchangeService.setCredentials(exchangeCredentials); exchangeService.setUrl(new URI("exchange web server URL"));
Check all Appointments from an email box
The following code allows access to the “Calendar” folder associated with a particular email and brings all the appointments of that email in a determined period of time, and for that purpose, the statistics method of ExchangeService is used.findAppointments(FolderId, CalendarView).
Note: We are assuming the getExchangeService ( ) method returns an object of the ExchangeService class, configured as in the above example.
import java.util.Date; import java.util.ArrayList; import microsoft.exchange.webservices.data.ExchangeService; import microsoft.exchange.webservices.data.MailBox; import microsoft.exchange.webservices.data.FolderId; import microsoft.exchange.webservices.data.WellKnownFolderName; import microsoft.exchange.webservices.data.CalendarView; import microsoft.exchange.webservices.data.FindItemsResults; import microsoft.exchange.webservices.data.Appointment; ExchangeService exchangeService = getExchangeService(); Date startDate = new Date(1422270000000L); //Monday January 26th 2015 8:00 hs Date endDate = new Date(1422313200000L); //Monday January 26th 2015 20:00 hs Mailbox userMailbox = new Mailbox("user mail"); FolderId folderId = new FolderId(WellKnownFolderName.Calendar, userMailbox); CalendarView calendarView = new CalendarView(startDate,endDate); FindItemsResults<Appointment> searchResult = exchangeService.findAppointments(folderId,calendarView); ArrayList<Appointment> userAppointments = searchResult.getItems();
Updating an appointment
To update an appointment, a new object of the “Appointment” class is created and links it to the appointment to be updated by its ItemId and exchange server. When this is done, all the data from the appointment is obtained and it can be to worked with, appropriate modifications are made and the update method ( ConflictResolutionMode ) is invoked for the changes to take effect on the server.
import java.util.Date; import microsoft.exchange.webservices.data.ExchangeService; import microsoft.exchange.webservices.data.Appointment; import microsoft.exchange.webservices.data.MessageBody; import microsoft.exchange.webservices.data.ConflictResolutionMode; ExchangeService exchangeService = getExchangeService(); Appointment appointmentToBeUpdated = Appointment.bind(exchangeService,new ItemId("known appointment id string")); //Make changes to the appointment here appointmentToBeUpdated.setSubject("New subject"); appointmentToBeUpdated.setBody(new MessageBody("Changing the body too")); appointmentToBeUpdated.setEnd(new Date()); //Commit the changes to the Exchange Server appointmentToBeUpdated.update(ConflictResolutionMode.AlwaysOverwrite);
Eliminating an appointment
To eliminate an appointment, just like when upgrading it, it is necessary to know the appointment’s ItemID and have a properly configurated ExchangeService object. After performing the bind, the delete method (DeleteMode) can be invoked.
import microsoft.exchange.webservices.data.ExchangeService; import microsoft.exchange.webservices.data.Appointment; import microsoft.exchange.webservices.data.DeleteMode; ExchangeService exchangeService = getExchangeService(); Appointment appointmentToBeDeleted = Appointment.bind(exchangeService,new ItemId("known appointment id string")); appointmentToBeDeleted.delete(DeleteMode.HardDelete);