diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index 3948c64e..10e59054 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -1174,6 +1174,9 @@ public class EwsExchangeSession extends ExchangeSession { // workaround for missing urlcompname in Exchange 2010 if (itemName == null) { itemName = StringUtil.base64ToUrl(itemId.id) + ".EML"; + } else if (itemName.indexOf('\n') >= 0) { + // encode line feed + itemName = itemName.replaceAll("\n", "_x000D__x000A_"); } String instancetype = response.get(Field.get("instancetype").getResponseName()); boolean isrecurring = "true".equals(response.get(Field.get("isrecurring").getResponseName())); diff --git a/src/java/davmail/util/StringUtil.java b/src/java/davmail/util/StringUtil.java index 55634e12..9729571e 100644 --- a/src/java/davmail/util/StringUtil.java +++ b/src/java/davmail/util/StringUtil.java @@ -133,7 +133,8 @@ public final class StringUtil { private static final Pattern GT_PATTERN = Pattern.compile(">"); private static final Pattern QUOTE_PATTERN = Pattern.compile("\""); - private static final Pattern LF_PATTERN = Pattern.compile("\n"); + private static final Pattern CR_PATTERN = Pattern.compile("\r"); + private static final Pattern LF_PATTERN = Pattern.compile("\n"); private static final Pattern URLENCODED_AMP_PATTERN = Pattern.compile("%26"); @@ -142,6 +143,8 @@ public final class StringUtil { private static final Pattern ENCODED_GT_PATTERN = Pattern.compile(">"); private static final Pattern F8FF_PATTERN = Pattern.compile("_xF8FF_"); + private static final Pattern X0D0A_PATTERN = Pattern.compile("_x000D__x000A_"); + private static final Pattern PLUS_PATTERN = Pattern.compile("\\+"); private static final Pattern COLON_PATTERN = Pattern.compile(":"); private static final Pattern SLASH_PATTERN = Pattern.compile("/"); @@ -213,6 +216,12 @@ public final class StringUtil { if (result.indexOf('"') >= 0) { result = QUOTE_PATTERN.matcher(result).replaceAll("""); } + if (result.indexOf('\r') >=0) { + result = CR_PATTERN.matcher(result).replaceAll(" "); + } + if (result.indexOf('\n') >=0) { + result = LF_PATTERN.matcher(result).replaceAll(" "); + } } return result; } @@ -295,8 +304,8 @@ public final class StringUtil { if (result.indexOf('"') >= 0) { result = QUOTE_PATTERN.matcher(result).replaceAll("%22"); } - if (result.indexOf('\n') >= 0) { - result = LF_PATTERN.matcher(result).replaceAll("%0A"); + if (result.indexOf("_x000D__x000A_") >= 0) { + result = X0D0A_PATTERN.matcher(result).replaceAll("\r\n"); } return result; }