1
0
Fork 0
mirror of https://github.com/M66B/FairEmail.git synced 2024-12-24 00:36:28 +00:00

Workaround servers with non working STARTTLS capability

This commit is contained in:
M66B 2020-08-22 20:41:24 +02:00
parent 49b45ac612
commit 02c92d070c

View file

@ -510,7 +510,18 @@ public class EmailService implements AutoCloseable {
iservice = isession.getStore(protocol); iservice = isession.getStore(protocol);
if (listener != null) if (listener != null)
((IMAPStore) iservice).addStoreListener(listener); ((IMAPStore) iservice).addStoreListener(listener);
iservice.connect(address.getHostAddress(), port, user, password); try {
iservice.connect(address.getHostAddress(), port, user, password);
} catch (MessagingException ex) {
if (startTlsFailure(ex)) {
// Workaround servers with non working STARTTLS capability
Log.i("Disabling STARTTLS");
properties.put("mail.imap.starttls.enable", "false");
iservice = isession.getStore(protocol);
iservice.connect(address.getHostAddress(), port, user, password);
} else
throw ex;
}
// https://www.ietf.org/rfc/rfc2971.txt // https://www.ietf.org/rfc/rfc2971.txt
IMAPStore istore = (IMAPStore) getStore(); IMAPStore istore = (IMAPStore) getStore();
@ -551,7 +562,13 @@ public class EmailService implements AutoCloseable {
try { try {
iservice.connect(address.getHostAddress(), port, user, password); iservice.connect(address.getHostAddress(), port, user, password);
} catch (MessagingException ex) { } catch (MessagingException ex) {
if (ehlo == null && ConnectionHelper.isSyntacticallyInvalid(ex)) { if (startTlsFailure(ex)) {
// Workaround servers with non working STARTTLS capability
Log.i("Disabling STARTTLS");
properties.put("mail.smtp.starttls.enable", "false");
iservice = isession.getStore(protocol);
iservice.connect(address.getHostAddress(), port, user, password);
} else if (ehlo == null && ConnectionHelper.isSyntacticallyInvalid(ex)) {
properties.put("mail." + protocol + ".localhost", useip ? hdomain : haddr); properties.put("mail." + protocol + ".localhost", useip ? hdomain : haddr);
Log.i("Fallback localhost=" + properties.getProperty("mail." + protocol + ".localhost")); Log.i("Fallback localhost=" + properties.getProperty("mail." + protocol + ".localhost"));
try { try {
@ -568,6 +585,20 @@ public class EmailService implements AutoCloseable {
throw new NoSuchProviderException(protocol); throw new NoSuchProviderException(protocol);
} }
private boolean startTlsFailure(MessagingException ex) {
if (!"STARTTLS failure".equals(ex.getMessage()))
return false;
Throwable cause = ex.getCause();
while (cause != null && cause.getCause() != null)
cause = cause.getCause();
if (cause == null)
return false;
return "Unable to parse TLS packet header".equals(cause.getMessage());
}
private static class ErrorHolder { private static class ErrorHolder {
AuthorizationException error; AuthorizationException error;
} }