Casbay Knowledge Base

Search our articles or browse by category below

Send out emails using ASP script

Last updated: September 8, 2022
Estimated reading time: 2 min

How to send out emails using ASP script?

In Windows 2008, we prefer all users to send out email using CDOSYS mail component. This mail component will integrate into your ASP coding scripts which uses the mail server to send out email instead of CDONTS which is using IIS Virtual SMTP Server.

To send email messages using ASP.NET, your application must:

  • Create the SmtpClient and MailMessage classes instances.
  • Set the properties for the SmtpClient and MailMessage instances (such as the mail server, sender address, recipient address, message subject, and so on).
  • Call the Send() method of the SmtpClient instance to send the message.

This guide will shows the code samples for C# and VB.NET to demonstrate how to do this, and both are functionally equivalent to each other.

C# example

Firstly, create an HTML page that contains a basic interface where the user can specify the message’s sender, recipient, subject, and body. To do this, copy and paste the following code into a file named mail.aspx:
Secondly, create the code-behind page for the HTML page. To do this, copy and paste the following code into a file named mail.aspx.cs. Replace the following text values:

EventWireup="true" CodeFile="mail.aspx.cs" Inherits="SendMail" %>
<html>
<head id="Head1" runat="server"><title>E-mail test page</title></head>
<body>
    <form id="form1" runat="server">
        Message sender: <asp:textbox id="txtFrom" runat="server" /><br>
        Message recipient: <asp:textbox id="txtTo" runat="server" /><br>
        Message subject: <asp:textbox id="txtSubject" runat="server" /><br>
        Message body:<br/>
        <asp:textbox id="txtBody" runat="server" height="150px" textmode="multiline" /><br>
        <asp:button id="btn_SendMessage" runat="server" onclick="btn_SendMessage_Click" text="Send message" /><br>
        <asp:label id="Label1" runat="server" text="" />
    </form>
</body>
</html>

Secondly, create the code-behind page for the HTML page. To do this, copy and paste the following code into a file named mail.aspx.cs. Replace the following text values:

    • Replace domain.example.com with your own domain name.
    • Replace [email protected] with the name of an e-mail account you created in Plesk.
    • Replace password with the password for the e-mail account you specified in the previous step.
using System;
using System.Web.UI.WebControls;
using System.Net.Mail;

public partial class SendMail : System.Web.UI.Page
{
    protected void btn_SendMessage_Click(object sender, EventArgs e)
    {
        SmtpClient smtpClient = new SmtpClient("domain.example.com", 25);

        smtpClient.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;

        MailMessage mailMessage = new MailMessage(txtFrom.Text, txtTo.Text);
        mailMessage.Subject = txtSubject.Text;
        mailMessage.Body = txtBody.Text;

        try
        {
            smtpClient.Send(mailMessage);
            Label1.Text = "Message sent";
        }
        catch (Exception ex)
        {
            Label1.Text = ex.ToString();
        }
    }
}

Now you can use your web browser to load the mail.aspx file. Complete the fields, click Send message, and if the values are valid, the server should send the message.

VB.NET example

Firstly, create an HTML page that contains a basic interface where the user can specify the message’s sender, recipient, subject, and body. To do this, copy and paste the following code into a file named mail.aspx:

<%@ Page Language="VB" AutoEventWireup="true" CodeFile="mail.aspx.vb" Inherits="SendMail" %>
<html>
<head id="Head1" runat="server"><title>E-mail test page</title></head>
<body>
    <form id="form1" runat="server">
        Message sender: <asp:textbox id="txtFrom" runat="server" /><br>
        Message recipient: <asp:textbox id="txtTo" runat="server" /><br>
        Message subject: <asp:textbox id="txtSubject" runat="server" /><br>
        Message body:<br/>
        <asp:textbox id="txtBody" runat="server" height="150px" textmode="multiline" /><br>
        <asp:button id="btn_SendMessage" runat="server" onclick="btn_SendMessage_Click" text="Send message" /><br>
        <asp:label id="Label1" runat="server" text="" />
    </form>
</body>
</html>

Second, create the code-behind page for the HTML page. To do this, copy and paste the following code into a file named mail.aspx.vb. Replace the following text values:

    • Replace domain.example.com with your own domain name.
    • Replace [email protected] with the name of an e-mail account you created in Plesk.
    • Replace password with the password for the e-mail account you specified in the previous step.
Imports System
Imports System.Web.UI.WebControls
Imports System.Net.Mail

Public Class SendMail
    Inherits System.Web.UI.Page
    
    Protected Sub btn_SendMessage_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim smtpClient As SmtpClient = New SmtpClient("domain.example.com", 25)

        smtpClient.Credentials = New System.Net.NetworkCredential("[email protected]", "password")
        smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network

        Dim mailMessage As MailMessage = New MailMessage(txtFrom.Text, txtTo.Text)
        mailMessage.Subject = txtSubject.Text
        mailMessage.Body = txtBody.Text

        Try 
            smtpClient.Send(mailMessage)
            Label1.Text = "Message sent"
        Catch ex As Exception
            Label1.Text = ex.ToString
        End Try

    End Sub
End Class

Now you can use your web browser to load the mail.aspx file. Complete the fields, click Send message, and if the values are valid, the server should send the message.

We hope this article helped you to learn how to send emails using ASP script. For more articles, please go to Knowledge Base.

Was this article helpful?
Dislike 0
Previous: How Do I Download and Backup My Old Address Book?
Next: Cannot deliver emails to Yahoo Mail
Discover the perfect balance of performance and budget-friendly Dedicated Server plan !
Discover the perfect balance of performance and budget-friendly Dedicated Server plan !
High performance and low cost Dedicated Server plan 128GB from $185 – upgrade today!
High performance and cheap Dedicated Server plan 128GB from $185 – upgrade today!