Esempio n. 1
0
        public static string Parse(string strForParsing, string checkEncodedStr, string patternEncoding,
                                   string patternWithOutEncoding, string strGroupNameToDecode,
                                   string strCharset  = "charset",
                                   string strEncoding = "encoding")
        {
            bool   isEncoded      = strForParsing.Contains(checkEncodedStr);
            string changedPattern = isEncoded ? patternEncoding : patternWithOutEncoding;
            Regex  regex          = new Regex(changedPattern);
            Match  match;

            if (isEncoded)
            {
                match = regex.Match(strForParsing);
                string encoding  = match.Groups[strEncoding].Value;
                string charset   = match.Groups[strCharset].Value;
                string groupName = match.Groups[strGroupNameToDecode].Value;

                Regex           regexEnc        = new Regex(MailMessageRegExPattern.EncodedString);
                MatchCollection matchCollection = regexEnc.Matches(groupName);
                string          encodedData     = string.Empty;
                IMailEncoder    mailEncoder     = MailEncoding.GetMailEncoder(encoding);
                string          decodedString   = string.Empty;

                foreach (Match m in matchCollection)
                {
                    encodedData += m.Groups["data"];
                    if (encodedData.EndsWith("="))
                    {
                        string decodedPart = mailEncoder.Decode(encodedData, charset);
                        decodedString += decodedPart;
                        encodedData    = string.Empty;
                    }
                }

                if (!string.IsNullOrEmpty(encodedData))
                {
                    decodedString += mailEncoder.Decode(encodedData, charset);
                }

                return(decodedString);
            }

            match = regex.Match(strForParsing);
            return(match.Groups[strGroupNameToDecode].Value);
        }
Esempio n. 2
0
        public static string Parse(Match match, bool isEncoded, string strGroupNameToDecode,
                                   string strCharset  = "charset",
                                   string strEncoding = "encoding")
        {
            string matchedStr = match.Groups[strGroupNameToDecode].Value;

            if (!isEncoded)
            {
                return(matchedStr);
            }

            string          encoding        = match.Groups[strEncoding].Value;
            string          charset         = match.Groups[strCharset].Value;
            Regex           regex           = new Regex(MailMessageRegExPattern.EncodedString);
            MatchCollection matchCollection = regex.Matches(matchedStr);
            string          encodedData     = string.Empty;
            IMailEncoder    mailEncoder     = MailEncoding.GetMailEncoder(encoding);
            string          decodedString   = string.Empty;

            foreach (Match m in matchCollection)
            {
                encodedData += m.Groups["data"];
                if (encodedData.EndsWith("="))
                {
                    string decodedPart = mailEncoder.Decode(encodedData, charset);
                    decodedString += decodedPart;
                    encodedData    = string.Empty;
                }
            }

            if (!string.IsNullOrEmpty(encodedData))
            {
                decodedString += mailEncoder.Decode(encodedData, charset);
            }

            return(decodedString);
        }