Downloading Unread IMAP Mail Messages

To list and download unread mail messages with the Ultimate IMAP Mail component, you just need to pass the ImapCriterion.DontHaveFlags(ImapMessageFlags.Seen) search condition to the ListMessages method. The following code illustrates how to do so:

C#:

// IMAP server information.
const string serverName = "myserver";
const string user = "name@domain.com";
const string password = "mytestpassword";
const int port = 993;
const SecurityMode securityMode = SecurityMode.Implicit;
// Create a new instance of the ImapClient class.
ImapClient client = new ImapClient();
// Connect to the server.
client.Connect(serverName, port, securityMode);
// Login to the server.
client.Authenticate(user, password);
// Select 'INBOX' mailbox
client.Select("INBOX");
// Get the message list.
Console.WriteLine("Getting message list...");
ImapMessageCollection list = client.ListMessages(EnvelopeParts.UniqueId | EnvelopeParts.Size, ImapCriterion.DontHaveFlags(ImapMessageFlags.Seen));
// Get messages.
for (int i = 0; i < list.Count; i++)
{
   ImapMessage imapMessage = list[i];
   // Download the message to an instance of the MailMessage class.
   MailMessage msg = client.DownloadMailMessage(imapMessage.UniqueId);
   // Display some information about it.
   Console.WriteLine("Size: " + imapMessage.Size);
   Console.WriteLine("Number of attachments: " + msg.Attachments.Count);
   Console.WriteLine("Number of header name value pairs: " + msg.Headers.Count);
}
// Close the connection.
client.Disconnect();

VB.NET:

' IMAP server information.
Const serverName As String = "myserver"
Const user As String = "name@domain.com"
Const password As String = "mytestpassword"
Const port As Integer = 993
Const securityMode As SecurityMode = securityMode.Implicit
' Create a new instance of the ImapClient class.
Dim client As New ImapClient()
' Connect to the server.
client.Connect(serverName, port, securityMode)
' Login to the server.
client.Authenticate(user, password)
' Select 'INBOX' mailbox
client.Select("INBOX")
' Get the message list.
Console.WriteLine("Getting message list...")
Dim list As ImapMessageCollection = client.ListMessages(EnvelopeParts.UniqueId Or EnvelopeParts.Size, ImapCriterion.DontHaveFlags(ImapMessageFlags.Seen))
' Get messages.
For i As Integer = 0 To list.Count - 1
    Dim imapMessage As ImapMessage = list(i)
    ' Download the message to an instance of the MailMessage class.
    Dim msg As MailMessage = client.DownloadMailMessage(imapMessage.UniqueId)
    ' Display some information about it.
    Console.WriteLine("Size: " & imapMessage.Size)
    Console.WriteLine("Number of attachments: " & msg.Attachments.Count)
    Console.WriteLine("Number of header name value pairs: " & msg.Headers.Count)
Next i
' Close the connection.
client.Disconnect()