How do I send email using ASP.NET?

Simply copy the code below and save it as textemail.aspx

C# Version

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
 protected void btnSubmit_Click(object sender, EventArgs e)
 {
  try
  {
   MailMessage message = new MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);
   SmtpClient emailClient = new SmtpClient(txtSMTPServer.Text);
   emailClient.Send(message);
   litStatus.Text = "Message Sent";
  }
  catch (Exception ex)
  {
   litStatus.Text = ex.ToString();
  }
 }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Send Test Email in C#</title>
</head>
<body>
<form id="form1" runat="server">
 <div>
 <table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;To</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtTo" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;From</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtFrom" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">&nbsp;SMTP Server</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSMTPServer" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;Subject</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSubject" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;Body</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtBody" runat="server" Columns="40" TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">Action</td>
<td bgcolor="#FFFFFF"><asp:Button ID="btnSubmit" runat="server" Text="Send Email" OnClick="btnSubmit_Click" /></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">Status</td>
<td bgcolor="#FFFFFF" class="basix"><asp:Literal ID="litStatus" runat="server"></asp:Literal></td>
</tr>
</table>
</div>
</form>
</body>
</html>

 

VB.NET Version

<%@ Page Language="vb" %>
<%@ Import Namespace="System.Net.Mail" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
 Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
  Try
   Dim message As MailMessage = New MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text)
   Dim emailClient As SmtpClient = New SmtpClient(txtSMTPServer.Text)
   emailClient.Send(message)
   litStatus.Text = "Message Sent"
  Catch ex As Exception
   litStatus.Text = ex.ToString()
  End Try
 End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
 <title>Send Test Email</title>
</head>
<body>
 <form id="form1" runat="server">
 <div>
 <table width="600" border="0" align="center" cellpadding="5" cellspacing="1" bgcolor="#cccccc">
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;To</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtTo" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;From</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtFrom" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">&nbsp;SMTP Server</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSMTPServer" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;Subject</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtSubject" runat="server" Columns="50"></asp:TextBox></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">&nbsp;Body</td>
<td bgcolor="#FFFFFF"><asp:TextBox ID="txtBody" runat="server" Columns="40" TextMode="MultiLine"></asp:TextBox></td>
</tr>
<tr>
<td align="right" bgcolor="#eeeeee" class="header1">Action</td>
<td bgcolor="#FFFFFF"><asp:Button ID="btnSubmit" runat="server" Text="Send Email" OnClick="btnSubmit_Click" /></td>
</tr>
<tr>
<td width="100" align="right" bgcolor="#eeeeee" class="header1">Status</td>
<td bgcolor="#FFFFFF" class="basix"><asp:Literal ID="litStatus" runat="server"></asp:Literal></td>
</tr>
</table>
</div>
</form>
</body>
</html>

Content retrieved from: https://support.appliedi.net/kb/a385/how-do-i-send-email-using-asp_net.aspx.

Updated on November 11, 2019
Was this article helpful?

Related Articles

Customer Support Portal
Can't find the answer you're looking for? Don't worry we're here to help!
CONTACT SUPPORT