Send Email in C#, VB.NET, and ASP.NET

The following example shows how you can send an email with a basic body from your Console application by using C# or VB.NET code.

The emails are sent using an SMTP server, a custom server, or a server from a free email provider like Gmail, Outlook, Yahoo, etc.

Last, you'll also see examples of sending emails from ASP.NET applications.

using GemBox.Email;
using GemBox.Email.Smtp;

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

        // Create new email message.
        MailMessage message = new MailMessage(
            new MailAddress("sender@example.com", "Sender"),
            new MailAddress("first.receiver@example.com", "First receiver"),
            new MailAddress("second.receiver@example.com", "Second receiver"));

        // Add additional receivers.
        message.Cc.Add(
            new MailAddress("third.receiver@example.com", "Third receiver"),
            new MailAddress("fourth.receiver@example.com", "Fourth receiver"));

        // Add subject and body.
        message.Subject = "Send Email in C# / VB.NET / ASP.NET";
        message.BodyText = "Hi 👋,\n" +
            "This message was created and sent with GemBox.Email.\n" +
            "Read more about it on https://www.gemboxsoftware.com/email";

        // Create new SMTP client and send an email message.
        using (SmtpClient smtp = new SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)"))
        {
            smtp.Connect();
            smtp.Authenticate("<USERNAME>", "<PASSWORD>");
            smtp.SendMessage(message);
        }
    }
}
Imports GemBox.Email
Imports GemBox.Email.Smtp

Module Program

    Sub Main()

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

        ' Create new email message.
        Dim message As New MailMessage(
            New MailAddress("sender@example.com", "Sender"),
            New MailAddress("first.receiver@example.com", "First receiver"),
            New MailAddress("second.receiver@example.com", "Second receiver"))

        ' Add additional receivers.
        message.Cc.Add(
            New MailAddress("third.receiver@example.com", "Third receiver"),
            New MailAddress("fourth.receiver@example.com", "Fourth receiver"))

        ' Add subject and body.
        message.Subject = "Send Email in C# / VB.NET / ASP.NET"
        message.BodyText = "Hi 👋," & vbLf &
            "This message was created and sent with GemBox.Email." & vbLf &
            "Read more about it on https://www.gemboxsoftware.com/email"

        ' Create new SMTP client and send an email message.
        Using smtp As New SmtpClient("<ADDRESS> (e.g. smtp.gmail.com)")
            smtp.Connect()
            smtp.Authenticate("<USERNAME>", "<PASSWORD>")
            smtp.SendMessage(message)
        End Using

    End Sub
End Module

The above code creates a simple email that is represented with MailMessage object.

It has a plain text body, and it's sent using the SmtpClient to multiple recipients, which can be specified in MailMessage.To, MailMessage.Cc and MailMessage.Bcc address collections.

Screenshot of generated plain text email
Sending a basic email in C# and VB.NET

The SmtpClient.SendMessage method sends the MailMessage to the SMTP server for delivery, and the SMTP server will immediately send that email to its recipients.

If you'd like to know how you can create advance emails, which include HTML body, attachment and images, then check the Html With Attachment example.

Using an SMTP Server Like Gmail, Outlook and Yahoo

Every SMTP server has a different server address, and you'll need to find out what your email server's address is before you can use it from your C# and VB.NET code.

To connect an SMTP server with your .NET application, you need to create the SmtpClient object and provide it the address of your outgoing mail server.

There are many free email providers that you can use to send an email. Here is the summary of SMTP server settings for some commonly used free email services:

Provider Host Port ConnectionSecurity
Gmail smtp.gmail.com 465 SSL
Outlook smtp-mail.outlook.com 587 STARTTLS
Office 365 smtp.office365.com 587 STARTTLS
Yahoo smtp.mail.yahoo.com 465 SSL
AOL smtp.aol.com 465 SSL

Note, you can reuse the same instance of SmtpClient to send multiple emails. However, in case of an ASP.NET application, we encourage you to recreate the client for each request, as shown in the next examples.

Sending email in ASP.NET MVC

public class ExampleController : Controller
{
    public ActionResult SendEmail()
    {
        var host = "<SMTP SERVER DOMAIN>";
        var port = 0;
        var security = GemBox.Email.Security.ConnectionSecurity.Auto;
        System.Net.Security.RemoteCertificateValidationCallback ignoreCertificate = (sender, certificate, chain, errors) => true;

        var username = "<USERNAME>";
        var password = "<PASSWORD>";

        //  Set the SMTP host, port and connection security.
        using (var smtp = new SmtpClient(host, port, security, ignoreCertificate))
        {
            //  Connect and sign to the SMTP server.
            smtp.Connect();
            smtp.Authenticate(username, password);

            // Create and send a new email.
            var message = new MailMessage();
            // ...
            smtp.SendMessage(message);
        }

        return new EmptyResult();
    }
}
Public Class ExampleController
    Inherits Controller

    Function SendEmail() As ActionResult
        Dim host = "<SMTP SERVER DOMAIN>"
        Dim port = 0
        Dim security = GemBox.Email.Security.ConnectionSecurity.Auto
        Dim ignoreCertificate As System.Net.Security.RemoteCertificateValidationCallback = Function(sender, certificate, chain, errors) True

        Dim username = "<USERNAME>"
        Dim password = "<PASSWORD>"

        '  Set the SMTP host, port and connection security.
        Using smtp As New SmtpClient(host, port, security, ignoreCertificate)
            '  Connect and sign to the SMTP server.
            smtp.Connect()
            smtp.Authenticate(username, password)

            ' Create and send a new email.
            Dim message As New MailMessage()
            ' ...
            smtp.SendMessage(message)
        End Using

        Return New EmptyResult()
    End Function

End Class

Sending email in ASP.NET Web Forms

public partial class ExamplePage : Page
{
    protected void SendEmailButton_Click(object sender, EventArgs e)
    {
        //  Create SMTP client.
        using (var smtp = new SmtpClient("<HOST>"))
        {
            //  Connect and sign to the SMTP server.
            smtp.Connect();
            smtp.Authenticate("<USERNAME>", "<PASSWORD>");

            // Create and send a new email.
            var message = new MailMessage();
            // ...
            smtp.SendMessage(message);
        }
    }
}
Public Class ExamplePage
    Inherits Page

    Protected Sub SendEmailButton_Click(sender As Object, e As EventArgs) Handles SendEmailButton.Click
        ' Create SMTP client.
        Using smtp As New SmtpClient("<HOST>")

            ' Connect and sign to the SMTP server.
            smtp.Connect()
            smtp.Authenticate("<USERNAME>", "<PASSWORD>")

            ' Create and send a new email.
            Dim message As New MailMessage()
            ' ...
            smtp.SendMessage(message)
        End Using
    End Sub
End Class

Check next example or download examples from GitHub.