How to send SMTP email using MailKit

Programming, error messages and sample code > sample code
NOTICE: The trial account does not include SMTP service, preventing testing within the trial period to deter spammers from exploiting our Free Trial Service.
 
For your web applications, use the assigned SMTP server "mail.yourdomain.com" as the outgoing server (Replace yourdomain.com with your own domain name).
 
To set up your SMTP server, please follow the steps below:
  1. Log in to your control panel.
  2. Ensure 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
<%@ 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 or 587
        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>