public static string Base64HeaderDecode(string line) { MemoryStream ms = null; byte[] bytes = null; string oStr = null; string code = null; string content = null; Match m = Regex.Match(line, @"=\?([^?]+)\?[^?]+\?([^?]+)\?="); while (m.Success) { ms = new MemoryStream(); oStr = m.Groups[0].ToString(); code = m.Groups[1].ToString(); content = m.Groups[2].ToString(); NntpUtil.Base64Decode(content, ms); ms.Seek(0, SeekOrigin.Begin); bytes = new byte[ms.Length]; ms.Read(bytes, 0, bytes.Length); line = line.Replace(oStr, Encoding.GetEncoding(code).GetString(bytes)); m = m.NextMatch(); } return(line); }
public static MIMEPart DispatchMIMEContent(StreamReader sr, MIMEPart part, string seperator) { string line = null; Match m = null; MemoryStream ms; byte[] bytes; switch (part.ContentType.Substring(0, part.ContentType.IndexOf('/')).ToUpper()) { case "MULTIPART": MIMEPart newPart = null; while ((line = sr.ReadLine()) != null && line != seperator && line != seperator + "--") { m = Regex.Match(line, @"CONTENT-TYPE: ""?([^""\s;]+)", RegexOptions.IgnoreCase); if (!m.Success) { continue; } newPart = new MIMEPart(); newPart.ContentType = m.Groups[1].ToString(); newPart.Charset = "US-ASCII"; newPart.ContentTransferEncoding = "7BIT"; while (line != "") { m = Regex.Match(line, @"BOUNDARY=""?([^""\s;]+)", RegexOptions.IgnoreCase); if (m.Success) { newPart.Boundary = m.Groups[1].ToString(); newPart.EmbeddedPartList = new ArrayList(); } m = Regex.Match(line, @"CHARSET=""?([^""\s;]+)", RegexOptions.IgnoreCase); if (m.Success) { newPart.Charset = m.Groups[1].ToString(); } m = Regex.Match(line, @"CONTENT-TRANSFER-ENCODING: ""?([^""\s;]+)", RegexOptions.IgnoreCase); if (m.Success) { newPart.ContentTransferEncoding = m.Groups[1].ToString(); } m = Regex.Match(line, @"NAME=""?([^""\s;]+)", RegexOptions.IgnoreCase); if (m.Success) { newPart.Filename = Base64HeaderDecode(m.Groups[1].ToString()); newPart.Filename = newPart.Filename.Substring(newPart.Filename.LastIndexOfAny(new char[] { '\\', '/' }) + 1); } line = sr.ReadLine(); } part.EmbeddedPartList.Add(DispatchMIMEContent(sr, newPart, "--" + part.Boundary)); } break; case "TEXT": ms = new MemoryStream(); bytes = null; long pos; StreamReader msr = new StreamReader(ms, Encoding.GetEncoding(part.Charset)); StringBuilder sb = new StringBuilder(); while ((line = sr.ReadLine()) != null && line != seperator && line != seperator + "--") { pos = ms.Position; if (line != "") { switch (part.ContentTransferEncoding.ToUpper()) { case "QUOTED-PRINTABLE": NntpUtil.QuotedPrintableDecode(line, ms); break; case "BASE64": if (line != null && line != "") { NntpUtil.Base64Decode(line, ms); } break; case "UU": if (line != null && line != "") { NntpUtil.UUDecode(line, ms); } break; case "7BIT": bytes = Encoding.ASCII.GetBytes(line); ms.Write(bytes, 0, bytes.Length); ms.WriteByte((byte)'\n'); break; default: bytes = Encoding.ASCII.GetBytes(line); ms.Write(bytes, 0, bytes.Length); ms.WriteByte((byte)'\n'); break; } } ms.Position = pos; if (part.ContentType.ToUpper() == "TEXT/HTML") { sb.Append(msr.ReadToEnd()); } else { sb.Append(HttpUtility.HtmlEncode(msr.ReadToEnd()).Replace("\n", "<br>\n")); } } part.Text = sb.ToString(); break; default: ms = new MemoryStream(); bytes = null; while ((line = sr.ReadLine()) != null && line != seperator && line != seperator + "--") { if (line != "") { switch (part.ContentTransferEncoding.ToUpper()) { case "QUOTED-PRINTABLE": NntpUtil.QuotedPrintableDecode(line, ms); break; case "BASE64": if (line != null && line != "") { NntpUtil.Base64Decode(line, ms); } break; case "UU": if (line != null && line != "") { NntpUtil.UUDecode(line, ms); } break; default: bytes = Encoding.ASCII.GetBytes(line); ms.Write(bytes, 0, bytes.Length); break; } } } ms.Seek(0, SeekOrigin.Begin); part.BinaryData = new byte[ms.Length]; ms.Read(part.BinaryData, 0, (int)ms.Length); break; } return(part); }