andyedinborough / aenetmail

C# POP/IMAP Mail Client
370 stars 153 forks source link

HeaderDictionary does not allow duplicate Headers #128

Open jvdvleuten opened 11 years ago

jvdvleuten commented 11 years ago

When there is a duplicate Header, e.g. Delivered-To, the last header is taken.

Is there a reason that duplicate Headers are not all passed into the HeaderDictionary?

Is it not valid to have duplicate Headers in an email or was there another reason?

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);
            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 HeaderDictionary();
            foreach (var item in temp) {
                result.Add(item.Key, new HeaderValue(item.Value));
            }
            return result;
        }
jstedfast commented 10 years ago

It's perfectly valid for messages to have duplicate headers, especially for headers such as "Received:", "To:", and "Cc:".

I would highly recommend replacing the MIME parser in AENetMail with MimeKit as it properly handles this and many other cases.