EWS-Java-API - EmailMessage.send 变成 java.lang.NullPointerException: 格式 == null

EWS-Java-API - EmailMessage.send into java.lang.NullPointerException: format == null

我想为我的 android 应用程序使用 EWS-Java-API。 该应用程序需要登录才能以编程方式访问 Microsoft Exchange 服务器(例如日历会议、电子邮件、会议室等)。

此时几乎整个问题都已编辑,以防万一显示到目前为止的情况。

我尝试使用 url ("https ://yourserver/EWS/Exchange.asmx")

连接到交换版本 15.0.11

构建gradle文件:

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' }

这将导致此错误消息

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

行:

ExchangeService service = new ExchangeService();      

此时我测试了这个 ews 库 "github.com/faisons/EWS-For-Android"。

 compile files('libs/ews.jar')

这几乎可以正常工作了。我能够启动应用程序并设置 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();

现在的错误是:

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)

我很困惑如何让 api 工作。如果有人知道另一种访问方式,欢迎任何提示!

您是否将 apache 遗留行添加到您的 gradle 构建中?

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

有关详细信息,请参阅此内容:http://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-apache-http-client

您好!我有个好消息,它有效!我不确定这是否是完美的方式,但我可以发送电子邮件

首先确保您拥有清单文件中声明的权限:

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

这是我的 Build.gradle 文件:

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'}           // <---------------------

你可以在这个link下得到ews-android-api: https://github.com/alipov/ews-android-api

只需按照构建说明创建 jar 文件即可。

这是发送电子邮件的基本命令行(运行这是异步任务或在新线程中):

                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();

您可以使用 EWSEditor 检查您的凭据。 向 DevilGeek 和 Alipov 致敬。

如有不明之处欢迎随时提问。

亲切的问候!

编辑: 它似乎适用于几乎所有东西。您不需要特别需要 Office 365。例如,我也可以调用 "service.getRoomLists();"。