Get supported server extensions (IMAP, POP3, SMTP)

Not every server supports same extensions. This post explains how to get custom extensions supported by the email server in .NET.

Even though protocol commands and responses are different between server types (IMAP, POP3, and SMTP), client API is very similar. Great care is taken to make it almost the same for all email protocols. You can use SupportedExtensions on Imap, Pop3 or Smtp class to retrieve extensions supported by the server.

There are 3 different classes that represent server extensions for each email protocol: ImapExtension, Pop3Extension and SmtpExtension. Those classes contain static properties with well-know extensions like: ImapExtension.Idle or ImapExtension.Sort or Pop3Extension.STLS etc. SupportedExtensions returns a list of them.

You can use SupportedExtensions method to retrieve all protocol extensions supported by the server.

Get supported IMAP server extensions

// C#

using (Imap client = new Imap())
{
    client.ConnectSSL("imap.example.org");
    client.UseBestLogin("user", "password");

    Console.WriteLine("Supported extensions:");

    foreach (ImapExtension extension in client.SupportedExtensions())
    {
        Console.WriteLine(extension.Name);
    }

    Console.WriteLine("Supports IDLE:");

    bool supportsIdle = client.SupportedExtensions()
        .Contains(ImapExtension.Idle);

    Console.WriteLine(supportsIdle);

    client.Close();
}

' VB.NET

Using client As New Imap()
    client.ConnectSSL("imap.example.org")
    client.UseBestLogin("user", "password")

    Console.WriteLine("Supported extensions:")

    For Each extension As ImapExtension In client.SupportedExtensions()
	    Console.WriteLine(extension.Name)
    Next

    Console.WriteLine("Supports IDLE:")

    Dim supportsIdle As Boolean = client.SupportedExtensions() _
        .Contains(ImapExtension.Idle)

    Console.WriteLine(supportsIdle)

    client.Close()
End Using

For example Gmail produces following output:

Supported extensions:
IMAP4rev1
UNSELECT
IDLE
NAMESPACE
QUOTA
ID
XLIST
CHILDREN
X-GM-EXT-1
UIDPLUS
COMPRESS

Supports IDLE:
True

We take great care for the API to look similar for all protocols (IMAP, POP3, SMTP).

Get supported POP3 server extensions

// C#

using (Pop3 client = new Pop3())
{
    client.ConnectSSL("pop3.example.org");
    client.UseBestLogin("user", "password");

    Console.WriteLine("Supported extensions:");

    foreach (Pop3Extension extension in client.SupportedExtensions())
    {
        Console.WriteLine(extension.Name);
    }

    Console.WriteLine("Supports TOP:");

    bool supportsTop= client.SupportedExtensions()
        .Contains(Pop3Extension.Top);

    Console.WriteLine(supportsTop);

    client.Close();
}

' VB.NET

Using client As New Pop3()
    client.ConnectSSL("pop3.example.org")
    client.UseBestLogin("user", "password")

    Console.WriteLine("Supported extensions:")

    For Each extension As Pop3Extension In client.SupportedExtensions()
	    Console.WriteLine(extension.Name)
    Next

    Console.WriteLine("Supports TOP:")

    Dim supportsTop As Boolean = client.SupportedExtensions() _
        .Contains(Pop3Extension.Top)

    Console.WriteLine(supportsTop)

    client.Close()
End Using

Get supported SMTP server extensions

// C#

using (Smtpclient = new Smtp())
{
    client.Connect("smtp.example.org");
    client.UseBestLogin("smtp", "password");

    Console.WriteLine("Supported extensions:");

    foreach (SmtpExtension  extension in client.SupportedExtensions())
    {
        Console.WriteLine(extension.Name);
    }

    Console.WriteLine("Supports STARTTLS:");

    bool supportsStartTLS = client.SupportedExtensions()
        .Contains(SmtpExtension.StartTLS);

    Console.WriteLine(supportsStartTLS);

    client.Close();
}

' VB.NET

Using client As New Smtp()
    client.Connect("smtp.example.org")
    client.UseBestLogin("user", "password")

    Console.WriteLine("Supported extensions:")

    For Each extension As SmtpExtension In client.SupportedExtensions()
	    Console.WriteLine(extension.Name)
    Next

    Console.WriteLine("Supports STARTTLS:")

    Dim supportsStartTLS As Boolean = client.SupportedExtensions() _
        .Contains(SmtpExtension.StartTLS)

    Console.WriteLine(supportsStartTLS)

    client.Close()
End Using

Tags: , , , ,

Questions?

Consider using our Q&A forum for asking any questions.