使用Core JAVA中的ews-java-api-2.0.jar连接到Office365 Exchange Server [英] Connect to Office365 Exchange Server with ews-java-api-2.0.jar in Core JAVA

查看:1149
本文介绍了使用Core JAVA中的ews-java-api-2.0.jar连接到Office365 Exchange Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是ews-java-api-2.0.jar,连接到office365,下面是示例代码:

I am using ews-java-api-2.0.jar, to connect to office365 and below is the sample code:

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运行此代码,得到以下错误:

When i run this code from Netbeans IDE , getting below error:

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天开始,任何帮助都将受到赞赏!!

I am stuck here since 2 days, any help would be appreciated!!

推荐答案

您好,请添加以下jar文件

Hi add following jar files

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浏览器中打开网址 -

to resolve all dependencies .You need to open the URL in chrome browser -

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

然后输入您的身份验证凭据的用户名和密码将在以下代码中使用。

Then enter UserName and Password of your authenticating credentials which you are going to use in your below code.

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("user@domain.com", "xxxxxx");

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

它在我的工作得很好以Office365结束。

It works pretty well at my end with Office365.

这篇关于使用Core JAVA中的ews-java-api-2.0.jar连接到Office365 Exchange Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆