diff --git a/app/src/main/java/eu/faircode/email/FragmentAccount.java b/app/src/main/java/eu/faircode/email/FragmentAccount.java index 1e6bd915..ec80d673 100644 --- a/app/src/main/java/eu/faircode/email/FragmentAccount.java +++ b/app/src/main/java/eu/faircode/email/FragmentAccount.java @@ -75,6 +75,7 @@ import java.util.List; import java.util.Locale; import java.util.Properties; +import javax.mail.AuthenticationFailedException; import javax.mail.Folder; import javax.mail.MessagingException; import javax.mail.Session; @@ -472,7 +473,15 @@ public class FragmentAccount extends FragmentEx { IMAPStore istore = null; try { istore = (IMAPStore) isession.getStore("imaps"); - istore.connect(host, Integer.parseInt(port), user, password); + try { + istore.connect(host, Integer.parseInt(port), user, password); + } catch (AuthenticationFailedException ex) { + if (auth_type == Helper.AUTH_TYPE_GMAIL) { + password = Helper.refreshToken(context, "com.google", user, password); + istore.connect(host, Integer.parseInt(port), user, password); + } else + throw ex; + } if (!istore.hasCapability("IDLE")) throw new MessagingException(getContext().getString(R.string.title_no_idle)); @@ -708,7 +717,15 @@ public class FragmentAccount extends FragmentEx { IMAPStore istore = null; try { istore = (IMAPStore) isession.getStore("imaps"); - istore.connect(host, Integer.parseInt(port), user, password); + try { + istore.connect(host, Integer.parseInt(port), user, password); + } catch (AuthenticationFailedException ex) { + if (auth_type == Helper.AUTH_TYPE_GMAIL) { + password = Helper.refreshToken(context, "com.google", user, password); + istore.connect(host, Integer.parseInt(port), user, password); + } else + throw ex; + } if (!istore.hasCapability("UIDPLUS")) throw new MessagingException(getContext().getString(R.string.title_no_uidplus)); diff --git a/app/src/main/java/eu/faircode/email/FragmentIdentity.java b/app/src/main/java/eu/faircode/email/FragmentIdentity.java index 2d88cedd..c731ca57 100644 --- a/app/src/main/java/eu/faircode/email/FragmentIdentity.java +++ b/app/src/main/java/eu/faircode/email/FragmentIdentity.java @@ -49,6 +49,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Properties; +import javax.mail.AuthenticationFailedException; import javax.mail.Session; import javax.mail.Transport; @@ -342,7 +343,15 @@ public class FragmentIdentity extends FragmentEx { isession.setDebug(true); Transport itransport = isession.getTransport(starttls ? "smtp" : "smtps"); try { - itransport.connect(host, Integer.parseInt(port), user, password); + try { + itransport.connect(host, Integer.parseInt(port), user, password); + } catch (AuthenticationFailedException ex) { + if (auth_type == Helper.AUTH_TYPE_GMAIL) { + password = Helper.refreshToken(context, "com.google", user, password); + itransport.connect(host, Integer.parseInt(port), user, password); + } else + throw ex; + } } finally { itransport.close(); } diff --git a/app/src/main/java/eu/faircode/email/Helper.java b/app/src/main/java/eu/faircode/email/Helper.java index ea9b1d6f..ff2a7652 100644 --- a/app/src/main/java/eu/faircode/email/Helper.java +++ b/app/src/main/java/eu/faircode/email/Helper.java @@ -230,7 +230,7 @@ public class Helper { } } - private static String refreshToken(Context context, String type, String name, String current) { + static String refreshToken(Context context, String type, String name, String current) { try { AccountManager am = AccountManager.get(context); Account[] accounts = am.getAccountsByType(type); diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 328c434d..b245cde9 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -1261,7 +1261,16 @@ public class ServiceSynchronize extends LifecycleService { try { // Connect transport db.identity().setIdentityState(ident.id, "connecting"); - itransport.connect(ident.host, ident.port, ident.user, ident.password); + try { + itransport.connect(ident.host, ident.port, ident.user, ident.password); + } catch (AuthenticationFailedException ex) { + if (ident.auth_type == Helper.AUTH_TYPE_GMAIL) { + ident.password = Helper.refreshToken(this, "com.google", ident.user, ident.password); + DB.getInstance(this).identity().setIdentityPassword(ident.id, ident.password); + itransport.connect(ident.host, ident.port, ident.user, ident.password); + } else + throw ex; + } db.identity().setIdentityState(ident.id, "connected"); db.identity().setIdentityError(ident.id, null);