SMTP Client in C# and VB.NET

The Simple Mail Transfer Protocol (SMTP) is the only standard protocol for sending mail messages over the Internet.

GemBox.Email enables you to work with the SMTP protocol in C# and VB.NET using an SmtpClient class. To learn about how to send an email and how to connect with some common SMTP servers (like Gmail, Outlook, Yahoo, etc.), visit our Send example.

The following example shows how you can create a new SmtpClient and use it to connect to, authenticate and disconnect from a remote SMTP server.

Using an SMPT client in C# and VB.NET
Screenshot of SmtpClient's output
using System;
using GemBox.Email;
using GemBox.Email.Smtp;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // Create new SMTP client.
        using (var smtp = new SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)"))
        {
            // By default the connect timeout is 5 sec.
            smtp.ConnectTimeout = TimeSpan.FromSeconds(4);

            // Connect to SMTP server.
            smtp.Connect();
            Console.WriteLine("Connected.");

            // Authenticate using the credentials; username and password.
            smtp.Authenticate("<USERNAME>", "<PASSWORD>");
            Console.WriteLine("Authenticated.");
        }

        Console.WriteLine("Disconnected.");
    }
}
Imports System
Imports GemBox.Email
Imports GemBox.Email.Smtp

Module Program

    Sub Main()

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

        ' Create new SMTP client.
        Using smtp As New SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)")

            ' By default the connect timeout is 5 sec.
            smtp.ConnectTimeout = TimeSpan.FromSeconds(4)

            ' Connect to SMTP server.
            smtp.Connect()
            Console.WriteLine("Connected.")

            ' Authenticate using the credentials; username and password.
            smtp.Authenticate("<USERNAME>", "<PASSWORD>")
            Console.WriteLine("Authenticated.")
        End Using

        Console.WriteLine("Disconnected.")
    End Sub
End Module

Even though the SMTP protocol was defined in RFC 5321 more than 30 years ago, it is still used today, mainly due to its simplicity.

You can reuse the same connection that's established by the SmtpClient object and your email server multiple times.

When calling the SmtpClient.Authenticate method, the strongest possible password-based authentication mechanism will be used from the SmtpClient.SupportedAuthentications collection.

To disconnect from the email server, you can use either the SmtpClient.Disconnect or SmtpClient.Dispose method.

Last, SmtpClient utilizes the following protocol's extensions:

See also


Next steps

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

Download Buy