コード例 #1
0
ファイル: MessagePart.cs プロジェクト: daowzq/ExtFrame
        /// <summary>
        /// Parses a character set into an encoding
        /// </summary>
        /// <param name="characterSet">The character set that needs to be parsed. <see langword="null"/> is allowed.</param>
        /// <returns>The encoding specified by the <paramref name="characterSet"/> parameter, or ASCII if the character set was <see langword="null"/> or empty</returns>
        private static Encoding ParseBodyEncoding(string characterSet)
        {
            // Default encoding in Mime messages is US-ASCII
            Encoding encoding = Encoding.ASCII;

            // If the character set was specified, find the encoding that the character
            // set describes, and use that one instead
            if (!string.IsNullOrEmpty(characterSet))
            {
                encoding = HeaderFieldParser.ParseCharsetToEncoding(characterSet);
            }

            return(encoding);
        }
コード例 #2
0
        /// <summary>
        /// 标题转码类 <see cref="EncodedWord"/> encoding.<br/>
        ///<br/>
        /// This method will decode any encoded-word found in the string.<br/>
        /// All parts which is not encoded will not be touched.<br/>
        /// <br/>
        /// From <a href="http://tools.ietf.org/html/rfc2047">RFC 2047</a>:<br/>
        /// <code>
        /// Generally, an "encoded-word" is a sequence of printable ASCII
        /// characters that begins with "=?", ends with "?=", and has two "?"s in
        /// between.  It specifies a character set and an encoding method, and
        /// also includes the original text encoded as graphic ASCII characters,
        /// according to the rules for that encoding method.
        /// </code>
        /// Example:<br/>
        /// <c>=?ISO-8859-1?q?this=20is=20some=20text?= other text here</c>
        /// </summary>
        /// <remarks>See <a href="http://tools.ietf.org/html/rfc2047#section-2">RFC 2047 section 2</a> "Syntax of encoded-words" for more details</remarks>
        /// <param name="encodedWords">Source text. May be content which is not encoded.</param>
        /// <returns>Decoded text</returns>
        /// <exception cref="ArgumentNullException">If <paramref name="encodedWords"/> is <see langword="null"/></exception>
        public static string Decode(string encodedWords)
        {
            if (encodedWords == null)
            {
                throw new ArgumentNullException("encodedWords");
            }

            string       decodedWords = encodedWords;
            const string strRegEx     = @"\=\?(?<Charset>\S+?)\?(?<Encoding>\w)\?(?<Content>.+?)\?\=";
            // \w	Matches any word character including underscore. Equivalent to "[A-Za-z0-9_]".
            // \S	Matches any nonwhite space character. Equivalent to "[^ \f\n\r\t\v]".
            // +?   non-gready equivalent to +
            // (?<NAME>REGEX) is a named group with name NAME and regular expression REGEX

            MatchCollection matches = Regex.Matches(encodedWords, strRegEx);

            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 = HeaderFieldParser.ParseCharsetToEncoding(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);
            }

            return(decodedWords);
        }