/// <summary>
        /// Default constructor.
        /// </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>
        /// <exception cref="ArgumentNullException">Is raised when <b>charset</b> is null reference.</exception>
        public MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding encoding,Encoding charset)
        {
            if(charset == null){
                throw new ArgumentNullException("charset");
            }

            m_Encoding = encoding;
            m_pCharset = charset;
        }
        /// <summary>
        /// Default constructor.
        /// </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>
        /// <exception cref="ArgumentNullException">Is raised when <b>charset</b> is null reference.</exception>
        public MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding encoding, Encoding charset)
        {
            if (charset == null)
            {
                throw new ArgumentNullException("charset");
            }

            m_Encoding = encoding;
            m_pCharset = charset;
        }
        /// <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="split">If true, words are splitted after 75 chars.</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,bool split,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)){
                List<string> parts = new List<string>();
                if(split){
                    int index = 0;
                    // We just split text to 30 char words, then if some chars encoded, we don't exceed 75 chars lenght limit.
                    while(index < text.Length){
                        int countReaded = Math.Min(30,text.Length - index);
                        parts.Add(text.Substring(index,countReaded));                        
                        index += countReaded;                        
                    }
                }
                else{
                    parts.Add(text);
                }

                StringBuilder retVal = new StringBuilder();
                for(int i=0;i<parts.Count;i++){
                    string part = parts[i];
                    byte[] data = charset.GetBytes(part);

                    #region B encode

                    if(encoding == MIME_EncodedWordEncoding.B){
                        retVal.Append("=?" + charset.WebName + "?B?" + Convert.ToBase64String(data) + "?=");
                    }

                    #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();
                            }

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

                    #endregion

                    if(i < (parts.Count - 1)){
                        retVal.Append("\r\n ");
                    }
                }

                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="split">If true, words are splitted after 75 chars.</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, bool split, 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))
            {
                List <string> parts = new List <string>();
                if (split)
                {
                    int index = 0;
                    // We just split text to 30 char words, then if some chars encoded, we don't exceed 75 chars lenght limit.
                    while (index < text.Length)
                    {
                        int countReaded = Math.Min(30, text.Length - index);
                        parts.Add(text.Substring(index, countReaded));
                        index += countReaded;
                    }
                }
                else
                {
                    parts.Add(text);
                }

                StringBuilder retVal = new StringBuilder();
                for (int i = 0; i < parts.Count; i++)
                {
                    string part = parts[i];
                    byte[] data = charset.GetBytes(part);

                    #region B encode

                    if (encoding == MIME_EncodedWordEncoding.B)
                    {
                        retVal.Append("=?" + charset.WebName + "?B?" + Convert.ToBase64String(data) + "?=");
                    }

                    #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();
                            }

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

                    #endregion

                    if (i < (parts.Count - 1))
                    {
                        retVal.Append("\r\n ");
                    }
                }

                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 - (("=?" + charset.WebName + "?" + encoding + "?" + "?=")).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 - (("=?" + charset.WebName + "?" + encoding + "?" + "?=")).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;
            }
        }