Knowledge Base

How to send e-mail messages using ASP.NET

This article describes how to send e-mail messages using ASP.NET.

Sending e-mail messages with ASP.NET

To configure and send e-mail messages using ASP.NET, your application must:

  • Create instances of the SmtpClient and MailMessage classes.
  • 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.

The following code samples for C# and VB.NET demonstrate how to do this, and are functionally equivalent to each other.

These code samples demonstrate basic functionality, and are intended as a starting point for you to use in your own applications. The SmtpClient and MailMessage classes contain many more properties and methods for advanced usage:

C# example

First, 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="C#" AutoEventWireup="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, 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.a2hosted.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.a2hosted.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

First, 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.a2hosted.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.a2hosted.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.