Message.Sent return wrong value

Oct 6, 2008 at 8:48 AM
I test value of field on some free imap servers and always see default value from field Sent like 01.01.01. For fix update ImapCommands.cs method ParseMessages(ref ImapMailbox Mailbox).
Original code you may see in comments.

private void ParseMessages(ref ImapMailbox Mailbox)
        {
            string response = string.Empty;
            if (Mailbox.Messages == null)
                Mailbox.Messages = new List<ImapMailboxMessage>();
            do
            {
                response += Connection.Read();
            } while (!(response.EndsWith("))") || Regex.IsMatch(response, patFetchComplete)));
            if (response.StartsWith("*"))
            {
                do
                {
                    ImapMailboxMessage Message = new ImapMailboxMessage();
                    Message.Flags = new ImapMessageFlags();
                    Message.Addresses = new ImapAddressCollection();
                    Match match;
                    if ((match = Regex.Match(response, @"\* (\d*)")).Success)
                        Message.ID = Convert.ToInt32(match.Groups[1].ToString());
                    if ((match = Regex.Match(response, @"\(FLAGS \(([^\)]*)\)")).Success)
                        Message.Flags.ParseFlags(match.Groups[1].ToString());
                    if ((match = Regex.Match(response, @"INTERNALDATE ""([^""]+)""")).Success)
                        Message.Received = DateTime.Parse(match.Groups[1].ToString());
                    if ((match = Regex.Match(response, @"RFC822.SIZE (\d+)")).Success)
                        Message.Size = Convert.ToInt32(match.Groups[1].ToString());
                    if ((match = Regex.Match(response, @"ENVELOPE")).Success)
                        response = response.Remove(0, match.Index + match.Length);
                    // original:
                    // if ((match = Regex.Match(response, @"\(""([^""]+)""")).Success)
                    if ((match = Regex.Match(response, @"\(""(?:\w{3}\, )?([^""]+)""")).Success)
                    {
                        Match subMatch;
                        // original:
                        // subMatch = Regex.Match(match.Groups[1].ToString(), @"(-\d+.*|-\d+.*|NIL.*)"); //(-\d{4}|-\d{4}[^""]+|NIL)
                        subMatch = Regex.Match(match.Groups[1].ToString(), @"([\-\+]\d{4}.*|NIL.*)"); //(-\d{4}|-\d{4}[^""]+|NIL)
                        DateTime d;
                        DateTime.TryParse(match.Groups[1].ToString().Remove(subMatch.Index), out d);
                        Message.Sent = d;
                        Message.TimeZone = subMatch.Groups[1].ToString();
                        response = response.Remove(0, match.Index + match.Length);
                    }
                    Message.Subject = response.Substring(0, response.IndexOf("((")).Trim();
                    if (Message.Subject == "NIL")
                        Message.Subject = null;
                    else if ((match = Regex.Match(Message.Subject, "^\"(.*)\"$")).Success)
                        Message.Subject = match.Groups[1].ToString();
                    Message.Subject = ImapDecode.Decode(Message.Subject);
                    response = response.Remove(0, response.Substring(0, response.IndexOf("((")).Length);

                    //if ((match = Regex.Match(response, @"(""[^""]*"" \(\(|NIL)")).Success)
                    //{
                    //    Message.Subject = match.Groups[1].ToString();
                    //    if (Message.Subject == "NIL")
                    //        Message.Subject = null;
                    //    else if (Message.Subject.StartsWith("\""))
                    //        Message.Subject = Message.Subject.Substring(1, Message.Subject.Length -5);
                    //    response = response.Remove(0, match.Index + match.Length - 3);
                    //}
                    if ((match = Regex.Match(response, @"""<([^>]+)>""\)\)")).Success)
                    {
                        Message.MessageID = match.Groups[1].ToString();
                        response = response.Remove(match.Index).Trim();
                    }
                    if (response.EndsWith("NIL"))
                        response = response.Remove(response.Length - 3);
                    else {
                        match = Regex.Match(response, @"""<([^>]+)>""");
                        Message.Reference = match.Groups[1].ToString();
                    }
                    try
                    {
                        Message.Addresses = Message.Addresses.ParseAddresses(response);
                    }
                    catch (Exception ex)
                    {
                        Message.Errors = response + ex.ToString();
                    }
                    Mailbox.Messages.Add(Message);
                    response = string.Empty;
                    do
                    {
                        response += Connection.Read();
                    } while (!(response.EndsWith("))") || Regex.IsMatch(response, patFetchComplete)));
                } while (response.StartsWith("*"));

                //match = Regex.Match(response, @"\(FLAGS \(([\w\\]+)\) INTERNALDATE ""([^""]+)"" RFC822\.SIZE (\d+) ENVELOPE \(""([^""]+)"" ""([^""]+)"" \(\(NIL NIL ""([^""]+""\)\)");
            }
        }