MailKit
MailKit copied to clipboard
Read Inbox Body Fast
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();
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;
}
Yeah but this way is slow also. No other way to read it faster right?
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.