public static string DecodeMIME(string mimeString, e_Encoding_Type e_Type, string Charset) { // Example: mimeString = "=?Windows-1252?Q?Registered_Member_News=3A_WPC09_to_feature_Windows_7=2C_?==?Windows-1252?Q?_Office=2C_Exchange=2C_more=85?=" // In this example two Q-encoded strings are defined! string encodedString = mimeString; string decodedString = ""; while (encodedString.Length > 0) { Match match = Regex.Match(encodedString, @"=\?(?<charset>.*?)\?(?<encoding>[qQbB])\?(?<value>.*?)\?="); if (match.Success) { string CurrCharset = match.Groups["charset"].Value; if (string.IsNullOrEmpty(CurrCharset)) { CurrCharset = Charset; } string value = match.Groups["value"].Value; e_Encoding_Type e_TypeCurrent = GetEncoding(match.Groups["encoding"].Value.ToLower(), false); if (e_TypeCurrent == e_Encoding_Type.t_unknown) { e_TypeCurrent = e_Type; } decodedString += Decode(value, e_TypeCurrent, CurrCharset); // When multiple entries, subtract the currently decoded part encodedString = encodedString.Substring(match.Index + match.Length); } else { // Unable to decode (not mime encoded) if (!string.IsNullOrEmpty(decodedString)) { return(decodedString + encodedString); } else { return(mimeString); } } } // Successfull return(decodedString); }
public static string DecodeEncoding(string Source, e_Encoding_Type this_Encoding) { string res = ""; switch (this_Encoding) { case e_Encoding_Type.t_base64: try { // not all strings can be decoded // for example W05vcnRvbiBBbnRpU3BhbV0gS3JlbWxpbi5ydTog0JXQttC10LTQvdC10LLQvdCw0Y8g0YDQsNGB0YHRi9C70LrQsA // inpossible, but C++ working with it well ??? byte[] Tempval = Convert.FromBase64String(Source); for (int i = 0; i < Tempval.Length; i++) { res += (char)Tempval[i]; } } catch { res = c_base64.decode(Source); } break; case e_Encoding_Type.t_quoted_printable: // not realized //parse looking for =XX where XX is hexadecimal Regex re = new Regex( "(\\=([0-9A-F][0-9A-F]))", RegexOptions.IgnoreCase ); res = re.Replace(Source, new MatchEvaluator(HexDecoderEvaluator)); res = res.Replace('_', ' '); break; case e_Encoding_Type.t_7bit: case e_Encoding_Type.t_8bit: case e_Encoding_Type.t_binary: default: return(Source); } return(res); }
public static string DecodeMIME(string mimeString, e_Encoding_Type e_Type) { return(DecodeMIME(mimeString, e_Type, "")); }
public static string Decode(byte [] Source, e_Encoding_Type this_Encoding, string ThisCharset) { return(DecodeCodePage(DecodeEncoding(Source, this_Encoding), ThisCharset)); }