Пример #1
0
        private void ParseMime(Stream reader, string boundary, int maxLength)
        {
            var    maxLengthSpecified = maxLength > 0;
            string data,
                   bounderInner = "--" + boundary,
                   bounderOuter = bounderInner + "--";

            do
            {
                data = reader.ReadLine(ref maxLength, Encoding);
            } while (data != null && !data.StartsWith(bounderInner));

            while (data != null && !data.StartsWith(bounderOuter) && (maxLength > 0 || !maxLengthSpecified))
            {
                data = reader.ReadLine(ref maxLength, Encoding);
                var a = new Attachment {
                    Encoding = Encoding
                };

                var part = new StringBuilder();
                // read part header
                while (!data.StartsWith(bounderInner) && data != string.Empty)
                {
                    part.AppendLine(data);
                    data = reader.ReadLine(ref maxLength, Encoding);
                }
                a.RawHeaders = part.ToString();
                // header body

                data = reader.ReadLine(ref maxLength, Encoding);
                var body = new StringBuilder();
                while (data != null && !data.StartsWith(bounderInner))
                {
                    body.AppendLine(data);
                    data = reader.ReadLine(ref maxLength, Encoding);
                }
                // check for nested part
                string nestedboundary = a.Headers.GetBoundary();
                if (!string.IsNullOrEmpty(nestedboundary))
                {
                    ParseMime(body.ToString(), nestedboundary);
                }
                else // nested
                {
                    a.SetBody(body.ToString());
                    (a.IsAttachment ? Attachments : AlternateViews).Add(a);
                }
            }

            if (maxLength > 0)
            {
                data = reader.ReadToEnd(maxLength, Encoding);
            }
        }
Пример #2
0
        private static void ParseMime(Stream reader, string boundary, ref int maxLength, ICollection <Attachment> attachments, Encoding encoding, char?termChar)
        {
            var    maxLengthSpecified = maxLength > 0;
            string data,
                   bounderInner = "--" + boundary,
                   bounderOuter = bounderInner + "--";

            do
            {
                data = reader.ReadLine(ref maxLength, encoding, termChar);
            } while (data != null && !data.StartsWith(bounderInner));

            while (data != null && !data.StartsWith(bounderOuter) && !(maxLengthSpecified && maxLength == 0))
            {
                data = reader.ReadLine(ref maxLength, encoding, termChar);
                var a = new Attachment {
                    Encoding = encoding
                };

                var part = new StringBuilder();
                // read part header
                while (!data.StartsWith(bounderInner) && data != string.Empty && !(maxLengthSpecified && maxLength == 0))
                {
                    part.AppendLine(data);
                    data = reader.ReadLine(ref maxLength, encoding, termChar);
                }
                a.RawHeaders = part.ToString();
                // header body

                // check for nested part
                var nestedboundary = a.Headers.GetBoundary();
                if (!string.IsNullOrEmpty(nestedboundary))
                {
                    ParseMime(reader, nestedboundary, ref maxLength, attachments, encoding, termChar);
                }
                else
                {
                    data = reader.ReadLine(ref maxLength, a.Encoding, termChar);
                    var body = new StringBuilder();
                    while (!data.StartsWith(bounderInner) && !(maxLengthSpecified && maxLength == 0))
                    {
                        body.AppendLine(data);
                        data = reader.ReadLine(ref maxLength, a.Encoding, termChar);
                    }
                    a.SetBody(body.ToString());
                    attachments.Add(a);
                }
            }
        }
Пример #3
0
        private void ParseMime(TextReader reader, string boundary)
        {
            string data,
                   bounderInner = "--" + boundary,
                   bounderOuter = bounderInner + "--";

            do
            {
                data = reader.ReadLine();
            } while (data != null && !data.StartsWith(bounderInner));

            while (data != null && !data.StartsWith(bounderOuter))
            {
                data = reader.ReadLine();
                var a = new Attachment();

                var part = new StringBuilder();
                // read part header
                while (!data.StartsWith(bounderInner) && data != string.Empty)
                {
                    part.AppendLine(data);
                    data = reader.ReadLine();
                }
                a.RawHeaders = part.ToString();
                // header body

                data = reader.ReadLine();
                var body = new StringBuilder();
                while (data != null && !data.StartsWith(bounderInner))
                {
                    body.AppendLine(data);
                    data = reader.ReadLine();
                }
                // check for nested part
                string nestedboundary = a.Headers.GetBoundary();
                if (!string.IsNullOrEmpty(nestedboundary))
                {
                    using (var nestedReader = new System.IO.StringReader(body.ToString()))
                        ParseMime(nestedReader, nestedboundary);
                }
                else // nested
                {
                    a.SetBody(body.ToString());
                    (a.IsAttachment ? Attachments : AlternateViews).Add(a);
                }
            }
        }
Пример #4
0
        private void ParseMime(string body, string boundary)
        {
            string bounderInner = "--" + boundary,
                   bounderOuter = bounderInner + "--";

            var attachment = body.Split('\n')
                             // start at body skip blank lines at the top
                             .SkipWhile(x => !x.Trim().StartsWith(bounderInner))
                             // get every line until end where boundary outer is
                             .TakeWhile(x => !x.Trim().StartsWith(bounderOuter));

            // parse headers at top of body
            var bodyStart = 0;

            while (attachment.SkipWhile((s, i) => i <= bodyStart).Any(x => x.Trim().StartsWith(bounderInner)))
            {
                var a = new Attachment {
                    Encoding = Encoding
                };

                a.RawHeaders = String.Join(Environment.NewLine, attachment
                                           // skip the first line which we already know is a boundaryInner
                                           .SkipWhile((s, i) => i <= bodyStart || ((s.Trim().Length == 0 || s.Trim().StartsWith(bounderInner)) && (++bodyStart) >= 0))
                                           // take all the lines until another boundaryInner is hit
                                           .TakeWhile((x, i) => x.Trim().Length > 0 && !x.Trim().StartsWith(bounderInner) && (++bodyStart) >= 0));

                // get message between headers and ending boundary
                var message = String.Join(Environment.NewLine, attachment
                                          // skip the headers and the first line which is a boundary inner
                                          .SkipWhile((s, i) => i <= bodyStart || (s.Trim().Length == 0 && (++bodyStart) >= 0))
                                          // take everything else
                                          .TakeWhile(x => x != string.Empty && !x.Trim().StartsWith(bounderInner) && (++bodyStart) >= 0));

                // check for nested part
                string nestedboundary = a.Headers.GetBoundary();
                if (!string.IsNullOrEmpty(nestedboundary))
                {
                    ParseMime(message, nestedboundary);
                }
                else
                {
                    // nested
                    a.SetBody(message);
                    (a.IsAttachment ? Attachments : AlternateViews).Add(a);
                }
            }
        }
Пример #5
0
        private static string ParseMime(Stream reader, string boundary, ref int maxLength, ICollection <Attachment> attachments, Encoding encoding, char?termChar, ref string data, string parentBoundary)
        {
            var maxLengthSpecified = maxLength > 0;
            var bounderInner       = "--" + boundary;
            var bounderOuter       = bounderInner + "--";
            var n = 0;
            //var content = reader.ReadToEnd(maxLength, encoding);
            var body = new System.Text.StringBuilder();

            do
            {
                if (maxLengthSpecified && maxLength <= 0)
                {
                    return(body.ToString());
                }
                if (data != null)
                {
                    body.Append(data);
                }
                data = reader.ReadLine(ref maxLength, encoding, termChar);
                n++;
            } while (data != null &&
                     !data.StartsWith(bounderInner) &&
                     !(!string.IsNullOrEmpty(parentBoundary) &&
                       data.StartsWith(parentBoundary)));

            while (data != null &&
                   !data.StartsWith(bounderOuter) &&
                   !(maxLengthSpecified && maxLength == 0) &&
                   !(!string.IsNullOrEmpty(parentBoundary) &&
                     data.StartsWith(parentBoundary)))
            {
                data = reader.ReadLine(ref maxLength, encoding, termChar);
                if (data == null)
                {
                    break;
                }
                var a = new Attachment {
                    Encoding = encoding
                };

                var part = new StringBuilder();
                // read part header
                while (!data.StartsWith(bounderInner) && data != string.Empty && !(maxLengthSpecified && maxLength == 0))
                {
                    part.AppendLine(data);
                    data = reader.ReadLine(ref maxLength, encoding, termChar);
                    if (data == null)
                    {
                        break;
                    }
                }
                a.RawHeaders = part.ToString();
                // header body

                // check for nested part
                var nestedboundary = a.Headers.GetBoundary();
                if (!string.IsNullOrEmpty(nestedboundary))
                {
                    ParseMime(reader, nestedboundary, ref maxLength, attachments, encoding, termChar, ref data, bounderInner);
                    while (!data.StartsWith(bounderInner) && !(maxLengthSpecified && maxLength == 0))
                    {
                        data = reader.ReadLine(ref maxLength, encoding, termChar);
                    }
                }
                else
                {
                    data = reader.ReadLine(ref maxLength, a.Encoding, termChar);
                    if (data == null)
                    {
                        break;
                    }
                    var nestedBody = new StringBuilder();
                    while (!data.StartsWith(bounderInner) && !(maxLengthSpecified && maxLength == 0))
                    {
                        nestedBody.AppendLine(data);
                        data = reader.ReadLine(ref maxLength, a.Encoding, termChar);
                    }
                    a.SetBody(nestedBody.ToString());
                    attachments.Add(a);
                }
            }
            return(body.ToString());
        }