From b52c3171c4cde399fdee19d2af7e0e2e1ed4fb75 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 16 Jun 2021 16:35:13 +0200 Subject: [PATCH] Improved header encoding fixing --- .../java/eu/faircode/email/MessageHelper.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/MessageHelper.java b/app/src/main/java/eu/faircode/email/MessageHelper.java index ca197ff3a5..280f6fca43 100644 --- a/app/src/main/java/eu/faircode/email/MessageHelper.java +++ b/app/src/main/java/eu/faircode/email/MessageHelper.java @@ -1207,13 +1207,17 @@ public class MessageHelper { if (header.trim().startsWith("=?")) return header; - if (CharsetHelper.isUTF8(header)) { - Log.i("Converting " + name + " to UTF-8"); - return new String(header.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8); - } else { - Log.i("Converting " + name + " to ISO8859-1"); - return new String(header.getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.ISO_8859_1); - } + Charset detected = CharsetHelper.detect(header); + if (detected == null && CharsetHelper.isUTF8(header)) + detected = StandardCharsets.UTF_8; + if (detected == null || + CHARSET16.contains(detected) || + StandardCharsets.US_ASCII.equals(detected) || + StandardCharsets.ISO_8859_1.equals(detected)) + return header; + + Log.i("Converting " + name + " to " + detected); + return new String(header.getBytes(StandardCharsets.ISO_8859_1), detected); } private Address[] getAddressHeader(String name) throws MessagingException {