Search code examples
javajakarta-mailexchange-serverimap

JavaMail Connect over imaps to an e-mail store - extend the timeout


I'm trying to extend the timeout for connecting to an e-mail store (Microsoft Exchange server) but not having success. The reason being, on my local machine connecting to the store is almost instantaneous but on the machine I'm deploying the code to it can take up to a couple minutes. Rather than continually trying to connect over and over I'd rather just have a really long timeout. How to do this? Here is my code (trying to set the timeout to 2 minutes):

        Properties properties = new Properties();
        properties.put("mail.store.protocol", "imaps");
        properties.put("mail.imaps.host", IESProperties.STORE_HOST);
        properties.put("mail.imaps.port", "993");
        properties.put("mail.imaps.connectiontimeout", "120000"); // 2 minutes
        properties.put("mail.imaps.timeout", "120000"); // 2 minutes

        Session session = Session.getInstance(properties);
        IMAPStore store = null;

        try {
            store = (IMAPStore) session.getStore("imaps");
            try {
                store.connect(IESProperties.STORE_USERNAME, IESProperties.STORE_PASSWORD);

When I run this it seems to give up after only about 10 seconds or something like that.

jvm 1    | javax.mail.MessagingException: Connection timed out: connect;
jvm 1    |   nested exception is:
jvm 1    |      java.net.ConnectException: Connection timed out: connect
jvm 1    |      at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:66
5)
jvm 1    |      at javax.mail.Service.connect(Service.java:295)
jvm 1    |      at javax.mail.Service.connect(Service.java:176)
jvm 1    |      at javax.mail.Service.connect(Service.java:196)
jvm 1    |      at com.axa.ets.ies.Main.start(Main.java:297)
jvm 1    |      at com.axa.ets.ies.Main.main(Main.java:232)
jvm 1    |      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
jvm 1    |      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
jvm 1    |      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Sourc
e)
jvm 1    |      at java.lang.reflect.Method.invoke(Unknown Source)
jvm 1    |      at org.tanukisoftware.wrapper.WrapperSimpleApp.run(WrapperSimple
App.java:325)
jvm 1    |      at java.lang.Thread.run(Unknown Source)

When I telnet to the store it takes a really long time, but usually connects in about a minute.

How to extend this timeout using the API? Thanks


Solution

  • I´m not sure if you can control that via your code. However it might be possible to control that via server (see here -> connection time-out limits for IMAP4):

    This example sets the connection time-out limit for idle authenticated connections.

    Set-ImapSettings -Identity CAS01 -AuthenticatedConnectionTimeout TimeValue
    

    This example sets the connection time-out limit for idle unauthenticated connections.

    Set-ImapSettings -Identity CAS01 -PreAuthenticatedConnectionTimeout TimeValue