예제 #1
0
        internal 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.ContentType = 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.ContentType = ContentDispositionTypeMap.FromString(disposition["value"]);
                p.Disposition.FileName = disposition["FileName"];
                foreach (string k in disposition.AllKeys)
                {
                    p.Disposition.Attributes.Add(k, disposition[k]);
                }
            }

            return p;
        }
예제 #2
0
        internal static MIMEPart[] ParseMIMEParts(StringReader reader, string boundary)
        {
            List<MIMEPart> list = new List<MIMEPart>();
            string start = "--" + boundary, end = "--" + boundary + "--", line;

            while ((line = reader.ReadLine()) != null)
            {
                if (line.StartsWith(start))
                {
                    break;
                }
            }

            while (line != null && line.StartsWith(start))
            {
                MIMEPart p = new MIMEPart();

                StringBuilder header = new StringBuilder();
                while (!string.IsNullOrEmpty(line = reader.ReadLine()))
                {
                    header.AppendLine(line);
                }

                p.Header = ParseMailHeader(header.ToString());

                NameValueCollection contentType = ParseMIMEField(p.Header["Content-Type"]);
                if (contentType["Boundary"] != null)
                {
                    list.AddRange(ParseMIMEParts(reader, contentType["boundary"]));
                }

                StringBuilder body = new StringBuilder();
                while ((line = reader.ReadLine()) != null)
                {
                    if (line.StartsWith(start))
                    {
                        break;
                    }

                    body.AppendLine(line);
                }

                p.Body = body.ToString();

                if (string.IsNullOrEmpty(p.Body) && string.IsNullOrEmpty(p.Body.Trim()))
                {
                    list.Add(p);
                }

                if (line == null || line.StartsWith(end))
                {
                    break;
                }
            }

            return list.ToArray();
        }