From 4be5c50117f309747f51157c6f24f8ddfa43fcda Mon Sep 17 00:00:00 2001 From: Sebastian Holc Date: Wed, 3 Jan 2018 16:13:13 +0100 Subject: [PATCH 1/2] Place address between quotes after decoding. --- OpaqueMail/Functions.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/OpaqueMail/Functions.cs b/OpaqueMail/Functions.cs index d0680a5..609f0c3 100644 --- a/OpaqueMail/Functions.cs +++ b/OpaqueMail/Functions.cs @@ -20,6 +20,7 @@ using System.Net.NetworkInformation; using System.Security.Cryptography; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; namespace OpaqueMail @@ -267,7 +268,9 @@ public static string DecodeMailHeader(string header) } // Append the decoded string. - headerBuilder.Append(Encoding.UTF8.GetString(Encoding.Convert(encoding, Encoding.UTF8, encodedBytes))); + string decodedAddress = Encoding.UTF8.GetString(Encoding.Convert(encoding, Encoding.UTF8, encodedBytes)); + decodedAddress = NormalizeQuotes(decodedAddress); + headerBuilder.Append(decodedAddress); cursor = endCursor + 2; } @@ -945,6 +948,16 @@ public static string NormalizeCharSet(string charSet) return charSet; } + /// + /// Ensures that the given address is between quotes + /// + /// + /// + public static string NormalizeQuotes(string address) + { + return '"' + Regex.Replace(address, @"('|"")", "") + '"'; + } + /// /// Returns string representation of message sent over stream. /// From e6cd1fcd0d30f1be837f255efbe757ed89075fb4 Mon Sep 17 00:00:00 2001 From: Sebastian Holc Date: Wed, 10 Jan 2018 10:46:36 +0100 Subject: [PATCH 2/2] Only wrap mail address --- OpaqueMail/Functions.cs | 5 +++-- OpaqueMail/MailAddress.cs | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/OpaqueMail/Functions.cs b/OpaqueMail/Functions.cs index 609f0c3..0a8316c 100644 --- a/OpaqueMail/Functions.cs +++ b/OpaqueMail/Functions.cs @@ -222,7 +222,7 @@ public static string Decode(string input, string contentTransferEncoding, string /// /// Email header to be decoded. /// The decoded email header. - public static string DecodeMailHeader(string header) + public static string DecodeMailHeader(string header, bool wrapInQuotes=false) { if (!string.IsNullOrEmpty(header)) { @@ -269,7 +269,8 @@ public static string DecodeMailHeader(string header) // Append the decoded string. string decodedAddress = Encoding.UTF8.GetString(Encoding.Convert(encoding, Encoding.UTF8, encodedBytes)); - decodedAddress = NormalizeQuotes(decodedAddress); + if (wrapInQuotes) + decodedAddress = NormalizeQuotes(decodedAddress); headerBuilder.Append(decodedAddress); cursor = endCursor + 2; diff --git a/OpaqueMail/MailAddress.cs b/OpaqueMail/MailAddress.cs index 7c6b732..6af5a41 100644 --- a/OpaqueMail/MailAddress.cs +++ b/OpaqueMail/MailAddress.cs @@ -197,7 +197,7 @@ public class MailAddressCollection : Collection public static MailAddressCollection Parse(string addresses) { // Escape embedded encoding. - addresses = Functions.DecodeMailHeader(addresses); + addresses = Functions.DecodeMailHeader(addresses, wrapInQuotes: true); // Create a new collection of MailAddresses to be returned. MailAddressCollection addressCollection = new MailAddressCollection();