Version: 3.2.1.0
Send, receive, edit, sign/verify and encrypt/decrypt email messages in any .NET application or service. 

Get Messages With IMAP Code Example

The following example demonstrates getting specific messages from a mail server using the PowerTCP Imap component. Progress is provided while messages are downloaded.

< Back to all samples for PowerTCP Mail for .NET

private void getMessages_Click(object sender, EventArgs e)
{
    try
    {
        //Login to the IMAP server.
        imap1.Login("myMailServer""myAccount""myPassword");

        //The "INBOX" is the default mailbox.
        //Get headers for messages in the INBOX.
        Mailbox box = imap1.CurrentMailbox;
        box.Get(box.Messages[0], box.Messages[box.Messages.Count - 1],
            ImapMessageSections.header);
        if (imap1.CurrentMailbox.Messages.Count > 0)
        {
            //Get all messages from a specified address and add the
            //Subject line to a list.
            char[] brackets = { '<''>' };
            foreach (ImapMessage im in box.Messages)
            {
                if (im.Message.From.Address.Trim(brackets) == "important@gmail.com")
                {
                    im.Get();
                    textList.AppendText(im.Message.Subject + Environment.NewLine);
                }
            }
        }

        //Logout
        imap1.Logout();
        MessageBox.Show("DONE!");
    }
    catch (Exception ex)
    {
        //Show error messages.
        MessageBox.Show("Error: " + ex.Message);
    }
}

private void imap1_Progress(object sender, ImapProgressEventArgs e)
{
    //Show progress of each message as it is downloaded.
    if (e.Length > 0)
    {
        progressBarMessage.Maximum = 100;
        progressBarMessage.Value = (int)((e.Position * 100) / e.Length);
    }
}