This article describes how to send e-mail messages using ASP.NET.
To configure and send e-mail messages using ASP.NET, your application must:
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:
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:
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
, and if the values are valid, the server should send the message.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:
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
, and if the values are valid, the server should send the message.Subscribe to receive weekly cutting edge tips, strategies, and news you need to grow your web business.
No charge. Unsubscribe anytime.
Did you find this article helpful? Then you'll love our support. Experience the A2 Hosting difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.