2016-01-27 79 views
-2

我使用EWS-Java的API-2.0.jar,連接到office365以下是示例代碼:連接到Office365 Exchange服務器與EWS-Java的API-2.0.jar在Java核心

package javaapplication6; 

import java.net.URI; 
import microsoft.exchange.webservices.data.autodiscover.IAutodiscoverRedirectionUrl; 

import microsoft.exchange.webservices.data.credential.ExchangeCredentials; 
import microsoft.exchange.webservices.data.core.ExchangeService; 
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName; 
import microsoft.exchange.webservices.data.core.service.folder.Folder; 
import microsoft.exchange.webservices.data.credential.WebCredentials; 

public class JavaApplication6 { 

    public static class RedirectionUrlCallback implements IAutodiscoverRedirectionUrl { 
     public boolean autodiscoverRedirectionUrlValidationCallback(String redirectionUrl) { 
      return redirectionUrl.toLowerCase().startsWith("https://"); 
     } 
    } 

    public static ExchangeService connectViaExchangeManually(String email, String password) 
     throws Exception { 
     ExchangeService service = new ExchangeService(); 
     ExchangeCredentials credentials = new WebCredentials(email, password); 
     service.setUrl(new URI("https://outlook.office365.com/EWS/Exchange.asmx")); 
     service.setCredentials(credentials); 
     service.setTraceEnabled(true); 
     Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox); 
     System.out.println("messages: " + inbox.getTotalCount()); 
     return service; 
    } 

    public static ExchangeService connectViaExchangeAutodiscover(String email, String password) { 
     ExchangeService service = new ExchangeService(); 
     try { 
      service.setCredentials(new WebCredentials(email, password)); 
      service.autodiscoverUrl(email, new RedirectionUrlCallback()); 
      service.setTraceEnabled(true); 
      Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox); 
      System.out.println("messages: " + inbox.getTotalCount()); 
     } 
     catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return service; 
    } 
    public static void main(String[] args) { 
     try { 
     ExchangeService service = connectViaExchangeManually("<name>@<company>.onmicrosoft.com", "<password>"); 
     } catch (Exception e) { 
     e.printStackTrace(); 
     } 
    } 

} 

當我運行從NetBeans IDE中的代碼,獲得以下錯誤:

run: 
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/config/Lookup 
    at javaapplication6.JavaApplication6.connectViaExchangeAutodiscover(JavaApplication6.java:33) 
    at javaapplication6.JavaApplication6.main(JavaApplication6.java:48) 
Caused by: java.lang.ClassNotFoundException: org.apache.http.config.Lookup 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372) 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) 
    ... 2 more 
C:\Users\Brijesh Jalan\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1 
BUILD FAILED (total time: 0 seconds) 

我在這裏停留,因爲2天后,任何幫助,將不勝感激!

+0

錯誤非常明顯:ClassNotFoundException:org.apache.http.config.Lookup – Matt

+0

在引用下面的JAR之後,它得到了解決。 EWSJavaAPI_1.2original.jar,EWSJavaAPIWithJars_1.2.1.jar,httpclient-4.2.5.jar,httpcore-4.2.4.jar,jcifs-1.3.17.jar,commons-codec-1.7.jar,commons-logging-1.1 .jar –

+2

2016年1月27日8:23:03 org.apache.commons.httpclient.auth.AuthChallengeProcessor selectAuthScheme 信息:選擇基本身份驗證方案 2016年1月27日8:23:09 PM org.apache.commons .httpclient.HttpMethodDirector processWWWAuthChallenge 信息:驗證失敗與BASIC '' @ outlook.office365.com:443 microsoft.exchange.webservices.data.EWSHttpException:連接未建立 \t在microsoft.exchange.webservices.data.HttpClientWebRequest.throwIfConnIsNull(未知來源) \t at microsoft.exchange.webservices.data.HttpClientWebRequest.getResponseCo de(未知來源) –

回答

3

喜添加以下jar文件

EWSJavaAPI_1.2original.jar 
EWSJavaAPIWithJars_1.2.1.jar 
httpclient-4.2.5.jar 
httpcore-4.2.4.jar 
jcifs-1.3.17.jar 
commons-codec-1.7.jar 
commons-logging-1.1.1.jar 

來解決。你需要在Chrome瀏覽器中打開URL所有的依賴 -

https://outlook.office365.com/EWS/Exchange.asmx

然後輸入您的身份驗證憑據的用戶名和密碼,你將在你的下面的代碼中使用。

package EWSJavaAPI; 

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.Folder; 
import microsoft.exchange.webservices.data.IAutodiscoverRedirectionUrl; 
import microsoft.exchange.webservices.data.WebCredentials; 
import microsoft.exchange.webservices.data.WellKnownFolderName; 

public class EWSJavaAPI { 

    public static class RedirectionUrlCallback implements IAutodiscoverRedirectionUrl { 
     public boolean autodiscoverRedirectionUrlValidationCallback(String redirectionUrl) { 
      return redirectionUrl.toLowerCase().startsWith("https://"); 
     } 
    } 

    public static ExchangeService connectViaExchangeAutodiscover(String email, String password) { 
     ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2); 
     try { 

      service.setCredentials(new WebCredentials(email, password)); 
      service.autodiscoverUrl(email, new RedirectionUrlCallback()); 
      service.setTraceEnabled(true); 
      Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox); 
      System.out.println("messages: " + inbox.getTotalCount()); 
     } 
     catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return service; 
    } 
    public static void main(String[] args) { 
     try { 
      System.out.println("Hello World"); 
      ExchangeService service = connectViaExchangeAutodiscover("[email protected]", "xxxxxx"); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 
} 

它在Office365的結尾工作得很好。

+1

我能夠連接到我的Office 365交換機並能夠綁定收件箱。感謝! –

+0

嗨我試過上面的代碼與Office 365服務器通信,但我得到「(401)未經授權」。當我們使用webapp訪問帳戶時,相同的憑據正在工作。當發生此錯誤時,請讓我們知道嗎? –

+0

嘗試使用手動發現並讓我知道,如果您獲得成功:public static void connectViaExchangeManually(String email,String password){嘗試ExchangeCredentials credentials = new WebCredentials(email,password); service.setUrl(new URI(「https://outlook.office365.com/EWS/Exchange.asmx」)); service.setCredentials(credentials); EWSResponse =「SUCCESS」; } catch(Exception ex){ ex.printStackTrace(); EWSResponse =「FAILURE:自動發現服務找不到」; } } –

相關問題