/// <summary> /// Decodes the part body. /// </summary> /// <param name="part">The part.</param> private static void DecodePartBody(ref MimePart part) { // Let's see if a charset is specified. Otherwise we default to "iso-8859-1". var charset = (!string.IsNullOrEmpty(part.Charset) ? part.Charset : "iso-8859-1"); #if PocketPC if (charset.ToLower() == "iso-8859-1") { charset = "windows-1252"; } #endif try { if (part.ContentTransferEncoding.Equals(ContentTransferEncoding.Base64)) { DecodeBase64Part(part, charset); } else if (part.ContentTransferEncoding.Equals(ContentTransferEncoding.QuotedPrintable)) { part.TextContent = Codec.FromQuotedPrintable(ToASCII(part.BinaryContent), charset); part.BinaryContent = Codec.GetEncoding(charset).GetBytes(part.TextContent); } else { part.TextContent = Codec.GetEncoding(charset).GetString(part.BinaryContent); } } catch (Exception) { part.TextContent = Codec.GetEncoding(charset).GetString(part.BinaryContent); } }
/// <summary> /// Decodes the given string from the format specified in RFC 2047 (=?charset?value?=). /// </summary> /// <param name="input">The string to be decoded.</param> /// <returns>The decoded string.</returns> /// <example> /// The example below illustrates the decoding of a string. /// <code> /// C# /// /// string input = "I once wrote that =?iso-8859-1?B?QWN0aXZlTWFpbCByb2NrcyAhIEhlcmUgYXJlIHNvbWUgd2VpcmQgY2hhcmFjdGVycyA95y4=?="; /// string output = Codec.RFC2047Decode(input); /// </code> /// /// output returns I once wrote that ActiveMail rocks ! Here are some weird characters =ç. /// </example> public static string RFC2047Decode(string input) { input = input.Replace("=?=", "²rep?="); string decoded = input; if (input.IndexOf("=?") != -1 && input.IndexOf("?=") != -1) { string[] encodeds = System.Text.RegularExpressions.Regex.Split(input, System.Text.RegularExpressions.Regex.Escape("=?")); for (int i = 1; i < encodeds.Length; i++) { string encoded = encodeds[i].Substring(0, encodeds[i].LastIndexOf("?=")); string[] parts = encoded.Split('?'); if (parts[1].ToUpper() == "Q") { byte[] data = System.Text.Encoding.ASCII.GetBytes(parts[2].Replace("²rep", "=")); decoded = decoded.Replace("=?" + encoded + "?=", Codec.FromQuotedPrintable(GetEncoding(parts[0]).GetString(data, 0, data.Length), parts[0])); } else { byte[] data = System.Convert.FromBase64String(parts[2].Replace("²rep", "=")); decoded = decoded.Replace("=?" + encoded + "?=", GetEncoding(parts[0]).GetString(data, 0, data.Length)); } } decoded = decoded.Replace("_", " "); } else { decoded = input; } return(decoded); }
/// <summary> /// Decodes the given string from the format specified in RFC 2047 (=?charset?value?=). /// </summary> /// <param name="input">The string to be decoded.</param> /// <returns>The decoded string.</returns> /// <example> /// The example below illustrates the decoding of a string. /// <code> /// C# /// /// string input = "I once wrote that =?iso-8859-1?B?QWN0aXZlTWFpbCByb2NrcyAhIEhlcmUgYXJlIHNvbWUgd2VpcmQgY2hhcmFjdGVycyA95y4=?="; /// string output = Codec.RFC2047Decode(input); /// </code> /// /// output returns I once wrote that ActiveMail rocks ! Here are some weird characters =ç. /// </example> public static string RFC2047Decode(string input) { // Remove whitespaces input = whiteSpace.Replace( input, delegate(Match a) { return("?==?"); }); // Decode encoded words return(encodedWord.Replace( input, delegate(Match curRes) { if (curRes.Groups["encoding"].Value.Equals("B", StringComparison.OrdinalIgnoreCase)) { return GetEncoding(curRes.Groups["charset"].Value).GetString(Convert.FromBase64String(curRes.Groups["message"].Value)); } else { string tmpbuffer = curRes.Groups["message"].Value.Replace("_", " "); return Codec.FromQuotedPrintable(tmpbuffer, curRes.Groups["charset"].Value); } })); }
/// <summary> /// Decodes the part body. /// </summary> /// <param name="part">The part.</param> private static void DecodePartBody(ref MimePart part) { // Let's see if a charset is specified. Otherwise we default to "iso-8859-1". string charset = (!string.IsNullOrEmpty(part.Charset) ? part.Charset : "iso-8859-1"); #if PocketPC if (charset.ToLower() == "iso-8859-1") { charset = "windows-1252"; } #endif // This is a Base64 encoded part body. if (part.ContentTransferEncoding.Equals(ContentTransferEncoding.Base64)) { #if !PocketPC try { #endif //We have the Base64 string so we can decode it. part.BinaryContent = Convert.FromBase64String(part.TextContent); #if !PocketPC } catch (System.FormatException) { part.TextContent = part.TextContent.Remove(part.TextContent.LastIndexOf("=") + 1); part.BinaryContent = Convert.FromBase64String(part.TextContent); } #endif if (part.ContentDisposition != ContentDisposition.Attachment) { part.TextContent = System.Text.Encoding.GetEncoding(charset).GetString(part.BinaryContent, 0, part.BinaryContent.Length); } } // This is a quoted-printable encoded part body. else if (part.ContentTransferEncoding.Equals(ContentTransferEncoding.QuotedPrintable)) { //else if (part.Container != null && part.Container.Charset != null && part.Container.Charset.Length > 0) // charset = part.Container.Charset; // Let's decode. part.TextContent = Codec.FromQuotedPrintable(part.TextContent, charset); // Knowing the charset, we can provide a binary version of this body data. part.BinaryContent = Encoding.GetEncoding(charset).GetBytes(part.TextContent); } // Otherwise, this is an unencoded part body and we keep the text version as it is. else { var encoding = Encoding.GetEncoding(charset); // Knowing the charset, we can provide a binary version of this body data. part.BinaryContent = encoding.GetBytes(part.TextContent); part.TextContent = encoding.GetString(part.BinaryContent); } }
public static MimeTypedAndEncodedContent ParseMimeTypedAndEncodedContent(string data) { MimeTypedAndEncodedContent part = new MimeTypedAndEncodedContent(); // Separate header and body. int headerEnd = Regex.Match(data, @".(?=\r?\n\r?\n)").Index + 1; int bodyStart = Regex.Match(data, @"(?<=\r?\n\r?\n).").Index - 1; string header = data.Substring(0,headerEnd); header = Parser.Unfold(header); header = Codec.RFC2047Decode(header); string body = data.Substring(bodyStart); // Parse header fields and their parameters. Match m = Regex.Match(header, @"(?<=((\r?\n)|\n)|\A)\S+:(.|(\r?\n[\t ]))+(?=((\r?\n)\S)|\Z)"); while (m.Success) { if (m.Value.ToLower().StartsWith("content-type:")) part.ContentType = Parser.GetContentType(m.Value); else if (m.Value.ToLower().StartsWith("content-disposition:")) part.ContentDisposition = Parser.GetContentDisposition(m.Value); part.HeaderFields.Add(FormatFieldName(m.Value.Substring(0, m.Value.IndexOf(':'))), m.Value.Substring(m.Value.IndexOf(':') + 1)); m = m.NextMatch(); } // Is it QP encoded text ? if (part.ContentTransferEncoding.Equals(ContentTransferEncoding.QuotedPrintable)) { // Get the destination charset, or default to us-ascii. string charset = "us-ascii"; if (part.Charset != null && part.Charset.Length > 0) charset = part.Charset; // Decode part.TextContent = Codec.FromQuotedPrintable(body, charset); //part.BinaryContent = System.Text.Encoding.GetEncoding(charset).GetBytes(part.TextContent); } // Is it a Base64 encoded content ? else if (part.ContentTransferEncoding.Equals(ContentTransferEncoding.Base64)) { part.TextContent = body; //part.BinaryContent = Convert.FromBase64String(part.TextContent); } // Is it plain text or binary data ? else //if (part.ContentTransferEncoding.Equals(ContentTransferEncoding.SevenBits) || part.ContentTransferEncoding.Equals(ContentTransferEncoding.SevenBits)) { // Get the destination charset, or default to us-ascii. string charset = "us-ascii"; if (part.Charset != null && part.Charset.Length > 0) charset = part.Charset; // Extract part.TextContent = body; //part.BinaryContent = System.Text.Encoding.GetEncoding(charset).GetBytes(part.TextContent); } // Now we have the decoded content and it's type. Let's take appropriate action. if (part.ContentType.Type.Equals("multipart")) { MultipartContainer multipart = Parser.ParseMultipartContainer(part); multipart.Track = DispatchTrack.MultipartContainer; return multipart; } else if (part.ContentType.Type.Equals("message")) { // TODO } //else if (part.ContentType.Type.Equals("multipart")) return part; }
/// <summary> /// Decodes the given string from the format specified in RFC 2047 (=?charset?value?=). /// </summary> /// <param name="input">The string to be decoded.</param> /// <returns>The decoded string.</returns> /// <example> /// The example below illustrates the decoding of a string. /// <code> /// C# /// /// string input = "I once wrote that =?iso-8859-1?B?QWN0aXZlTWFpbCByb2NrcyAhIEhlcmUgYXJlIHNvbWUgd2VpcmQgY2hhcmFjdGVycyA95y4=?="; /// string output = Codec.RFC2047Decode(input); /// </code> /// /// output returns I once wrote that ActiveMail rocks ! Here are some weird characters =ç. /// </example> public static string RFC2047Decode(string input) { // Remove whitespaces string text = whiteSpace.Replace( input, delegate(Match a) { return("?==?"); }); //SUPPORT_CODE_BEGIN //Todo: Code below added for automated charset detection //This code not part of RFC 2084 var m = encodedWord.Match(text); if (m.Success) { //SUPPORT_CODE_END text = DecodeSameEncodedParts(text); // Decode encoded words text = encodedWord.Replace( text, delegate(Match curRes) { if (curRes.Groups["encoding"].Value.Equals("B", StringComparison.OrdinalIgnoreCase)) { var message = curRes.Groups["message"].Value.Replace(" ", ""); var encoder = GetEncoding(curRes.Groups["charset"].Value); try { return(encoder.GetString(Convert.FromBase64String(message))); } catch { int index = message.LastIndexOf("="); while (index != -1) { // remove the extra character message = message.Remove(index); try { return(encoder.GetString(Convert.FromBase64String(message))); } catch { index = message.LastIndexOf("="); } } throw; } } else { string tmpbuffer = curRes.Groups["message"].Value.Replace("_", " "); return(Codec.FromQuotedPrintable(tmpbuffer, curRes.Groups["charset"].Value)); } }); //SUPPORT_CODE_BEGIN } else { var encoder = GetEncoding(""); byte[] text_in_bytes = encoder.GetBytes(text); var charset_detector = new CharsetDetector(); charset_detector.Feed(text_in_bytes, 0, text_in_bytes.Length); charset_detector.DataEnd(); if (charset_detector.Charset != null) { text = GetEncoding(charset_detector.Charset).GetString(text_in_bytes); } } //SUPPORT_CODE_END return(text); }