1

I'm tying to use MailSystem.NET to read ALL mail (except for stuff in Drafts, I guess) and extract the UUID, the Date, the Sender, all Recipient emails.

I would like to have it fetch, beginning at the most recent and continue scanning backwards, loading batches of maybe 100 headers at a time.

If new emails come in during this operation, I don't want the message index change to affect the progress.

I'd like some advice on this. It seems to me that the Fetch() wrappers work on individual messages and that inbox search functionality gives message index ordinals rather than UIDs. I believe UIDs would be more robust to concurrent activity.

I can call imap.Command("fetch 1:100 (uid rfc822.header)") but then I don't know how to use MailSystem.NET to parse the results.

Also, is there a way to say "get the next 100 messages with UID less than the last seen UID"? If it is safe to assume that UIDs always increase with later messages. I mean this to be an alternative to basing it on the message index ordinal.

Thanks!

jstedfast
  • 35,744
  • 5
  • 97
  • 110
Jason Kleban
  • 20,024
  • 18
  • 75
  • 125

1 Answers1

1

I can't answer your question using MailSystem.NET, but I can answer this question using a far better C# IMAP library: MailKit.

using System;

using MailKit.Net.Imap;
using MailKit;
using MimeKit;

namespace TestClient {
    class Program
    {
        public static void Main (string[] args)
        {
            using (var client = new ImapClient ()) {
                client.Connect ("imap.friends.com", 993, true);

                // Note: since we don't have an OAuth2 token, disable
                // the XOAUTH2 authentication mechanism.
                client.AuthenticationMechanisms.Remove ("XOAUTH");

                client.Authenticate ("joey", "password");

                // The Inbox folder is always available on all IMAP servers...
                client.Inbox.Open (FolderAccess.ReadOnly);

                // Note: The envelope contains the date and all of the email addresses
                var items = MessageSummaryItems.UniqueId | MessageSummaryItems.Envelope;
                int upper = client.Inbox.Count - 1;
                int lower = Math.Max (upper - 100, 0);

                while (lower <= upper) {
                    foreach (var message in client.Inbox.Fetch (lower, upper, items)) {
                        Console.WriteLine ("Sender: {0}", message.Envelope.Sender);
                        Console.WriteLine ("From: {0}", message.Envelope.From);
                        Console.WriteLine ("To: {0}", message.Envelope.To);
                        Console.WriteLine ("Cc: {0}", message.Envelope.Cc);
                        if (message.Envelope.Date.HasValue)
                            Console.WriteLine ("Date: {0}", message.Envelope.Date.Value);
                    }

                    upper = lower - 1;
                    lower = Math.Max (upper - 100, 0);
                }

                client.Disconnect (true);
            }
        }
    }
}
jstedfast
  • 35,744
  • 5
  • 97
  • 110