/// <summary>
        /// Encodes specified text if it contains 8-bit chars, otherwise text won't be encoded.
        /// </summary>
        /// <param name="encoding">Encoding to use to encode text.</param>
        /// <param name="charset">Charset to use for encoding. If not sure UTF-8 is strongly recommended.</param>
        /// <param name="text">Text to encode.</param>
        /// <returns>Returns encoded text.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>charset</b> or <b>text</b> is null reference.</exception>
        public static string EncodeS(MIME_EncodedWordEncoding encoding, Encoding charset, string text)
        {
            if (charset == null)
            {
                throw new ArgumentNullException("charset");
            }
            if (text == null)
            {
                throw new ArgumentNullException("text");
            }

            /* RFC 2047 2.
             *  encoded-word = "=?" charset "?" encoding "?" encoded-text "?="
             *
             *  An 'encoded-word' may not be more than 75 characters long, including
             *  'charset', 'encoding', 'encoded-text', and delimiters.  If it is
             *  desirable to encode more text than will fit in an 'encoded-word' of
             *  75 characters, multiple 'encoded-word's (separated by CRLF SPACE) may
             *  be used.
             *
             * RFC 2231 (updates syntax)
             *  encoded-word := "=?" charset ["*" language] "?" encoded-text "?="
             */

            if (MustEncode(text))
            {
                StringBuilder retVal             = new StringBuilder();
                byte[]        data               = charset.GetBytes(text);
                int           maxEncodedTextSize = 75 - ((string)("=?" + charset.WebName + "?" + encoding.ToString() + "?" + "?=")).Length;

                #region B encode

                if (encoding == MIME_EncodedWordEncoding.B)
                {
                    retVal.Append("=?" + charset.WebName + "?B?");
                    int    stored = 0;
                    string base64 = Convert.ToBase64String(data);
                    for (int i = 0; i < base64.Length; i += 4)
                    {
                        // Encoding buffer full, create new encoded-word.
                        if (stored + 4 > maxEncodedTextSize)
                        {
                            retVal.Append("?=\r\n =?" + charset.WebName + "?B?");
                            stored = 0;
                        }

                        retVal.Append(base64, i, 4);
                        stored += 4;
                    }
                    retVal.Append("?=");
                }

                #endregion

                #region Q encode

                else
                {
                    retVal.Append("=?" + charset.WebName + "?Q?");
                    int stored = 0;
                    foreach (byte b in data)
                    {
                        string val = null;
                        // We need to encode byte. Defined in RFC 2047 4.2.
                        if (b > 127 || b == '=' || b == '?' || b == '_' || b == ' ')
                        {
                            val = "=" + b.ToString("X2");
                        }
                        else
                        {
                            val = ((char)b).ToString();
                        }

                        // Encoding buffer full, create new encoded-word.
                        if (stored + val.Length > maxEncodedTextSize)
                        {
                            retVal.Append("?=\r\n =?" + charset.WebName + "?Q?");
                            stored = 0;
                        }

                        retVal.Append(val);
                        stored += val.Length;
                    }
                    retVal.Append("?=");
                }

                #endregion

                return(retVal.ToString());
            }
            else
            {
                return(text);
            }
        }
        /// <summary>
        /// Encodes specified text if it contains 8-bit chars, otherwise text won't be encoded.
        /// </summary>
        /// <param name="encoding">Encoding to use to encode text.</param>
        /// <param name="charset">Charset to use for encoding. If not sure UTF-8 is strongly recommended.</param>
        /// <param name="text">Text to encode.</param>
        /// <returns>Returns encoded text.</returns>
        /// <exception cref="ArgumentNullException">Is raised when <b>charset</b> or <b>text</b> is null reference.</exception>
        public static string EncodeS(MIME_EncodedWordEncoding encoding,Encoding charset,string text)
        {
            if(charset == null){
                throw new ArgumentNullException("charset");
            }
            if(text == null){
                throw new ArgumentNullException("text");
            }

            /* RFC 2047 2.
                encoded-word = "=?" charset "?" encoding "?" encoded-text "?="

                An 'encoded-word' may not be more than 75 characters long, including
                'charset', 'encoding', 'encoded-text', and delimiters.  If it is
                desirable to encode more text than will fit in an 'encoded-word' of
                75 characters, multiple 'encoded-word's (separated by CRLF SPACE) may
                be used.

               RFC 2231 (updates syntax)
                encoded-word := "=?" charset ["*" language] "?" encoded-text "?="
            */

            if(MustEncode(text)){
                StringBuilder retVal             = new StringBuilder();
                byte[]        data               = charset.GetBytes(text);
                int           maxEncodedTextSize = 75 - ((string)("=?" + charset.WebName + "?" + encoding.ToString() + "?" + "?=")).Length;

                #region B encode

                if(encoding == MIME_EncodedWordEncoding.B){
                    retVal.Append("=?" + charset.WebName + "?B?");
                    int    stored = 0;
                    string base64 = Convert.ToBase64String(data);
                    for(int i=0;i<base64.Length;i+=4){
                        // Encoding buffer full, create new encoded-word.
                        if(stored + 4 > maxEncodedTextSize){
                            retVal.Append("?=\r\n =?" + charset.WebName + "?B?");
                            stored = 0;
                        }

                        retVal.Append(base64,i,4);
                        stored += 4;
                    }
                    retVal.Append("?=");
                }

                #endregion

                #region Q encode

                else{
                    retVal.Append("=?" + charset.WebName + "?Q?");
                    int stored = 0;
                    foreach(byte b in data){
                        string val = null;
                        // We need to encode byte. Defined in RFC 2047 4.2.
                        if(b > 127 || b == '=' || b == '?' || b == '_' || b == ' '){
                            val = "=" + b.ToString("X2");
                        }
                        else{
                            val = ((char)b).ToString();
                        }

                        // Encoding buffer full, create new encoded-word.
                        if(stored + val.Length > maxEncodedTextSize){
                            retVal.Append("?=\r\n =?" + charset.WebName + "?Q?");
                            stored = 0;
                        }

                        retVal.Append(val);
                        stored += val.Length;
                    }
                    retVal.Append("?=");
                }

                #endregion

                return retVal.ToString();
            }
            else{
                return text;
            }
        }