private void _parseMessage(Stream stream) { MimeReader reader = new MimeReader(stream); using (var bodyWriter = new StringWriter()) { SetFields(reader.ReadFields()); if (MimeVersion != null) { string StrContentType = Fields.Value("Content-Type"); if (StrContentType == null) { throw new MimeException("Invalid mime-message, missing 'Content-Type'."); } MimeContentType ContentType = MimeContentType.Parse(StrContentType, true); if (ContentType.isMultipart) { ParseMultiPart(ContentType, reader, bodyWriter); } else { SetContent(reader.ReadData(ContentTransferEncoding, null)); } } else { while (reader.ReadLine(false)) { reader.WriteLineTo(bodyWriter); } } _body = bodyWriter.ToString(); } Fields.SetCollectionReadOnly(); }
protected void ParseMultiPart(MimeContentType contentType, MimeReader reader, StringWriter bodyWriter) { if (contentType is null) { throw new ArgumentNullException(nameof(contentType)); } if (reader is null) { throw new ArgumentNullException(nameof(reader)); } _parts = new MimeParts(); string Boundary = contentType.Boundary; if (Boundary == null) { throw new MimeException("Invalid multipart-mime-message, missing 'Boundary'."); } while (true) { if (!reader.ReadLine(false)) { throw new MimeException("Invalid multipart-mime-message, missing begin-boundary."); } if (reader.TestBoundary(Boundary) == 1) { break; } if (bodyWriter != null) { reader.WriteLineTo(bodyWriter); } } while (reader.TestBoundary(Boundary) != -1) { if (reader.TestBoundary(Boundary) == 1) { MimeFields PartFields = reader.ReadFields(); MimeField FldContentTransferEncoding = PartFields["Content-Transfer-Encoding"]; byte[] PartContent = reader.ReadData(MimePart.StringToMimeEncoding(FldContentTransferEncoding?.Value), Boundary); MimeField FldPartContentType = PartFields["Content-Type"]; MimeContentType PartContentType = FldPartContentType?.ValueContentType; if (PartContentType != null && PartContentType.isMultipart) { _parts.Add(new MimeMultiPart(PartContentType, PartFields, PartContent)); } else { _parts.Add(new MimePart(PartFields, PartContent)); } PartFields.SetCollectionReadOnly(); } else { if (!reader.isLineEmpty) { throw new MimeException("Invalid multipart-mime-message, garbage in between parts."); } if (!reader.ReadLine(false)) { throw new MimeException("Invalid multipart-mime-message, missing end-boundary."); } } } _parts.SetCollectionReadOnly(); }