diff --git a/app/src/main/java/eu/faircode/email/ActivityEML.java b/app/src/main/java/eu/faircode/email/ActivityEML.java index 9cb2a3fb6c..e12a09bbf9 100644 --- a/app/src/main/java/eu/faircode/email/ActivityEML.java +++ b/app/src/main/java/eu/faircode/email/ActivityEML.java @@ -241,7 +241,7 @@ public class ActivityEML extends ActivityBase { Session isession = Session.getInstance(props, null); MimeMessage imessage = new MimeMessage(isession, is); - try (MailService iservice = new MailService(context, account.getProtocol(), account.realm, account.insecure, true)) { + try (MailService iservice = new MailService(context, account.getProtocol(), account.realm, account.insecure, false, true)) { iservice.setPartialFetch(account.partial_fetch); iservice.setIgnoreBodyStructureSize(account.ignore_size); iservice.setSeparateStoreConnection(); diff --git a/app/src/main/java/eu/faircode/email/BoundaryCallbackMessages.java b/app/src/main/java/eu/faircode/email/BoundaryCallbackMessages.java index 2c55cff684..fed805ed22 100644 --- a/app/src/main/java/eu/faircode/email/BoundaryCallbackMessages.java +++ b/app/src/main/java/eu/faircode/email/BoundaryCallbackMessages.java @@ -252,7 +252,7 @@ public class BoundaryCallbackMessages extends PagedList.BoundaryCallback folders; String aprotocol = provider.imap.starttls ? "imap" : "imaps"; - try (MailService iservice = new MailService(context, aprotocol, null, false, true)) { + try (MailService iservice = new MailService(context, aprotocol, null, false, true, true)) { try { iservice.connect(provider.imap.host, provider.imap.port, MailService.AUTH_TYPE_PASSWORD, user, password); } catch (AuthenticationFailedException ex) { @@ -267,7 +267,7 @@ public class FragmentQuickSetup extends FragmentBase { } String iprotocol = provider.smtp.starttls ? "smtp" : "smtps"; - try (MailService iservice = new MailService(context, iprotocol, null, false, true)) { + try (MailService iservice = new MailService(context, iprotocol, null, false, true, true)) { iservice.connect(provider.smtp.host, provider.smtp.port, MailService.AUTH_TYPE_PASSWORD, user, password); } diff --git a/app/src/main/java/eu/faircode/email/MailService.java b/app/src/main/java/eu/faircode/email/MailService.java index 3b0787b76e..a57b1629fc 100644 --- a/app/src/main/java/eu/faircode/email/MailService.java +++ b/app/src/main/java/eu/faircode/email/MailService.java @@ -46,6 +46,7 @@ public class MailService implements AutoCloseable { static final int AUTH_TYPE_PASSWORD = 1; static final int AUTH_TYPE_GMAIL = 2; + private final static int CHECK_TIMEOUT = 15 * 1000; // milliseconds private final static int CONNECT_TIMEOUT = 20 * 1000; // milliseconds private final static int WRITE_TIMEOUT = 60 * 1000; // milliseconds private final static int READ_TIMEOUT = 60 * 1000; // milliseconds @@ -57,7 +58,7 @@ public class MailService implements AutoCloseable { private MailService() { } - MailService(Context context, String protocol, String realm, boolean insecure, boolean debug) throws NoSuchProviderException { + MailService(Context context, String protocol, String realm, boolean insecure, boolean check, boolean debug) throws NoSuchProviderException { this.context = context.getApplicationContext(); this.protocol = protocol; this.debug = debug; @@ -70,6 +71,11 @@ public class MailService implements AutoCloseable { properties.put("mail." + protocol + ".sasl.realm", realm == null ? "" : realm); properties.put("mail." + protocol + ".auth.ntlm.domain", realm == null ? "" : realm); + // TODO: make timeouts configurable? + properties.put("mail." + protocol + ".connectiontimeout", Integer.toString(check ? CHECK_TIMEOUT : CONNECT_TIMEOUT)); + properties.put("mail." + protocol + ".writetimeout", Integer.toString(check ? CHECK_TIMEOUT : WRITE_TIMEOUT)); // one thread overhead + properties.put("mail." + protocol + ".timeout", Integer.toString(check ? CHECK_TIMEOUT : READ_TIMEOUT)); + if (debug && BuildConfig.DEBUG) properties.put("mail.debug.auth", "true"); @@ -85,11 +91,6 @@ public class MailService implements AutoCloseable { properties.put("mail.pop3.starttls.enable", "true"); properties.put("mail.pop3.starttls.required", Boolean.toString(!insecure)); - // TODO: make timeouts configurable? - properties.put("mail." + protocol + ".connectiontimeout", Integer.toString(CONNECT_TIMEOUT)); - properties.put("mail." + protocol + ".writetimeout", Integer.toString(WRITE_TIMEOUT)); // one thread overhead - properties.put("mail." + protocol + ".timeout", Integer.toString(READ_TIMEOUT)); - } else if ("imap".equals(protocol) || "imaps".equals(protocol)) { // https://javaee.github.io/javamail/docs/api/com/sun/mail/imap/package-summary.html#properties properties.put("mail." + protocol + ".ssl.checkserveridentity", Boolean.toString(!insecure)); @@ -100,11 +101,6 @@ public class MailService implements AutoCloseable { properties.put("mail.imap.starttls.enable", "true"); properties.put("mail.imap.starttls.required", Boolean.toString(!insecure)); - // TODO: make timeouts configurable? - properties.put("mail." + protocol + ".connectiontimeout", Integer.toString(CONNECT_TIMEOUT)); - properties.put("mail." + protocol + ".writetimeout", Integer.toString(WRITE_TIMEOUT)); // one thread overhead - properties.put("mail." + protocol + ".timeout", Integer.toString(READ_TIMEOUT)); - properties.put("mail." + protocol + ".connectionpool.debug", "true"); properties.put("mail." + protocol + ".connectionpoolsize", "2"); properties.put("mail." + protocol + ".connectionpooltimeout", Integer.toString(POOL_TIMEOUT)); @@ -135,10 +131,6 @@ public class MailService implements AutoCloseable { properties.put("mail." + protocol + ".auth", "true"); - properties.put("mail." + protocol + ".connectiontimeout", Integer.toString(CONNECT_TIMEOUT)); - properties.put("mail." + protocol + ".writetimeout", Integer.toString(WRITE_TIMEOUT)); // one thread overhead - properties.put("mail." + protocol + ".timeout", Integer.toString(READ_TIMEOUT)); - } else throw new NoSuchProviderException(protocol); } diff --git a/app/src/main/java/eu/faircode/email/ServiceSend.java b/app/src/main/java/eu/faircode/email/ServiceSend.java index 0a1e56208e..2a8bb63414 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSend.java +++ b/app/src/main/java/eu/faircode/email/ServiceSend.java @@ -388,7 +388,7 @@ public class ServiceSend extends ServiceBase { // Create transport try (MailService iservice = new MailService( - this, ident.getProtocol(), ident.realm, ident.insecure, debug)) { + this, ident.getProtocol(), ident.realm, ident.insecure, false, debug)) { iservice.setUseIp(ident.use_ip); // Connect transport diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 8d265f3601..e3f95b7af0 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -720,7 +720,7 @@ public class ServiceSynchronize extends ServiceBase { boolean debug = (prefs.getBoolean("debug", false) || BuildConfig.DEBUG); final MailService iservice = new MailService( - this, account.getProtocol(), account.realm, account.insecure, debug); + this, account.getProtocol(), account.realm, account.insecure, false, debug); iservice.setPartialFetch(account.partial_fetch); iservice.setIgnoreBodyStructureSize(account.ignore_size); if (account.pop)