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; }