我使用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天后,任何幫助,將不勝感激!
錯誤非常明顯:ClassNotFoundException:org.apache.http.config.Lookup – Matt
在引用下面的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 –
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(未知來源) –