GemBox.Email is a .NET component that enables you to read, write, receive, and send emails from your .NET applications using one simple API.

Search Emails in C# and VB.NET

One of the advanced IMAP features is a search command, which is supported by all IMAP servers regardless of their protocol version. Since search queries can be quite complex, GemBox.Email allows developers to construct custom queries according to RFC 3501 - section 6.4.4

The following example shows how to search for messages using the GemBox.Email component.

Screenshot
Search Screenshot

See the full code below.

using GemBox.Email;
using GemBox.Email.Imap;
using GemBox.Email.Mime;
using GemBox.Email.Security;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

class Sample
{
    [STAThread]
    static void Main(string[] args)
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        using (ImapClient imap = new ImapClient("<ADDRESS> (e.g. imap.gmail.com)"))
        {
            imap.Connect();
            Console.WriteLine("Connected.");

            imap.Authenticate("<USERNAME>", "<PASSWORD>");
            Console.WriteLine("Authenticated.");

            imap.SelectInbox();

            // Search for messages whose subject contains string 'Text'
            IList<int> messages = imap.SearchMessageNumbers("SUBJECT Test");
            Console.WriteLine("Number of messages with 'Test' string in subject: " + messages.Count);

            // Search for 'unseen' messages sent by 'sender@example.com'
            messages = imap.SearchMessageNumbers("UNSEEN FROM sender@example.com");
            Console.WriteLine("Number of unseen messages sent by 'sender@example.com': " + messages.Count);
        }
    }
}
Imports System.Globalization
Imports System.Net.Security
Imports System.Security.Cryptography.X509Certificates
Imports GemBox.Email
Imports GemBox.Email.Imap
Imports GemBox.Email.Mime

Module Samples

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        Using imap As New ImapClient("<ADDRESS> (e.g. imap.gmail.com)")

            imap.Connect()
            Console.WriteLine("Connected.")

            imap.Authenticate("<USERNAME>", "<PASSWORD>")
            Console.WriteLine("Authenticated.")

            imap.SelectInbox()

            ' Search for messages whose subject contains string 'Text'
            Dim messages As IList(Of Integer) = imap.SearchMessageNumbers("SUBJECT Test")
            Console.WriteLine("Number of messages with 'Test' string in subject: " & messages.Count)

            ' Search for 'unseen' messages sent by 'sender@example.com'
            messages = imap.SearchMessageNumbers("UNSEEN FROM sender@example.com")
            Console.WriteLine("Number of unseen messages sent by 'sender@example.com': " & messages.Count)

        End Using

    End Sub

End Module

Check next sample.