JavaMail 通过 imaps 连接到电子邮件存储 - 延长超时时间

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

我正在尝试延长连接到电子邮件存储(Microsoft Exchange 服务器)的超时时间,但没有成功。原因是,在我的本地机器上连接到商店几乎是瞬时的,但在我正在部署代码的机器上可能需要几分钟。与其不断尝试一遍又一遍地连接,我宁愿有一个非常长的超时时间。这该怎么做?这是我的代码(尝试将超时设置为 2 分钟):

        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);

当我 运行 这似乎只在大约 10 秒或类似的时候就放弃了。

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)

当我 telnet 到商店时需要很长时间,但通常在大约一分钟内连接。

如何使用 API 延长超时时间?谢谢

我不确定您是否可以通过代码控制它。但是,可以通过服务器进行控制(参见 here -> IMAP4 的连接超时限制):

此示例为经过身份验证的空闲连接设置连接超时限制。

Set-ImapSettings -Identity CAS01 -AuthenticatedConnectionTimeout TimeValue

此示例设置空闲未验证连接的连接超时限制。

Set-ImapSettings -Identity CAS01 -PreAuthenticatedConnectionTimeout TimeValue