0

I want to use the EWS-Java-API for my android application. The application requires a login to get programmatically access to the microsoft exchange server (e.g. calendar meetings, email, meeting rooms, and much more).

At this point almost the entire question is edited, just in case to show the situation so far.

I try to get connected to an exchange version 15.0.11 with the url ("https ://yourserver/EWS/Exchange.asmx")

Build gradle file:

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
useLibrary 'org.apache.http.legacy'

defaultConfig {        
    minSdkVersion 18
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
}

packagingOptions {        
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/LGPL2.1'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/notice.txt'
} [...] }

dependencies {
     compile fileTree(include: ['*.jar'], dir: 'libs')
     testCompile 'junit:junit:4.12'
     compile 'com.android.support:appcompat-v7:23.2.1'
     compile 'com.android.support:design:23.3.0'
     compile 'com.google.android.gms:play-services:6.5.87'
     compile 'org.altbeacon:android-beacon-library:2.7.1'
     compile 'org.greenrobot:eventbus:3.0.0'
     compile files('libs/jcifs-1.3.15.jar')
     compile files('libs/commons-logging-1.1.1.jar')
     compile files('libs/commons-codec-1.4.jar')
     compile files('libs/commons-httpclient-3.1.jar')
     compile 'com.microsoft.ews-java-api:ews-java-api:2.0' }

This will cause into this error message

 java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/client/protocol/HttpClientContext;

Line:

ExchangeService service = new ExchangeService();      

At this point i tested this ews libary "github.com/faisons/EWS-For-Android".

 compile files('libs/ews.jar')

This is almost working. Iam able to start the application and set up the ExchangeService.

 Thread thread = new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            try
            {
               ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
               ExchangeCredentials credentials = new WebCredentials(myuser, mypw);
               service.setCredentials(credentials);
               service.setUrl(new URI("https://exchmail.yourserver.com/EWS/Exchange.asmx"));
               EmailMessage message = new EmailMessage(service);
               EmailAddress from = new EmailAddress(myemail);
               message.setSender(from);                  
               message.getToRecipients().add(anotherEmail);
               message.setSubject("Much wow - such message");
               MessageBody messageBody = new MessageBody();
               messageBody.setText("text here");
               message.setBody(messageBody);
               message.send();

            } 
            catch (Exception e)
            {
                Log.e(TAG, "message", e);
            }
        }
    });

    thread.start();

The error now is:

java.lang.NullPointerException: format == null
    at java.lang.String.format(String.java:1799)
at java.lang.String.format(String.java:1777)
    at java.lang.String.format(String.java:1799)
    at microsoft.exchange.webservices.data.ServiceRequestBase.getEwsHttpWebResponse(ServiceRequestBase.java:936)
at microsoft.exchange.webservices.data.ServiceRequestBase.validateAndEmitRequest(ServiceRequestBase.java:821)
at microsoft.exchange.webservices.data.SimpleServiceRequestBase.internalExecute(SimpleServiceRequestBase.java:46)
at microsoft.exchange.webservices.data.MultiResponseServiceRequest.execute(MultiResponseServiceRequest.java:143)
at microsoft.exchange.webservices.data.ExchangeService.internalCreateItems(ExchangeService.java:463)
at microsoft.exchange.webservices.data.ExchangeService.createItem(ExchangeService.java:534)
at microsoft.exchange.webservices.data.Item.internalCreate(Item.java:215)
at microsoft.exchange.webservices.data.EmailMessage.internalSend(EmailMessage.java:125)

Iam confused how to get the api working. If somebody knows another way to get access any hint is welcome!

Lugo
  • 21
  • 6

2 Answers2

2

Greeting! I have good news, it works! Iam not sure if this is the perfect way, but i am able to send an Email

First of all make sure that you have the permissions declared in the manifes file:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

This is my Build.gradle file:

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
useLibrary 'org.apache.http.legacy'      // <---------------------

defaultConfig {
    applicationId "your application id"
    minSdkVersion 18
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
}

packagingOptions {
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/DEPENDENCIES.txt'
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/dependencies.txt'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/license.txt'
    exclude 'META-INF/LGPL2.1'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/notice.txt'
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:design:23.3.0
compile files('libs/ews-android-api.jar')    // <---------------------
compile 'joda-time:joda-time:2.8'}           // <---------------------

You can get the ews-android-api under this link: https://github.com/alipov/ews-android-api

Just follow the building instructions to create the jar file.

This is the basic command line for sending an email (run this as async task or in a new thread):

                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
                ExchangeCredentials credentials = new WebCredentials("user", "pw");
                service.setCredentials(credentials);
                service.setUrl(new URI("https:// s e r v e r /ews/exchange.asmx"));                    
                EmailAddress mEmail = new EmailAddress("your email / test email");

                EmailMessage message = new EmailMessage(service);
                message.getToRecipients().add(mEmail);
                message.setFrom(mEmail);
                message.setSubject("Hello world!");
                message.setBody(MessageBody.getMessageBodyFromText("Sent using the EWS Android API."));
                message.send();

You can check your credentials with the EWSEditor. Shout-out to DevilGeek and Alipov.

If something is not clear feel free to ask.

Kind regards!

EDIT: It seems to be working with almost everything. You do not need Office 365 in particular. For example iam able to call "service.getRoomLists();" aswell.

Lugo
  • 21
  • 6
  • I am getting "java.lang.NoClassDefFoundError: Failed resolution of: Ljava/applet/Applet" when I make a call to AutodiscoverService.getUserSettings. This has been documented at https://github.com/alipov/ews-android-api/issues/3 . How did you get around that? – NotAgain May 25 '16 at 05:50
0

Did you add the apache legacy lines into your gradle build?

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"
    useLibrary 'org.apache.http.legacy'
    ...
}

See this for more info: http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

Benoit
  • 1,168
  • 8
  • 16
  • Thanks for the fast answer Benoit! I added this and got the warning: Warning:Unable to find optional library: org.apache.http.legacy – Lugo Apr 15 '16 at 12:49
  • When i try to run the application i get this: Warning:Dependency commons-logging:commons-logging:1.2 is ignored for debug as it may be conflicting with the internal version provided by Android.In case of problem, please repackage it with jarjar to change the class packages Error:A problem occurred configuring project ':app'. > Unable to find optional library: org.apache.http.legacy – Lugo Apr 15 '16 at 12:53
  • I think you should remove your dependency `compile files('libs/org.apache.http.legacy.jar')` cause the legacy will be imported by the SDK. As for your error, you should take a look at that post: http://stackoverflow.com/questions/33898857/why-warningunable-to-find-optional-library-org-apache-http-legacy-occurs It seems that your SDK installation doesn't include the optionals. Reinstalling it may solve the problem. – Benoit Apr 15 '16 at 13:07
  • Hey Benoit. I reinstalled it and set the compileSdkVersion and the buildTools to 23. No errors anymore and iam able to start the application. Made my day thanks! – Lugo Apr 15 '16 at 13:54
  • @Lugo glad to hear that! Don't forget to mark the question as answered ;) – Benoit Apr 15 '16 at 14:08
  • Hey man, iam getting this error again when i want to create my service. "Caused by: java.lang.NoClassDefFoundError: org.apache.http.client.protocol.HttpClientContext". I found the class by myself in my external libaries, but do not know how to fix the NoClassDefFoundError. I already managed to avoid duplicate class entries and the http.legacy libars is working. – Lugo Apr 18 '16 at 09:40
  • Clean the whole project, build it again and do a fresh install of the app to be sure everything is not just a bad compilation cache problem – Benoit Apr 18 '16 at 09:42
  • Cleaned, rebuilded and i did a fresh installation on the device. Sadly the same error is happening. – Lugo Apr 18 '16 at 09:56
  • Are you stil using that `org.apache.http.legacy.jar` jar? You're not supposed to if you use the legacy line on Gradle. It can be the cause of the error. – Benoit Apr 18 '16 at 15:04
  • I just updated the post. I tried and managed to solve the second issue "ClassNotFoundException" by compiling 'org.apache.httpcomponents:httpclient-osgi:4.5.2' (with almost everything excluded to prevent duplicates, Iam not sure if this is the correct way of fixing it). Then the class is fround and only the ""NoClassDefFoundError" is remaining. – Lugo Apr 19 '16 at 07:44
  • Maybe just removing `exclude group: 'org.apache.httpcomponents'` from the `ews-java-api` compile line on Gradle? I never work with that API so I'm really not sure – Benoit Apr 19 '16 at 08:03
  • Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. > com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK META-INF/LICENSE The files are "\org.apache.httpcomponents\httpclient-osgi\4.5.2\3262c30d156f3ae05a5c95d9aa39f0e3eed17585\httpclient-osgi-4.5.2.jar" and \org.apache.httpcomponents\httpcore\4.4.1\f5aa318bda4c6c8d688c9d00b90681dcd82ce636\httpcore-4.4.1.jar". Not able to start the application. No problem, thanks alot for your help so far ;) – Lugo Apr 19 '16 at 08:24
  • It looks like it's the LICENSE file which cause the issue. try adding `android { packagingOptions { exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' } }` in your gradle file. – Benoit Apr 19 '16 at 08:26
  • The option "packagingOptions { exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' }" did not solve the problem. – Lugo Apr 19 '16 at 08:34
  • Have you looked at https://github.com/faisons/EWS-For-Android? I had the same problem, I guess I just took the wrong lib – Arnout Apr 22 '16 at 07:56
  • I downloaded github.com/faisons/EWS-For-Android. I compiled the exactly same jar files: "commons-codec-1.4", "commons-httpclient-3.1", "commons-logging-1.1.1", "ews.jar" and "jcifs-1.3.15". The application ist starting and no errors anymore at creating the service. It seems to be working fine now, but iam not able to send an email because of "java.lang.NullPointerException: format == null" – Lugo Apr 25 '16 at 12:10
  • This is strange. I managed to test my settings with the Microsoft Connectivity Analyzer, but iam now getting this error: "microsoft.exchange.webservices.data.EWSHttpException: Connection not established". – Lugo Apr 26 '16 at 11:42