/// <summary> /// Glue method to create a bodypart from a MIMEPart instance. /// </summary> /// <param name="mimePart">The MIMEPart instance to create the /// bodypart instance from.</param> /// <returns>An initialized instance of the Bodypart class.</returns> // ReSharper disable once InconsistentNaming private static Bodypart BodypartFromMIME(MIMEPart mimePart) { var contentType = ParseMIMEField(mimePart.header["Content-Type"]); var bodypart = new Bodypart(null); var match = Regex.Match(contentType["value"], "(.+)/(.+)"); if (match.Success) { bodypart.Type = ContentTypeMap.FromString(match.Groups[1].Value); bodypart.Subtype = match.Groups[2].Value; } bodypart.Encoding = ContentTransferEncodingMap.FromString( mimePart.header["Content-Transfer-Encoding"]); bodypart.Id = mimePart.header["Content-Id"]; foreach (var key in contentType.AllKeys) { bodypart.Parameters.Add(key, contentType[key]); } bodypart.Size = mimePart.body.Length; if (mimePart.header["Content-Disposition"] == null) { return(bodypart); } var disposition = ParseMIMEField(mimePart.header["Content-Disposition"]); bodypart.Disposition.Type = ContentDispositionTypeMap.FromString(disposition["value"]); bodypart.Disposition.Filename = disposition["Filename"]; foreach (var key in disposition.AllKeys) { bodypart.Disposition.Attributes.Add(key, disposition[key]); } return(bodypart); }
/// <summary> /// Parses the body of a multipart MIME mail message. /// </summary> /// <param name="reader">An instance of the StringReader class initialized /// with a string containing the body of the mail message.</param> /// <param name="boundary">The boundary value as is present as part of /// the Content-Type header field in multipart mail messages.</param> /// <returns>An array of initialized MIMEPart instances representing /// the various parts of the MIME mail message.</returns> // ReSharper disable once InconsistentNaming private static MIMEPart[] ParseMIMEParts(StringReader reader, string boundary) { List <MIMEPart> list = new List <MIMEPart>(); string start = "--" + boundary, end = "--" + boundary + "--", line; // Skip everything up to the first boundary while ((line = reader.ReadLine()) != null) { if (line.StartsWith(start)) { break; } } // Read MIME parts delimited by boundary strings while (line != null && line.StartsWith(start)) { var p = new MIMEPart(); // Read the part header var header = new StringBuilder(); while (!String.IsNullOrEmpty(line = reader.ReadLine())) { header.AppendLine(line); } p.header = ParseMailHeader(header.ToString()); // Account for nested multipart content var contentType = ParseMIMEField(p.header["Content-Type"]); if (contentType["Boundary"] != null) { list.AddRange(ParseMIMEParts(reader, contentType["boundary"])); } // Read the part body var body = new StringBuilder(); while ((line = reader.ReadLine()) != null) { if (line.StartsWith(start)) { break; } body.AppendLine(line); } p.body = body.ToString(); // Add the MIME part to the list unless body is null which means the // body contained nested multipart content if (!string.IsNullOrEmpty(p.body)) { list.Add(p); } // If the boundary is actually the end boundary, we're done if (line == null || line.StartsWith(end)) { break; } } return(list.ToArray()); }