Archived Forum Post

Index of archived forum posts


using IMAP-methods in multithreading operations

Feb 24 '14 at 17:02


I have an object of Imap class. I am trying to call the same (or not) methods of my Imap object in the same time in two different threads. Will all this methods return correct data when they complete?

For a example:

I want to get emails from mailbox:

public class ImapWrapper { private Imap _imap = new Imap();

// some methods for initializing Imap connection here

public Email[] GetMailboxMessages(string mailbox, int[] uids)

if (!_imap.SelectMailbox(mailbox)) return null;

List<email> messages = new List<email>();

foreach (int uid in uids) {

 Email email = _imap.FetchSingle(uid, true);

 if (email != null)


return messages.ToArray(); } }

In the main program I create an instance of ImapWrapper class:

... ImapWrapper wr = new ImapWrapper();

start in one thread wr.GetMessages("Inbox", new int[] {1, 2, 3})

start in another thread wr.GetMessages("UserFolder", new int[] {5, 6, 7})

Will results of two calls be correct?

Accepted Answer

For virtually any Internet protocol based on commands and responses, it would make no sense for multiple threads to be trying to send commands and receive responses on the same connection at the same time. Everything would be a mess -- commands and responses would be intermixed and it just doesn't make sense.

(This is not necessarily the case w/ SSH because of the concept of logical channels within a single connection. But for SMTP, HTTP, IMAP, POP3, FTP, etc. this fundamental restriction applies.)