public byte[] SerializeMimeContent(MimeContent content) { MemoryStream ContentStream = new MemoryStream(); SerializeMimeContent(content, ContentStream); return(ContentStream.ToArray()); }
public MimeContent DeserializeMimeContent(string httpContentType, byte[] binaryContent) { // // First of all parse the http content type // string MimeType = null, MimeBoundary = null, MimeStart = null; ParseHttpContentTypeHeader(httpContentType, ref MimeType, ref MimeBoundary, ref MimeStart); // // Create the mime-content // MimeContent Content = new MimeContent() { Boundary = MimeBoundary }; // // Start finding the parts in the mime message // Note: in MIME RFC a "--" represents the end of something // int EndBoundaryHelperIdx = 0; byte[] MimeBoundaryBytes = ParserEncoding.GetBytes("\r\n--" + MimeBoundary); for (int i = 0; i < binaryContent.Length; i++) { if (AreArrayPartsForTextEqual(MimeBoundaryBytes, 0, binaryContent, i, MimeBoundaryBytes.Length)) { EndBoundaryHelperIdx = i + MimeBoundaryBytes.Length; if ((EndBoundaryHelperIdx + 1) < binaryContent.Length) { // The end of the MIME-message is the boundary followed by "--" if (binaryContent[EndBoundaryHelperIdx] == '-' && binaryContent[EndBoundaryHelperIdx + 1] == '-') { break; } } else { throw new ApplicationException("Invalid MIME content parsed, premature End-Of-File detected!"); } // Start reading the mime part after the boundary MimePart Part = ReadMimePart(binaryContent, ref i, MimeBoundaryBytes); if (Part != null) { Content.Parts.Add(Part); } } } // // Finally return the ready-to-use object model // return(Content); }
public void SerializeMimeContent(MimeContent content, Stream contentStream) { byte[] WriteHelper; byte[] CarriageReturnLineFeed = new byte[] { (byte)'\r', (byte)'\n' }; // // Prepare some bytes written more than once // byte[] BoundaryBytes = ParserEncoding.GetBytes("--" + content.Boundary); // // Write every part into the stream // foreach (var item in content.Parts) { // // First of all write the boundary // contentStream.Write(CarriageReturnLineFeed, 0, CarriageReturnLineFeed.Length); contentStream.Write(BoundaryBytes, 0, BoundaryBytes.Length); contentStream.Write(CarriageReturnLineFeed, 0, 2); // // Write the content-type for the current element // StringBuilder Builder = new StringBuilder(); Builder.Append(string.Format("Content-Type: {0}", item.ContentType)); if (!string.IsNullOrEmpty(item.CharSet)) { Builder.Append(string.Format("; charset={0}", item.CharSet)); } Builder.Append(new char[] { '\r', '\n' }); Builder.Append(string.Format("Content-Transfer-Encoding: {0}", item.TransferEncoding)); Builder.Append(new char[] { '\r', '\n' }); Builder.Append(string.Format("Content-Id: {0}", item.ContentId)); WriteHelper = ParserEncoding.GetBytes(Builder.ToString()); contentStream.Write(WriteHelper, 0, WriteHelper.Length); contentStream.Write(CarriageReturnLineFeed, 0, CarriageReturnLineFeed.Length); contentStream.Write(CarriageReturnLineFeed, 0, CarriageReturnLineFeed.Length); // // Write the actual content // contentStream.Write(item.Content, 0, item.Content.Length); } // // Write one last content boundary // contentStream.Write(CarriageReturnLineFeed, 0, CarriageReturnLineFeed.Length); contentStream.Write(BoundaryBytes, 0, BoundaryBytes.Length); contentStream.Write(new byte[] { 45, 45 }, 0, 2); contentStream.Write(CarriageReturnLineFeed, 0, CarriageReturnLineFeed.Length); }