Get message without attachments

Jul 17, 2014 at 11:26 AM
Edited Jul 17, 2014 at 12:33 PM
Hi, how can I get message without attachments (only metadata and text messages) or only metadata (uid, from, to, subject, date, etc..). I don't want to download attachments, because they spend a lot of time. I use both protocols (POP3, IMAP). Thanks.
Coordinator
Jul 18, 2014 at 1:47 AM
Hi MrRobespierre!!

There are no way to get only header data in current version.
I'll create this feature in few weeks.
Please wait for next release for a moment.

regards.
Jul 18, 2014 at 3:48 AM
Ok, I will wait this important function.
Jul 18, 2014 at 5:54 AM
Hi, I use next code for get date and ID of message, but I modify original IMAP class (make Execute public)
            ImapCommandResult result = cl.Execute(
                String.Format("{2} FETCH {0}:{1} (BODY[HEADER.FIELDS (DATE Message-ID)])",
                    1, cl.CurrentFolder.MailCount,
                    "tag" + cl.TagNo)
                );
            String s = result.Text;

            Regex HeaderParse = new Regex("(?<key>[^:]*):[\\s]*(?<value>.*)\r\n");
            Regex messageRegex = new Regex(@"^\* (?<num>\d+) FETCH \([^\r\n]*\r\n(?<msg>.*?)\r\n\)"
                , RegexOptions.Multiline | RegexOptions.Singleline);

            MatchCollection mths = messageRegex.Matches(s);
            if (mths.Count > 0)
                foreach (Match m in mths)
                {
                    MessageKey key = new MessageKey();
                    key.Number = Convert.ToInt32(m.Groups["num"].Value);

                    s = m.Groups["msg"].Value;
                    MatchCollection hdr = HeaderParse.Matches(s);
                    foreach (Match h in hdr)
                    {
                        try
                        {
                            if (h.Groups["key"].ToString().ToLower().Contains("date"))
                                key.Date = DateTimeRfc2822.Parse(h.Groups["value"].ToString());
                            //key.Date = MailParser.ToDateTimeOffset(h.Groups["value"].ToString());
                            if (h.Groups["key"].ToString().ToLower().Contains("message-id"))
                            {
                                key.ID = h.Groups["value"].ToString();
                                key.ID = key.ID.Trim();
                            }
                        }
                        catch (Exception e)
                        { }
                    }

                }
Coordinator
Jul 22, 2014 at 11:24 PM
Thank you KooL_ru!

Your implementation is very helpful that I can reduce to create this feature.
Please wait for a week or so.

sincerely.
Coordinator
Aug 1, 2014 at 8:50 AM
Hi everybody!

I uploaded new version.
Please use GetHeaderCollections method of ImapClient object.
That returns List<MailHeaderCollection> object.

I'll release this version on Nuget next week.


regards.
Aug 1, 2014 at 3:19 PM
Dear Highty, I was download new version HigLabo, which contains this overloaded method, but I don't understand what to pass in parameters.

thanks.
Coordinator
Aug 1, 2014 at 11:46 PM
You can specify mail index that you want to get and header keys that you want to know.
var headers = cl.GetHeaderCollections(1, 20, "uid", "from", "to", "subject", "date");
I'll add a feature to get all header keys in next release if it can.


regards.
Marked as answer by MrRobespierre on 8/1/2014 at 6:09 PM
Aug 2, 2014 at 1:27 AM
Excellent! I liked this method!
Sep 4, 2014 at 11:19 AM
In latest source code I make next changes :

In HigLabo.Mail\Imap\ImapClient.cs
public List<MailHeaderCollection> GetHeaderCollections(Int32 startIndex, Int32 endIndex, params String[] headerKeys)

Replace headerKeys for String.Join(" ", headerKeys)
            var command = String.Format("{0} FETCH {1}:{2} (BODY[HEADER.FIELDS ({3})])"
                , this.Tag, startIndex, endIndex, String.Join(" ",  headerKeys));
and in private List<MailHeaderCollection> CreateMailHeaderCollections(String text)
            if (readingHeader == true)
            {
                if (line.EndsWith(")") == true)
Coordinator
Sep 8, 2014 at 12:36 AM
Hi, KooL_ru!

Thank you for your reply for patch.
I'll fix these method in next release.

regards.
Coordinator
Sep 8, 2014 at 6:45 AM
Hi.

I modified and upload new version.
Please check it out.