Your Gateway To Digital Success
Wesbytes Blue Logo

Wesbytes Knowledge Base

Search our articles or browse by category below

Send out emails using ASP script

Last modified: July 2, 2022
You are here:
Estimated reading time: 2 min

How to Send Out Emails Using ASP Script?

We prefer that everyone utilise the CDOSYS mail component to send emails while running Windows 2008. Instead of CDONTS, which uses IIS Virtual SMTP Server, this mail component will integrate into your ASP coding scripts that use the mail server to send email.

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

  • Create instances of the SmtpClient and MailMessage classes.
  • Set the SmtpClient and MailMessage instances’ properties (such as the mail server, sender address, recipient address, message subject, and so on).
  • To send the message, use the SmtpClient instance’s Send() function.

This post will describe how to do this using code samples written in C# and VB.NET, both of which are functionally comparable.

C# example

First, make an HTML page with a simple interface on which the user may enter the sender, recipient, subject, and body of the message. For this, create a file called mail.aspx and paste the following code into it:
Second, make the HTML page’s code-behind page. To accomplish this, create a file called mail.aspx.cs and paste the following code into it. Text values to be replaced include:

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>

Second, make the HTML page’s code-behind page. To accomplish this, create a file called mail.aspx.cs and paste the following code into it. Text values to be replaced include:

  • 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();
        }
    }
}

You can now open the mail.aspx file in your web browser. If the data entered are correct, click Send message after filling out the forms, and the server should send the message.

VB.NET Example

First, make an HTML page with a simple interface on which the user may enter the sender, recipient, subject, and body of the message. For this, create a file called mail.aspx and paste the following code into it:

<%@ 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, make the HTML page’s code-behind page. To accomplish this, create a file called mail.aspx.vb and paste the following code into it. Text values to be replaced include:

  • 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

You can now open the mail.aspx file in your web browser. If the data entered are correct, click Send message after filling out the forms, and the server should send the message.

Was this article helpful?
Dislike 0
Views: 7