diff --git a/OpaqueMail/Functions.cs b/OpaqueMail/Functions.cs index d0680a5..0a8316c 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 @@ -221,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)) { @@ -267,7 +268,10 @@ 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)); + if (wrapInQuotes) + decodedAddress = NormalizeQuotes(decodedAddress); + headerBuilder.Append(decodedAddress); cursor = endCursor + 2; } @@ -945,6 +949,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. /// 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();