Пример #1
0
        public static MimeMessage ParseMessage(Stream inputStream, HTTPHeaders headers, CancellationToken cancellationToken)
        {
            if (inputStream == null || inputStream == Stream.Null)
            {
                return(null);
            }

            var headerArray = headers.ToArray();

            if (IsMimeMessage(headerArray) == false)
            {
                return(null);
            }

            string headerString = String.Join("\r\n", headerArray.Select(h => $"{h.Name}: {h.Value}"));

            try
            {
                using (ChainedStream streamWithHeaders = new ChainedStream())
                {
                    streamWithHeaders.Add(new MemoryStream(Encoding.UTF8.GetBytes(headerString)), false);
                    streamWithHeaders.Add(inputStream, false);

                    var parser = new MimeKit.MimeParser(streamWithHeaders);

                    return(parser.ParseMessage(cancellationToken));
                }
            }
            catch (FormatException)
            {
                return(null);
            }
        }
Пример #2
0
        public static Dictionary <string, string> ToDictionary(this HTTPHeaders headers)
        {
            if (headers == null)
            {
                return(null);
            }

            var result = new Dictionary <string, string>();

            foreach (var item in headers.ToArray())
            {
                if (!result.ContainsKey(item.Name))
                {
                    result.Add(item.Name, item.Value);
                }
                else
                {
                    result[item.Name] += ";" + item.Value;
                }
            }

            return(result);
        }