Beispiel #1
0
        private static Encoding ParseBodyEncoding(string characterSet)
        {
            Encoding encoding = Encoding.ASCII;

            if (!string.IsNullOrEmpty(characterSet))
            {
                encoding = EncodingFinder.FindEncoding(characterSet);
            }
            return(encoding);
        }
Beispiel #2
0
        public static string Decode(string encodedWords)
        {
            if (encodedWords == null)
            {
                throw new ArgumentNullException("encodedWords");
            }
            const string encodedWordRegex = @"\=\?(?<Charset>\S+?)\?(?<Encoding>\w)\?(?<Content>.+?)\?\=";
            const string replaceRegex     = @"(?<first>" + encodedWordRegex + @")\s+(?<second>" + encodedWordRegex + ")";

            encodedWords = Regex.Replace(encodedWords, replaceRegex, "${first}${second}");
            encodedWords = Regex.Replace(encodedWords, replaceRegex, "${first}${second}");
            string          decodedWords = encodedWords;
            MatchCollection matches      = Regex.Matches(encodedWords, encodedWordRegex);

            foreach (Match match in matches)
            {
                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;
                Encoding charsetEncoding = EncodingFinder.FindEncoding(charset);
                string   decodedText;
                switch (encoding.ToUpperInvariant())
                {
                case "B":
                    decodedText = Base64Decode(encodedText, charsetEncoding);
                    break;

                case "Q":
                    decodedText = QuotedPrintable.DecodeEncodedWord(encodedText, charsetEncoding);
                    break;

                default:
                    throw new ArgumentException("The encoding " + encoding + " was not recognized");
                }
                decodedWords = decodedWords.Replace(fullMatchValue, decodedText);
            }
            return(decodedWords);
        }