Java-клиент EWS не может подключиться. Автообнаружение не удается. Эквивалентный клиент в c # работает - PullRequest
0 голосов
/ 11 октября 2019

Я пытаюсь получать электронные письма с локального сервера EWS 2016, и поскольку нет доступных хороших API REST, я прибегнул к управляемому API EWS. Я хотел бы использовать Java по нескольким причинам (в основном, проще для запуска в Linux Linux), поэтому библиотека Java - мой лучший вариант. Я создал следующий фрагмент кода для тестирования:

    this.service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
    service.setCredentials(new WebCredentials("<email>", "<pw>"));
    service.setTraceEnabled(true);
    service.setTraceFlags( EnumSet.allOf( TraceFlags.class ));

    service.autodiscoverUrl("<email>", new RedirectionUrlCallback());

    EmailMessage email = new EmailMessage(service);
    email.getToRecipients().add("recipientadress@somedomain.com");
    email.setSubject("HelloWorld");
    email.setBody(new MessageBody("This is the first email I've sent by using the EWS Managed API"));
    email.send();

со следующим подтверждением перенаправления:

private static class RedirectionUrlCallback implements IAutodiscoverRedirectionUrl {
    public boolean autodiscoverRedirectionUrlValidationCallback(
            String redirectionUrl) {
        // The default for the validation callback is to reject the URL.
        boolean result = false;
        Uri redirectionUri;

        try{
            redirectionUri = new Uri(redirectionUrl);
        }catch (MalformedURLException e) {
            e.printStackTrace();
            return result;
        }

        // Validate the contents of the redirection URL. In this simple validation
        // callback, the redirection URL is considered valid if it is using HTTPS
        // to encrypt the authentication credentials.
        if (redirectionUri.getScheme().equals("https"))
        {
            result = true;
        }

        return result;
    }

Я получаю следующую ошибку:

The Autodiscover service couldn't be located.

Я также написал следующий эквивалентный код c #:

        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
        service.Credentials = new WebCredentials("<email>", "<pw>");
        service.TraceEnabled = true;
        service.TraceFlags = TraceFlags.All;
        service.AutodiscoverUrl("<email>", RedirectionUrlValidationCallback);
        EmailMessage email = new EmailMessage(service);
        email.ToRecipients.Add("recipientadress@somedomain.com");
        email.Subject = "HelloWorld";
        email.Body = new MessageBody("This is the first email I've sent by using the EWS Managed API");
        email.Send();

со следующим подтверждением перенаправления:

    private static bool RedirectionUrlValidationCallback(string redirectionUrl)
    {
        // The default for the validation callback is to reject the URL.
        bool result = false;
        Uri redirectionUri = new Uri(redirectionUrl);
        // Validate the contents of the redirection URL. In this simple validation
        // callback, the redirection URL is considered valid if it is using HTTPS
        // to encrypt the authentication credentials. 
        if (redirectionUri.Scheme == "https")
        {
            result = true;
        }

        return result;
    }

Конечно, используя тот же адрес электронной почты и пароль. Последний отлично работает . Но это C #, я хотел бы использовать Java.

Почему?!? Почему?!? Почему точный эквивалент в Java терпит неудачу? Я делаю что-то неправильно? Или это еще Microsoft снова ** меня больше?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...