private ArticleBody GetNormalBody(string messageId) { char[] buff = new char[1]; string response = null; ArrayList list = new ArrayList(); StringBuilder sb = new StringBuilder(); Attachment attach = null; MemoryStream ms = null; sr.Read(buff, 0, 1); int i = 0; byte[] bytes = null; Match m = null; while ((response = sr.ReadLine()) != null) { if (buff[0] == '.') { if (response == "") { break; } else { sb.Append(response); } } else { if ((buff[0] == 'B' || buff[0] == 'b') && (m = Regex.Match(response, @"^EGIN \d\d\d (.+)$", RegexOptions.IgnoreCase)).Success) { ms = new MemoryStream(); while ((response = sr.ReadLine()) != null && (response.Length != 3 || response.ToUpper() != "END")) { NntpUtil.UUDecode(response, ms); } ms.Seek(0, SeekOrigin.Begin); bytes = new byte[ms.Length]; ms.Read(bytes, 0, (int)ms.Length); attach = new Attachment(messageId + " - " + m.Groups[1].ToString(), m.Groups[1].ToString(), bytes); list.Add(attach); ms.Close(); i++; } else { sb.Append(buff[0]); sb.Append(response); } } sb.Append('\n'); sr.Read(buff, 0, 1); } ArticleBody ab = new ArticleBody(); ab.IsHtml = false; ab.Text = sb.ToString(); ab.Attachments = (Attachment[])list.ToArray(typeof(Attachment)); return(ab); }
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); }