MailKit icon indicating copy to clipboard operation
MailKit copied to clipboard

Read Inbox Body Fast

Open nik0s100 opened this issue 7 years ago • 3 comments

So I'm using ImapClient to Connect into gmail and read all messages as HtmlBody. But it takes too long to read about 2000 emails. Is there any possibility to read it faster? I used MultiThread but it doesn't allow it needs SyncRoot Lock..So is the same speed. Any way to read fast HtmlBody of the Inbox?

var client = new ImapClient();
client.ServerCertificateValidationCallback = (s, c, h, a) => true;
client.Connect(imap, Convert.ToInt32(port), true);
client.Authenticate(email, pass);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadWrite);
var items = inbox.Fetch(0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.Size | MessageSummaryItems.Flags);
var messages = new ConcurrentQueue<IMessageSummary>(items);
for (int i = 0; i < 50; i++)
    new Thread(new ThreadStart(() => readMessageAsync(inbox))).Start();

nik0s100 avatar Jun 29 '19 20:06 nik0s100

I would recommend changing your code to this:

var client = new ImapClient();
client.ServerCertificateValidationCallback = (s, c, h, a) => true;
client.Connect(imap, Convert.ToInt32(port), true);
client.Authenticate(email, pass);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadWrite);
var items = inbox.Fetch(0, -1, MessageSummaryItems.UniqueId | MessageSummaryItems.Size | MessageSummaryItems.Flags | MessageSummaryItems.BodyStructure);
foreach (var item in items) {
    var part = item.HtmlBody ?? item.TextBody;
    var body = (TextPart) inbox.GetBodyPart (item.UniqueId, part);
    var text = body.Text;
}

jstedfast avatar Jul 01 '19 19:07 jstedfast

Yeah but this way is slow also. No other way to read it faster right?

nik0s100 avatar Jul 01 '19 23:07 nik0s100

The method I posted above is as fast as you can get unless you open multiple connections to the IMAP server to share the load in downloading the html bodies.

jstedfast avatar Jul 02 '19 16:07 jstedfast