diff --git a/src/java/davmail/exchange/ExchangeSessionFactory.java b/src/java/davmail/exchange/ExchangeSessionFactory.java index 18e4358c..6f795bd9 100644 --- a/src/java/davmail/exchange/ExchangeSessionFactory.java +++ b/src/java/davmail/exchange/ExchangeSessionFactory.java @@ -124,6 +124,40 @@ public final class ExchangeSessionFactory { return session; } + /** + * Get a non expired session. + * If the current session is not expired, return current session, else try to create a new session + * + * @param currentSession current session + * @param userName user login + * @param password user password + * @return authenticated session + * @throws IOException on error + */ + public static ExchangeSession getInstance(ExchangeSession currentSession, String userName, String password) + throws IOException { + ExchangeSession session = currentSession; + try { + if (session.isExpired()) { + session = null; + String baseUrl = Settings.getProperty("davmail.url"); + PoolKey poolKey = new PoolKey(baseUrl, userName, password); + // expired session, remove from cache + synchronized (LOCK) { + POOL_MAP.remove(poolKey); + } + session = getInstance(userName, password); + } + } catch (DavMailAuthenticationException exc) { + throw exc; + } catch (DavMailException exc) { + throw exc; + } catch (Exception exc) { + handleNetworkDown(exc); + } + return session; + } + /** * Send a request to Exchange server to check current settings. * diff --git a/src/java/davmail/imap/ImapConnection.java b/src/java/davmail/imap/ImapConnection.java index c6f5163d..85960f44 100644 --- a/src/java/davmail/imap/ImapConnection.java +++ b/src/java/davmail/imap/ImapConnection.java @@ -126,6 +126,8 @@ public class ImapConnection extends AbstractConnection { if (state != State.AUTHENTICATED) { sendClient(commandId + " BAD command authentication required"); } else { + // check for expired session + session = ExchangeSessionFactory.getInstance(session, userName, password); if ("lsub".equalsIgnoreCase(command) || "list".equalsIgnoreCase(command)) { if (tokens.hasMoreTokens()) { String folderContext = BASE64MailboxDecoder.decode(tokens.nextToken());