Ejemplo n.º 1
0
        /// <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>
        static Bodypart BodypartFromMIME(MIMEPart mimePart)
        {
            NameValueCollection contentType = ParseMIMEField(
                mimePart.header["Content-Type"]);
            Bodypart p = new Bodypart(null);
            Match    m = Regex.Match(contentType["value"], "(.+)/(.+)");

            if (m.Success)
            {
                p.Type    = ContentTypeMap.fromString(m.Groups[1].Value);
                p.Subtype = m.Groups[2].Value;
            }
            p.Encoding = ContentTransferEncodingMap.fromString(
                mimePart.header["Content-Transfer-Encoding"]);
            p.Id = mimePart.header["Content-Id"];
            foreach (string k in contentType.AllKeys)
            {
                p.Parameters.Add(k, contentType[k]);
            }
            p.Size = mimePart.body.Length;
            if (mimePart.header["Content-Disposition"] != null)
            {
                NameValueCollection disposition = ParseMIMEField(
                    mimePart.header["Content-Disposition"]);
                p.Disposition.Type = ContentDispositionTypeMap.fromString(
                    disposition["value"]);
                p.Disposition.Filename = disposition["Filename"];
                foreach (string k in disposition.AllKeys)
                {
                    p.Disposition.Attributes.Add(k, disposition[k]);
                }
            }
            return(p);
        }
Ejemplo n.º 2
0
        /// <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>
        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 the MIME parts which are delimited by boundary strings.
            while (line != null && line.StartsWith(start))
            {
                MIMEPart p = new MIMEPart();
                // Read the part header.
                StringBuilder header = new StringBuilder();
                while (!String.IsNullOrEmpty(line = reader.ReadLine()))
                {
                    header.AppendLine(line);
                }
                p.header = ParseMailHeader(header.ToString());
                // Account for nested multipart content.
                NameValueCollection contentType = ParseMIMEField(p.header["Content-Type"]);
                if (contentType["Boundary"] != null)
                {
                    list.AddRange(ParseMIMEParts(reader, contentType["boundary"]));
                }
                // Read the part body.
                StringBuilder 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 or empty which means the body
                // contained nested multipart content.
                if (p.body != null && p.body.Trim() != String.Empty)
                {
                    list.Add(p);
                }
                // If this boundary is the end boundary, we're done.
                if (line == null || line.StartsWith(end))
                {
                    break;
                }
            }
            return(list.ToArray());
        }