Example #1
0
        public static HeaderCollection Parse(string headers)
        {
            headers = Utilities.DecodeWords(headers);
            var    temp  = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            var    lines = headers.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            int    i;
            string key = null, value;

            foreach (var line in lines)
            {
                if (key != null && (line[0] == '\t' || line[0] == ' '))
                {
                    temp[key] += line.Trim();
                }
                else
                {
                    i = line.IndexOf(':');
                    if (i > -1)
                    {
                        key   = line.Substring(0, i).Trim();
                        value = line.Substring(i + 1).Trim();
                        temp.Set(key, value);
                    }
                }
            }

            var result = new HeaderCollection();

            foreach (var item in temp)
            {
                result.Add(item.Key, new HeaderValue(item.Value));
            }
            return(result);
        }
Example #2
0
        public void Load(string message, bool headersonly)
        {
            Raw          = message;
            _HeadersOnly = headersonly;
            if (headersonly)
            {
                RawHeaders = message;
            }
            else
            {
                var headers = new StringBuilder();
                using (var reader = new System.IO.StringReader(message)) {
                    string line;
                    do
                    {
                        line = reader.ReadLine();
                        headers.AppendLine(line);
                    } while (line != string.Empty);
                    RawHeaders = headers.ToString();

                    string boundary = Headers.GetBoundary();
                    if (!string.IsNullOrEmpty(boundary))
                    {
                        //else this is a multipart Mime Message
                        ParseMime(reader, boundary);
                    }
                    else
                    {
                        _Body = reader.ReadToEnd();
                    }
                }
            }

            Date      = Headers.GetDate("Date");
            To        = Headers.GetAddresses("To");
            Cc        = Headers.GetAddresses("Cc");
            Bcc       = Headers.GetAddresses("Bcc");
            Sender    = Headers.GetAddresses("Sender").FirstOrDefault();
            ReplyTo   = Headers.GetAddresses("Reply-To").FirstOrDefault();
            From      = Headers.GetAddresses("From").FirstOrDefault();
            MessageID = Headers["Message-ID"].RawValue;

            Importance = Headers.GetEnum <MailPriority>("Importance");
            Subject    = Utilities.DecodeWords(Headers["Subject"].RawValue);
        }
        public static HeaderDictionary Parse(string headers, System.Text.Encoding encoding)
        {
            headers = Utilities.DecodeWords(headers, encoding);
            var    temp = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
            var    lines = headers.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            string key = null, value;

            foreach (var line in lines)
            {
                if (key != null && (line[0] == '\t' || line[0] == ' '))
                {
                    temp[key] += line.TrimStartOnce();
                }
                else
                {
                    if (key != null)
                    {
                        temp[key] = temp[key].TrimEndOnce(); // It trims the last line of the previous key
                    }
                    var index = line.IndexOf(':');
                    if (index > -1)
                    {
                        key   = line.Substring(0, index).TrimStartOnce();
                        value = line.Substring(index + 1).TrimStartOnce();
                        temp.Set(key, value);
                    }
                }
            }

            var result = new HeaderDictionary();

            foreach (var item in temp)
            {
                result.Add(item.Key, new HeaderValue(item.Value));
            }

            return(result);
        }