From b82f10dd803661d4a5e220e8db1f3256c14f0d6a Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 22 Jan 2020 20:43:44 +0100 Subject: [PATCH] Workaround '501 Syntactically invalid HELO argument(s)' --- .../java/eu/faircode/email/MailService.java | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/MailService.java b/app/src/main/java/eu/faircode/email/MailService.java index ab688a89bd..9c484b4810 100644 --- a/app/src/main/java/eu/faircode/email/MailService.java +++ b/app/src/main/java/eu/faircode/email/MailService.java @@ -364,33 +364,31 @@ public class MailService implements AutoCloseable { } else if ("smtp".equals(protocol) || "smtps".equals(protocol)) { String[] c = BuildConfig.APPLICATION_ID.split("\\."); Collections.reverse(Arrays.asList(c)); - String domain = TextUtils.join(".", c); + String hdomain = TextUtils.join(".", c); - String haddr = domain; - if (useip) - try { - // This assumes getByName always returns the same address (type) - InetAddress addr = InetAddress.getByName(host); - if (addr instanceof Inet4Address) - haddr = "[" + Inet4Address.getLocalHost().getHostAddress() + "]"; - else - haddr = "[IPv6:" + Inet6Address.getLocalHost().getHostAddress() + "]"; - } catch (UnknownHostException ex) { - Log.w(ex); - } + String haddr = "[127.0.0.1]"; + try { + // This assumes getByName always returns the same address (type) + InetAddress addr = InetAddress.getByName(host); + if (addr instanceof Inet4Address) + haddr = "[" + Inet4Address.getLocalHost().getHostAddress() + "]"; + else + haddr = "[IPv6:" + Inet6Address.getLocalHost().getHostAddress() + "]"; + } catch (UnknownHostException ex) { + Log.w(ex); + } Log.i("Using localhost=" + haddr); - properties.put("mail." + protocol + ".localhost", haddr); + properties.put("mail." + protocol + ".localhost", useip ? haddr : hdomain); iservice = isession.getTransport(protocol); try { iservice.connect(host, port, user, password); } catch (MessagingException ex) { - if (useip && - ex.getMessage() != null && + if (ex.getMessage() != null && ex.getMessage().toLowerCase().contains("syntactically invalid")) { - Log.w("Using localhost=" + domain, ex); - ((SMTPTransport) iservice).setLocalHost(domain); + Log.w("Using localhost=" + (useip ? hdomain : haddr), ex); + ((SMTPTransport) iservice).setLocalHost(useip ? hdomain : haddr); iservice.connect(host, port, user, password); } else throw ex;