// We create Imap client

Imap4Client imap = new Imap4Client();

 

try

{

    // We connect to the imap4 server

    imap.Connect(_tbImap4Server.Text);

 

    this.AddLogEntry(string.Format("Connection to {0} successfully", _tbImap4Server.Text));

 

    // Login to mail box

    imap.Login(_tbUserName.Text, _tbPassword.Text);

 

    this.AddLogEntry(string.Format("Login to {0} successfully", _tbImap4Server.Text));

 

    Mailbox inbox = imap.SelectMailbox("inbox");

    if (inbox.MessageCount > 0)

    {

        imap.HeaderRetrieved += new HeaderRetrievedEventHandler(this.HeaderFetched);

 

        inbox.Fetch.BeginHeaderObject(1, null);

    }

 

    else

    {

        this.AddLogEntry("There is no message in the imap4 account");

    }

 

}

 

catch (Imap4Exception iex)

{

    this.AddLogEntry(string.Format("Imap4 Error: {0}", iex.Message));

}

 

catch (Exception ex)

{

    this.AddLogEntry(string.Format("Failed: {0}", ex.Message));

}

 

finally

{

    if (imap.IsConnected)

    {

        imap.Disconnect();

    }

}


public void HeaderFetched(object sender, HeaderRetrievedEventArgs e)

{

    Header header = e.Header;

    this.AddLogEntry(string.Format("Subject: {0} From :{1} ", header.Subject, header.From.Email));

}