diff --git a/OpenPop/Mime/Decode/EncodedWord.cs b/OpenPop/Mime/Decode/EncodedWord.cs index c916f39..1f747d3 100644 --- a/OpenPop/Mime/Decode/EncodedWord.cs +++ b/OpenPop/Mime/Decode/EncodedWord.cs @@ -80,53 +80,75 @@ public static string Decode(string encodedWords) string decodedWords = encodedWords; MatchCollection matches = Regex.Matches(encodedWords, encodedWordRegex); - foreach (Match match in matches) - { - // If this match was not a success, we should not use it - if (!match.Success) continue; - - string fullMatchValue = match.Value; - - string encodedText = match.Groups["Content"].Value; - string encoding = match.Groups["Encoding"].Value; - string charset = match.Groups["Charset"].Value; - - // Get the encoding which corrosponds to the character set - Encoding charsetEncoding = EncodingFinder.FindEncoding(charset); - - // Store decoded text here when done - string decodedText; - - // Encoding may also be written in lowercase - switch (encoding.ToUpperInvariant()) - { - // RFC: - // The "B" encoding is identical to the "BASE64" - // encoding defined by RFC 2045. - // http://tools.ietf.org/html/rfc2045#section-6.8 - case "B": - decodedText = Base64.Decode(encodedText, charsetEncoding); - break; - - // RFC: - // The "Q" encoding is similar to the "Quoted-Printable" content- - // transfer-encoding defined in RFC 2045. - // There are more details to this. Please check - // http://tools.ietf.org/html/rfc2047#section-4.2 - // - case "Q": - decodedText = QuotedPrintable.DecodeEncodedWord(encodedText, charsetEncoding); - break; - - default: - throw new ArgumentException("The encoding " + encoding + " was not recognized"); - } - - // Repalce our encoded value with our decoded value - decodedWords = decodedWords.Replace(fullMatchValue, decodedText); - } + if (matches.Count > 0) + { + #region decode words + string encoding = null; + string charset = null; + + // Find encoding and charset + foreach (Match match in matches) + { + if (match.Success) + { + encoding = match.Groups["Encoding"].Value; + charset = match.Groups["Charset"].Value; + break; + } + } + if (encoding == null) + throw new ArgumentException("The encoding " + encoding + " was not recognized"); + + // Join all ecncoded text + StringBuilder matchCache = new StringBuilder(); + StringBuilder encodedCache = new StringBuilder(); + foreach (Match match in matches) + { + // If this match was not a success, we should not use it + if (!match.Success) + continue; + + matchCache.Append(match.Value); + encodedCache.Append(match.Groups["Content"].Value); + } + string encodedText = encodedCache.ToString(); + + // Get the encoding which corrosponds to the character set + Encoding charsetEncoding = EncodingFinder.FindEncoding(charset); + + // Store decoded text here when done + string decodedText; + + // Encoding may also be written in lowercase + switch (encoding.ToUpperInvariant()) + { + // RFC: + // The "B" encoding is identical to the "BASE64" + // encoding defined by RFC 2045. + // http://tools.ietf.org/html/rfc2045#section-6.8 + case "B": + decodedText = Base64.Decode(encodedText, charsetEncoding); + break; + + // RFC: + // The "Q" encoding is similar to the "Quoted-Printable" content- + // transfer-encoding defined in RFC 2045. + // There are more details to this. Please check + // http://tools.ietf.org/html/rfc2047#section-4.2 + // + case "Q": + decodedText = QuotedPrintable.DecodeEncodedWord(encodedText, charsetEncoding); + break; + + default: + throw new ArgumentException("The encoding " + encoding + " was not recognized"); + } + + decodedWords = decodedWords.Replace(matchCache.ToString(), decodedText); + #endregion + } return decodedWords; } } -} \ No newline at end of file +}