HOWTO: Get the list of unread messages from an IMAP server in VB.NET

  |   Martin Vobr

Summary

This post shows how to connect to an IMAP server and retrieve the list of unread messages. The code uses Rebex Mail for .NET component.

Searching for unread messages

One of the most common mail processing operations is getting the list of new messages. When using the IMAP protocol, you have to select the correct mail folder (or "mailbox" in the IMAP terminology), construct the appropriate query and search for messages. Don't worry - with Rebex Mail, you don't have to learn the complicated IMAP query language or do your own modified-UTF-7-encoding :-)

The following code illustrates the whole process - it searches for messages marked as "not seen" in the selected folder at the IMAP server:

' find the unread messages  
Dim messages As New ImapMessageCollection         
messages = client.Search( _
    ImapSearchParameter.HasFlagsNoneOf(ImapMessageFlags.Seen ) _ 
) 
Console.WriteLine ("Found {0} unread messages.", messages.Count)

' list the unread messages: Dim message As ImapMessageInfo 
For Each message In messages
    Console.WriteLine( _
        "{0} | {1} | {2} | {3}", _
        message.UniqueID, _
        message.From, _
        message.To, _
        message.Subject) 
Next 

More searching options

The search method is of course not limited to seen/unseen messages. To learn more about searching IMAP folders, please see our IMAP Search Tutorial. The Following table illustrates possible search parameters:

Search parameter Description
From(address) Messages that contain the specified string in their From field.
NotFrom Messages that do not contain the specified string in their From field.
To(address) Messages that contain the specified string in their To field.
NotTo Messages that do not contain the specified string in their To field.
CC(address) Messages that contain the specified string in their CC field.
NotCC Messages that do not contain the specified string in their CC field.
Bcc(address) Messages that contain the specified string in their BCC field.
NotBcc Messages that do not contain the specified string in their BCC field.
Subject(queryTerm) Messages that contain the specified string in their subject field.
Body(queryTerm) Messages that contain the specified string in their body.
FullText(queryTerm) Messages that contain the specified string in their headers or body.
Arrived(on) Messages that arrived on the specified date (disregarding time).
Arrived(since, before) Messages that arrived in the specified date interval (disregarding time).
Sent(on) Messages that were sent on the specified date (disregarding time).
Sent(since, before) Messages that were sent in the specified date interval (disregarding time).
HasFlagsAllOf(flags) Messages with all the specified flags set.
HasFlagsNoneOf(flags) Messages with none of the specified flags set.
Deleted Messages whose Deleted flag is set.
New Messages whose Recent flag is set and Seen flag not set.
Recent Messages whose Recent flag is set.
NotRecent Messages whose Recent flag is not set.
Header(headerName, queryTerm) Messages that contain the specified string in the specified header.
Size(min, max) Messages whose size within the specified interval.
All Search for all messages. Same as GetMessageList method.

Full code

Let's see the full source code:

Imports Microsoft.VisualBasic 
Imports System 
Imports System.Collections 
Imports Rebex.Mail 
Imports Rebex.Net

Public Module MyModule
    Public Sub Main
        Dim server As String = "myServer"
        Dim username As String = "myUser"
        Dim password As String = "myPassword"

        ' create client, connect and log in 
        Dim client As New Imap
        client.Connect(server)
        client.Login(username, password)

        ' select the folder for search operation 
        client.SelectFolder("Inbox")

        ' find unread messages 
        Dim messages As New ImapMessageCollection        
        messages = client.Search( _
            ImapSearchParameter.HasFlagsNoneOf(ImapMessageFlags.Seen ) _
        )

        Console.WriteLine ("Found {0} unread messages.", messages.Count)

        ' list unread messages:
        Dim message As ImapMessageInfo
        For Each message In messages
            Console.WriteLine( _
                "{0} | {1} | {2} | {3}", _
                message.UniqueID, _
                message.From, _
                message.To, _
                message.Subject)
        Next                            
    End Sub
End Module