How to send SMTP email using MailKit

Programming, error messages and sample code > sample code
NOTICE: The trial account does not include SMTP service, which prevents testing during the trial period to prevent spammers from exploiting our Free Trial Service.
 
For your web applications, please use the assigned SMTP server "mail.yourdomain.com" as the outgoing server (replacing "yourdomain.com" with your own domain name).
 
To set up your SMTP server, please follow the steps outlined below:
  1. Please log in to your Control Panel.
  2. Ensure that a domain name is added to your account by navigating to Hosting Control Panel -> Website Domain Manager.
  3. After adding the domain name to your account, go to Hosting Control Panel -> Email Manager and activate your email service for the domain name.
  4. Create the necessary email accounts needed for your scripts. Your script MUST send from a valid Email account, so create the ones you need.
  5. Done! Follow the code sample below to send emails.
 
 
C# Code Sample without encryption:
<%@ Page Language="C#" AutoEventWireup="true" %>

<script language="C#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        // force TLS 1.2 connection if your application requires
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

        string server = "mail.yourDomain.com"; // replace it with your own domain name
        int port = 25; // alternative port is 8889
        bool enableSsl = false; // false for port 25 or 8889, true for port 465
        string from = "user@yourDomain.com"; // from address and SMTP username
        string password = "password"; // replace it with your real password
        string to = "user@email.com"; // recipient address

        var message = new MimeKit.MimeMessage();
        message.From.Add(new MimeKit.MailboxAddress("from_name", from)); // replace from_name with real name
        message.To.Add(new MimeKit.MailboxAddress("to_name", to)); // replace to_name with real name
        message.Subject = "This is an email";
        message.Body = new MimeKit.TextPart("plain")
        {
            Text = @"This is from MailKit.Net.Smtp using C sharp with SMTP authentication."
        };

        using (var client = new MailKit.Net.Smtp.SmtpClient())
        {
            client.Connect(server, port, enableSsl);
            client.Authenticate(from, password);
            client.Send(message);
            client.Disconnect(true);
        }

        Label1.Text = "Message sent";
    }
</script>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</body>
</html>
 
C# Code Sample with encryption:
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="MailKit.Security" %>

<script language="C#" runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        // force TLS 1.2 connection if your application requires
        System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;

        string server = "mail50xx.site4now.net"; // replace it with the actual one you see in your Control Panel > Emails
        int port = 587; // 587 for StartTls, 465 for SSL
        string from = "user@yourDomain.com"; // from address and SMTP username
        string password = "password"; // replace it with your real password
        string to = "user@email.com"; // recipient address

        var message = new MimeKit.MimeMessage();
        message.From.Add(new MimeKit.MailboxAddress("from_name", from)); // replace from_name with real name
        message.To.Add(new MimeKit.MailboxAddress("to_name", to)); // replace to_name with real name
        message.Subject = "This is an email";
        message.Body = new MimeKit.TextPart("plain")
        {
            Text = @"This is from MailKit.Net.Smtp using C sharp with SMTP authentication."
        };

        using (var client = new MailKit.Net.Smtp.SmtpClient())
        {
            // client.Connect(server, port, true); // for port 465
            client.Connect(server, port, SecureSocketOptions.StartTls) // for port 587
            client.Authenticate(from, password);
            client.Send(message);
            client.Disconnect(true);
        }

        Label1.Text = "Message sent";
    }
</script>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</body>
</html>