Getting Started

Using the InterIMAP library is pretty straight forward. Just add a reference to InterIMAP.dll in your project, and you are ready to go.

Connecting

using InterIMAP; 


IMAPConfig config = new IMAPConfig("server","username","password", ssl (true | false), autlogin (true | false), "initial folder");
IMAPAsyncClient client = new IMAPAsyncClient(config, 5); // 5 represents the number of worker connections to create. Depending on your bandwidth this number might need to be adjusted for optimal performance
client.Start();


The client is now connected and awaiting requests. The first request that should be done is the FolderTreeRequest. This request will retrieve the list of folders in the account and generate the necessary IFolders for them. This is accomplished by executing this:

client.RequestManager.SubmitRequest(new FolderTreeRequest("\"\"", FolderTreeCallback), false);

private void FolderTreeCallback(IRequest req)
{
    // whatever logic you need to do to indicate the process has completed.
}


An alternative to using .SubmitRequest is to use SubmitAndWait, which will block the calling thread while the request executes. Be careful when using this method in applications that have a user interface as this will prevent the user from interacting with anything while the request is running.

Getting Messages

Once you have the folder tree populated, the next step is to execute the PopulateFolderData method which examines each folder and determines how many messages it contains and stores each message's UID. Once this is done, you are ready to start pulling down messages and examining their contents.

The most common operation is checking for new messages, which can easily be accomplished using the NewMessageRequest object like this:
NewMessageRequest nmr = new NewMessageRequest(client);
nmr.NewMessageRequestCompleted += new NewMessageRequestCompletedHandler(new_messages);
nmr.Start(); // checks for new messages in all folders
IFolder folder = client.MailboxManager.GetFolderByPath("INBOX");
nmr.Start(folder); // checks for new messages in the INBOX folder only

private void new_messages(IMessage[] msgs)
{
    // do what you need to do here
}

Last edited Apr 5, 2010 at 8:00 PM by JasonMiesionczek, version 2

Comments

Ronglath Aug 4, 2012 at 1:47 AM 
To clear the errors and to be able to call all object listed in code above i needed to include this ones also

using InterIMAP;
using InterIMAP.Asynchronous.Client;
using InterIMAP.Common.Requests;
using InterIMAP.Common.Interfaces;

PACCBET Aug 3, 2012 at 8:17 AM 
Try use the library : https://imapx.codeplex.com/

That library free, open source and have example at this : https://imapx.codeplex.com/wikipage?title=Sample%20code%20for%20get%20messages%20from%20your%20inbox

Good Luck :)

mslot May 13, 2011 at 4:38 PM 
Btw, I have added the client.Start() to my code, and it still does not work.

mslot May 13, 2011 at 4:27 PM 
Why cant I get this code to work?

static void Main()
{
IMAPConfig config = new IMAPConfig("mail.myhost.tl", "xxxx@xxx.xx", "xxxxxx", false, false, "\"\"");
IMAPAsyncClient client = new IMAPAsyncClient(config, 5);
client.RequestManager.SubmitAndWait(new FolderTreeRequest("\"\"", FolderTreeCallback), false);

NewMessagesRequest nmr = new NewMessagesRequest(client);
nmr.NewMessageRequestCompleted += new NewMessagesRequest.NewMessageRequestCompletedHandler(nmr_NewMessageRequestCompleted);
//nmr.Start(); // checks for new messages in all folders
IFolder folder = client.MailboxManager.GetFolderByPath("INBOX");
nmr.Start(folder); // checks for new messages in the INBOX folder only
}

private static void FolderTreeCallback(IRequest req)
{
// whatever logic you need to do to indicate the process has completed.
}

static void nmr_NewMessageRequestCompleted(IMessage[] messages)
{
throw new NotImplementedException();
}

? The only thing I want this code to do is to throw an exception, but I do not get that exception. Can you tell me why, Jason? Or refer my to some examples that retrieve new messages.

dalibor Jul 10, 2010 at 7:50 PM 
When I place this code, i have an error.

There is error which is fixed: NewMessageRequest , without S , NewMessageSRequest


I just want to pull new e-mails and to read their sybject and body. And then to sign them read.


Can you please put code for that??