Request timed out when accessing EWS service

I am using the version aspose-email-18.5-jdk16.jar.
And I tried to connect to EWS server(Exchange Server 2013) using the codes like following.

IEWSClient client = EWSClient.getEWSClient( “https://mail.ricoh-gse.net/ews/exchange.asmx”, "slnxtest1@ricoh-gse.net", “xxxxxxxx”, “” );

However I got the following error messages after a while.

class com.aspose.email.system.exceptions.WebException: The request timed out
com.aspose.email.internal.m.ab.x(Unknown Source)
com.aspose.email.ra.a(SourceFile:254)
com.aspose.email.ra.a(SourceFile:351)
com.aspose.email.EWSClient.getEWSClient(SourceFile:313)
com.aspose.email.EWSClient.getEWSClient(SourceFile:123)
com.ricoh.mdm.mie.print.mail.ExchangeEwsTests.testEwsConnection(ExchangeEwsTests.java:31)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

Do I need to configure any additional settings except using the Aspose library or do I need to add any more extra codes?

I tested the EWS server using the site ‘[https://testconnectivity.microsoft.com/ ](https://testconnectivity.microsoft.com/)’ and it looks fine.
image.png (36.5 KB)

1 Like

@tedkong,

I have observed the issue shared by you and request you to please try using latest Aspose.Email for Java 20.4 on your end. If there is still an issue then please share the used sample code and test account credentials with us reproducing the issue.

@mudassir.fayyaz

I tested with the version 20.4 but it still shows the same issue.
You can use the following test codes and the credential information is there.

public void testEwsConnection() {

	try {
		IEWSClient client = EWSClient.getEWSClient( "https://mail.ricoh-gse.net/ews/exchange.asmx", "slnxtest1@ricoh-gse.net", "5]Lb8BEq>kPW+A" );

		System.out.println( "client - " + client.toString() );

	} catch ( Exception e ) {
		System.out.println( "Exception message - " + e.getMessage() );
	}
}

@tedkong,

I have worked over the issue shared by you and have been able to reproduce the issue. A ticket with ID EMAILJAVA-34699 has been created in our issue tracking system to further investigate and resolve the issue. This thread has been linked with the issue so that you may be notified once the issue will be fixed.

I found the status was changed to resolved. Then with which version can I verify the fix?

@tedkong,

We actually have no access to your account. We can suggest using new EWS core implementation using Aspose.Email for Java 20.7:

EWSClient.useSAAJAPI(true);
IEWSClient client = EWSClient.getEWSClient( "https://mail.ricoh-gse.net/ews/exchange...

Why do I need to use .NET version instead of Java version?

@tedkong,

You can use the Java version and sample code provided is for Java too. We are sorry for your inconvenience.

@mudassir.fayyaz

I retested with latest version 20.7(aspose-email-20.7-java) but still could reproduce the issue.

(1) Enabled the SAAJ API and got the following error.
Aug 18, 2020 1:22:36 PM com.sun.xml.internal.messaging.saaj.client.p2p.HttpSOAPConnection post
SEVERE: SAAJ0008: Bad Response; Unauthorized
Exception message - com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: com.sun.xml.internal.messaging.saaj.SOAPExceptionImpl: Bad response: (401Unauthorized

(2) Not enabled the SAAJ API and got the following error.
java.lang.RuntimeException: class com.aspose.email.system.exceptions.WebException: The request timed out
com.aspose.email.internal.o.zab.x(Unknown Source)
com.aspose.email.zahw.a(SourceFile:86)
com.aspose.email.zsq.a(SourceFile:34)
com.aspose.email.zg.a(SourceFile:251)
com.aspose.email.zg.a(SourceFile:398)
com.aspose.email.EWSClient.a(SourceFile:411)
com.aspose.email.EWSClient.getEWSClient(SourceFile:352)
com.aspose.email.EWSClient.getEWSClient(SourceFile:337)
com.aspose.email.EWSClient.getEWSClient(SourceFile:153)
com.ricoh.mdm.mie.print.mail.ExchangeEwsTests.testEwsConnection(ExchangeEwsTests.java:32)

(3) You can use the following information to check the issue by yourself.

IEWSClient client = EWSClient.getEWSClient( "https://mail.ricoh-gse.com/EWS/Exchange.asmx", "slxntest001@ricoh-gse.com", "6Q=A9c~PYmKPjN*5!\"d)!L:{6e?>" );

I hope to hear the resolution ASAP because our product is being delayed with this issue.

@tedkong

Please try using following code snippets with NTLM authentication:

NTLM authentication with SAAJ Authenticator for single user (JDK 1.8):

import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URL;

static Authenticator getAuthenticator(String user, String pw, String domain) {
    final String username = domain + "\\" + user;
    final String password = pw;
    System.out.println("New Credentials " + username);
    return new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            System.out.println("Authenticate " + username);
            return new PasswordAuthentication(username, password.toCharArray());
        }
    };
}

@Test
public void test() throws MalformedURLException {
    EWSClient.useSAAJAPI(true);
    System.setProperty("http.auth.preference", "NTLM");
    Authenticator.setDefault(getAuthenticator("slxntest001@ricoh-gse.com", "6Q=A9c~PYmKPjN*5!\"d)!L:{6e?>", ""));
    IEWSClient client = EWSClient.getEWSClient(new URL("https://mail.ricoh-gse.com/EWS/Exchange.asmx"));
}

NTLM authentication with SAAJ Authenticator for multiple users (JDK 1.8):

static Authenticator getAuthenticator() {

    // This block is written for suppressing a bug in sun implementation.
    // In Sun Impl client doesn't authenticate user for each connection,
    // uses cached credentials instead.
    sun.net.www.protocol.http.AuthCacheValue.setAuthCache(new sun.net.www.protocol.http.AuthCache() {
        public void remove(String pkey, sun.net.www.protocol.http.AuthCacheValue entry) {
        }
        public void put(String pkey, sun.net.www.protocol.http.AuthCacheValue value) {
        }
        public sun.net.www.protocol.http.AuthCacheValue get(String pkey, String skey) {
            return null;
        }
    });

    return new Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return getEmbeddedCredentials(getRequestingURL());
        }

        PasswordAuthentication getEmbeddedCredentials(URL url) {
            if (url == null) {
                return null;
            }
            String userInfo = url.getUserInfo();
            int colon = userInfo == null ? -1 : userInfo.indexOf(":");
            if (colon == -1) {
                return null;
            } else {
                String userName = rawURLDecode(userInfo.substring(0, colon));
                String pass = rawURLDecode(userInfo.substring(colon + 1));
                System.out.println("Authenticate " + userInfo);
                return new PasswordAuthentication("\\" + userName, pass.toCharArray());
            }
        }
    };
}

static String rawURLEncode(String value) {
    try {
        return java.net.URLEncoder.encode(java.util.Base64.getEncoder().encodeToString(value.getBytes()), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        return value;
    }
}

static String rawURLDecode(String value) {
    try {
        return new String(java.util.Base64.getDecoder().decode(java.net.URLDecoder.decode(value, "UTF-8")), "UTF-8");
    } catch (UnsupportedEncodingException e) {
        return value;
    }
}

static URL getURL(String url, String user, String pw, String domain) throws Exception {
    String host = new URL(url).getHost();
    URL endpoint = new URL(url.replace(host, rawURLEncode(user) + ":" + rawURLEncode(pw) + "@" + host));

    return endpoint;
}

@Test
public void test() throws Exception {
    Authenticator.setDefault(getAuthenticator());

    EWSClient.useSAAJAPI(true);
    System.setProperty("http.auth.preference", "NTLM");
    IEWSClient client = EWSClient.getEWSClient(getURL("https://mail.ricoh-gse.com/EWS/Exchange.asmx", "slxntest001@ricoh-gse.com", "6Q=A9c~PYmKPjN*5!\"d)!L:{6e?>", ""));
}

Since Java 9, we can set Authenticator for connection:

static Map<String, Authenticator> authInfo = new HashMap<String, Authenticator>();
static URL getURL(String url, final String user, final String pw, final String domain) throws MalformedURLException {
    URL endpoint = new URL(new URL(url), "", new URLStreamHandler() {
        protected URLConnection openConnection(URL url) throws IOException {
            URL target = new URL(url.toString());
            HttpURLConnection connection = (HttpURLConnection) target.openConnection();
            // Cache for User@Url
            Authenticator auth = authInfo.get(user + "@" + url);
            if (auth == null) {
                auth = new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        System.out.println("Authenticate " + user);
                        return new PasswordAuthentication(domain + "\\" + user, pw.toCharArray());
                    }
                };
                authInfo.put(user + "@" + url, auth);
            }
            connection.setAuthenticator(auth);

            return connection;
        }
    });

    return endpoint;
}

@Test
public void test() throws Exception {
    EWSClient.useSAAJAPI(true);
    System.setProperty("http.auth.preference", "NTLM");
    IEWSClient client = EWSClient.getEWSClient(getURL("https://mail.ricoh-gse.com/EWS/Exchange.asmx", "slxntest001@ricoh-gse.com", "6Q=A9c~PYmKPjN*5!\"d)!L:{6e?>", ""));
}

Note:
The JAXB APIs are considered to be Java EE APIs, and therefore are no longer contained on the default class path in Java SE 9.
Maven JAXB dependencies:

<dependency>
    <groupId>javax.xml.bind</groupId>
    <artifactId>jaxb-api</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-core</artifactId>
    <version>2.3.0.1</version>
</dependency>
<dependency>
    <groupId>com.sun.xml.messaging.saaj</groupId>
    <artifactId>saaj-impl</artifactId>
    <version>1.5.0</version>
</dependency>