ImapClient.selectFolder Method Reaches Timeout

Hi,
We are using aspose-email-17.3.0-jdk16.jar since long time. We got a new error never happened before:
On ImapClient.SelectFolder(ImapFolderInfo.IN_BOX) we have a timeout exception (Connection failure. Timeout ‘200000’ has been reached).

The program runs every 15 minutes. Can you please help.
Here is my code:

private boolean HandleEmailsAspose(IDfSession session, StoreInDocumentum sDctm, Properties prop,
String user, String password, String emailACL) {
ImapClient client = null;
try
{
client = new ImapClient();
client.setHost(emailHost);
client.setPort(Integer.parseInt(emailPort));
client.setUsername(user);
client.setPassword(password);
client.setSecurityOptions(SecurityOptions.Auto);
client.selectFolder(ImapFolderInfo.IN_BOX);

		client.setTimeout(900000); // in ms
		ImapMessageInfoCollection coll = client.listMessages();
        try 
        {
        	for(int pt = 0; pt < 30; pt++)
       		{
       			coll = client.listMessages();
    			
    			if(coll.size() == 0)
    				break;
    			ImapMessageInfo info = coll.get_Item(0);
    		
        		MailMessage eml = null;
        		try {
        			eml = client.fetchMessage(info.getUniqueId());
        		}
        		catch(ImapException imap) { //EK: 2021-09-29: added to handle move exception
        			....
            		break;
        		}
            	
            	if(eml != null)
            	{
            		-- do processing here --
            	}
            }
            
            return true;
        }
        catch (Exception e) {
        	return false;
		}
    }
	catch(Exception e) {
		return false;
	}
	finally {
		if(client != null)
			client.dispose();
	}
}

@elie.kach,
Thank you for contacting support. Unfortunately, I was unable to reproduce the exception on my side. Please try to set the timeout before calling the selectFolder method as shown below:

client.setTimeout(900000);
client.selectFolder(ImapFolderInfo.IN_BOX);

More examples: Working with Folders on IMAP Server
API Reference: ImapClient Class

There were many updates after version 17.3.0. Please check your results by using the latest version of Aspose.Email.

If the issue persists, please specify the following:

  • email host or server provider
  • OS version where the exception occurs

Hi,

I put setTiemout before selectFolder but timeout no effect because in the exception we still see 200000.
I will try again again with latest version.
Please note that this issue is random, it occurs time to time, even if no messages in the inbox
email host: pod51009.outlook.com
OS version: Windows 10 professional and Windows server 2012 R2

HI,
Please note that at the same time when timeout occur if I run following code it works:

Store store = mailSession.getStore(“imaps”);
store.connect(emailHost, user, password);
Folder dFolder = store.getDefaultFolder();
Folder inbox = dFolder.getFolder(“INBOX”);
inbox.open(Folder.READ_WRITE);
Message[] msg = inbox.getMessages();

@elie.kach,
Thank you for more details.

It is strange behavior because the timeout applies to selectFolder method as well.

It may indicate a server-specific case.

We will be waiting for your results.

Hi,

I was able to user version 18.9 and I confirm that setting timeout (900000) before SelectFolder has no effect (Connection failure. Timeout ‘200000’ has been reached)

I cannot use last version (21.9) because of my licence, is it possible to get a temporary licence for one or two weeks to perform more tests?

Regards,

@elie.kach,

You should post your request for a temporary license to Aspose.Purchase forum or try to use evaluation version for tests.

Hi

We purchased a new licence and we are now using version 21.9.
I do not see Timeout but a new Exception: “Unable to connect to the server”

This error/Exception occur on SelectFolder call

Can you please help?

@elie.kach

I noticed that you use very old outlook settings. All Office 365 accounts email accounts have been upgraded. Recommended changes from MS:

IMAP connections:

  • Need to change pod51009.outlook.com to outlook.office365.com for incoming server
  • Need to change pod51009.outlook.com to smtp-mail.office365.com for outgoing SMTP server

Could you please try outlook.office365.com instead of pod51009.outlook.com. I checked both servers and they both work in my case, but the second one is much much slower. Maybe that’s the reason.

See POP, IMAP, and SMTP settings for Outlook.com - Microsoft Support for details.

Hi

I change my settings but still get same error.
Any advise?

@elie.kach I have registered the EMAILJAVA-34960 task in the Aspose.Email for Java tracker for further investigation. Our dev team will try to reproduce the problem and find a solution. You will be notified when the issue is resolved.

@elie.kach

Could you please add System.setProperty("javax.net.debug","all"); call before client.selectFolder(ImapFolderInfo.IN_BOX); and send us all logs from the console.

And please clarify: is the “Unable to connect to the server” error persistent when using v21.9 or it is a random one and occurs from time to time?

Hi,

I put javax.net.debug before but still getting same error “Unable connect to server”. I did not see more logs

Should I see more debug traces?

Hi,

More info: Aspose java email is running within a frame and not as a standalone java application. So from within this framwork I dont see traces but as a standalone application I can see more traces.

Hi

I run the program as a standalone java application on the same server where the issue appears. I got traces and see the error.
I think issue is related to that machine because don’t occur on another machine.java-debug.zip (3.6 KB)debug.net.zip (7.8 KB)

I add a second log file which I think more consistent.logs.zip (7.7 KB)

@elie.kach

Perhaps there is a missing certificate in the Java keystore on the machine. You can try the following code to temporary disable certificates validation and check:

import java.security.cert.CertificateException;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public static SSLSocketFactory getSSLSocketFactory() {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
        }

        public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
        }

        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return new java.security.cert.X509Certificate[] {};
        }
    } };

    try {
        SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
        sslContext.init(null, trustAllCerts, new java.security.SecureRandom());

        return sslContext.getSocketFactory();
    } catch (java.lang.Exception e) {
        throw new RuntimeException(e);
    }
}

public final void test()
{
    System.setProperty("javax.net.debug","all");
    EmailClient.setSocketsLayerVersion2SSLSocketFactory(getSSLSocketFactory());
   
	// existing code
	client = new ImapClient();
	client.setHost(emailHost);
	client.setPort(Integer.parseInt(emailPort));
	client.setUsername(user);
	client.setPassword(password);
    // ...
}

Thanks, It works. Is there any security issue if we use this workaround?
Can you tell me what kind of certificate is missing in order to add it to the keystore?

@elie.kach

Thanks, It works.

Great, looks like we found the problem.

Is there any security issue if we use this workaround?

Yes, there are! And I cannot recommend skipping the certificate check for production.

Can you tell me what kind of certificate is missing in order to add it to the keystore?

Unfortunately not, you have to analyze it yourself (or use a keystore from another machine without this error).

Also, please note: the code example above explicitly sets TLSv1.2 protocol , whereas old Java versions use TLSv1 by default. You can configure the JVM for which version of TLS to use:

-Dhttps.protocols=TLSv1.2,TLSv1.1

Or do this manually for Aspose.Email connections. The code above should work, just remove the sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); call.

Thank,

One more question,
When using following code I do not have connection issue, why, can you please explain:
props.setProperty(“mail.store.protocol”, emailStore);
props.setProperty(“mail.imap.host”, emailHost);
props.setProperty(“mail.imap.port”, port);
props.setProperty(“mail.imap.socketFactory.class”, Constants.SSL_FACTORY);
props.setProperty(“mail.imap.socketFactory.fallback”, “false”);
props.setProperty(“mail.imap.socketFactory.port”, port);
props.setProperty(“mail.imap.user”, user);
Session mailSession = Session.getInstance(props);
mailSession.setDebug(true);
Store store = mailSession.getStore(emailStore);
System.out.println("store.getURLName() = " + store.getURLName());
store.connect(emailHost, user, password);
Folder dFolder = store.getDefaultFolder();
Folder inbox = dFolder.getFolder(“INBOX”); // Import-Workflow-Error
inbox.open(Folder.READ_WRITE);
Message[] msg = inbox.getMessages();
for (int i = 0; i < msg.length; i++) {
Message msgn = msg[i];
Address[] add = msgn.getFrom();

	}
		
inbox.close(true);
    store.close();

@elie.kach

We need more information to answer the question:

  • factory implementation (Constants.SSL_FACTORY class implementation)
    props.setProperty("mail.imap.socketFactory.class", Constants.SSL_FACTORY);
    
  • java version