internal static new MimeContentType Parse(string mimeValue, bool readOnly) { MimeContentType rtn = new MimeContentType(); rtn.MimeParse(mimeValue, readOnly); return(rtn); }
public void SetTextContent(string text, System.Text.Encoding encoding) { if (encoding is null) { throw new ArgumentNullException(nameof(encoding)); } ContentType = MimeContentType.NewTextPlain(); ContentType.Charset = encoding.BodyName; ContentTransferEncoding = (encoding.BodyName == "us-ascii") ? MimeEncoding.Text7bit : MimeEncoding.QuotedPrintable; SetContent(encoding.GetBytes(text)); }
public static MimeContentType MapContentType(string fileName, MimeContentType contentType) { if (contentType is null) { throw new ArgumentNullException(nameof(contentType)); } if (contentType.Type == ApplicationOctetStream && fileName != null) { return(FromFileExtension(fileName)); } return(contentType); }
public static MimePart NewAttachment(string name, MimeContentType contentType, byte[] data, int length) { MimePart part = new MimePart() { ContentType = contentType, ContentDisposition = MimeContentDisposition.NewAttachment(name), ContentTransferEncoding = MimeEncoding.Base64 }; part.ContentType.Name = name; part.SetContent(data, length); return(part); }
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(); }
protected MimeMultiPart(MimeContentType contentType, MimeFields fields, byte[] content) : base(fields, content) { ParseMultiPart(contentType, new MimeReader(new MemoryStream(content)), null); }