This project is read-only.

Get message without attachments

Jul 17, 2014 at 12:26 PM
Edited Jul 17, 2014 at 1: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.
Jul 18, 2014 at 2: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.

Jul 18, 2014 at 4:48 AM
Ok, I will wait this important function.
Jul 18, 2014 at 6: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)
                            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)
                        { }

Jul 23, 2014 at 12:24 AM
Thank you KooL_ru!

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

Aug 1, 2014 at 9: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.

Aug 1, 2014 at 4: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.

Aug 2, 2014 at 12:46 AM
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.

Marked as answer by MrRobespierre on 8/1/2014 at 6:09 PM
Aug 2, 2014 at 2:27 AM
Excellent! I liked this method!
Sep 4, 2014 at 12:19 PM
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)
Sep 8, 2014 at 1:36 AM
Hi, KooL_ru!

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

Sep 8, 2014 at 7:45 AM

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