JavaMail4EWS is meant as full JavaMail Implementation for Exchange-based mailboxes. The required steps to use the library in your project are outlined below.

Gathering nessassary information

To use this library, an Exchange Server with Exchange WebService is required. This WebService is availible for Exchange 2007SP1 and 2010. Please contact your Exchange Administrator to get the EWS-URL.

The EWS-URL will look like https://example.com/ews/exchange.asmx.
Most of the time the EWS will be published together with the OWA: If the OWA-URL is for example https://owa.example.com/owa, EWS is availible at https://owa.example.com/ews/exchange.asmx.

The EWS-URL can be tested in any browser (except Internet Explorer). The request should be forwarded to https://example.com/ews/Services.wsdl and a WSDL should be send to the browser.

You also need a valid Exchange Account (username (i.e. a mail address) and a matching password).

Adding JARs to the CLASSPATH

The JavaMail4EWS consists of the two JARs provided via the archive file in the download section.

  • ewsjavaapi-X.Y.Z.jar
    • This is the pure Exchange WebService library provided by Microsoft. It uses a custom API but allows access to all types of Exchange information (Mails, Contacts, Calendars etc.)
  • javamail-ews-bridge-X.Y.Z.jar
    • This is the "glue" between the EWS-API provided by Microsoft and the JavaMail interface contract.

These JARs must be on the CLASSPATH. Also the 3rd party libraries provided in the lib/ folder must be added to the CLASSPATH.

Additional configuration is not required as the library registers itself. The transport implementation can be acquired via the pseudo-protocol ewstransport.
The store implementation can be acquired via the pseudo-protocol ewsstore.

Using the library

All functionality is exported via the general JavaMail API with no additional API. You may use all the Session-properties as before - only the dedicated port is always ignored as it is provided via the EWS-URL which is always used as server!

Accessing the Mailbox (Store)

Accessing the mailbox can be done via the regular JavaMail interfaces:

                //Initalize a session with no properties
                //Can also be initalized with System.getProperties();
                Session session = Session.getDefaultInstance(new Properties());

                //Get the EWS store implementation
                Store store = session.getStore("ewsstore");

                //Connect to the Exchange server - No port required.
                //Also connect() might be used if the session is initalized with the known mail.* properties
                store.connect(
                                "https://example.com/ews/exchange.asmx",
                                "test@example.com",
                                "password");
                
                Folder folder = store.getDefaultFolder();
                folder.open(Folder.READ_ONLY);
                
                Message[] messages = folder.getMessages();
                // Your message logic here

Note: Please note that the implementation is done on a use-case basis. The current implementation is known to be incomplete. Please raise a ticket to outline what your use-case is and what is missing.

Sending mails (Transport)

Sending an email is as easy as with ESMTP - An initalized Transport object is required as no anonymous access is possible with EWS.

                //Initalize a session with no properties
                //Can also be initalized with System.getProperties();
                Session session = Session.getDefaultInstance(new Properties());
                
                //Get the EWS transport implementation
                Transport lTransport = session.getTransport("ewstransport");

                //Connect to the Exchange server - No port required.
                //Also connect() might be used if the session is initalized with the known mail.* properties
                lTransport.connect(
                                "https://example.com/ews/exchange.asmx",
                                "test@example.com",
                                "password");

                //Create a message as before 
                Message lMessage = new MimeMessage(session);
                lMessage.setRecipient(RecipientType.TO, new InternetAddress("user@example.com"));
                lMessage.setSubject("Hello World!");
                lMessage.setText("Hello World!");
                
                //Send the mail via EWS 
                lTransport.sendMessage(lMessage, lMessage.getRecipients(RecipientType.TO));