ASP Hosting sale!
1,000MB space, 40GB transfer, 1 SQL Server 2000 db! - $9.95 per month*!
Limited Time Offer!

aspdev | articles | tutorials | forums

ASPdev -> All Articles -> Sending email in ASP.NET

Sending email in ASP.NET

One of the most common functionalities used in web development is sending email from a web page. Before you ask, Iíll give you a couple of reasons why you
might want to send email from your web application:

. create feedback web page
. implement "forgot password" script that sends a password to the userís email
. send welcome email to your new newsletter subscriber
. send automatic email update notification
. send automatic email notification whenever an error occur in your web

Of course there are many more situations where is appropriate to send email from your web application and it's up to you how and where exactly you are going to do it.

The .NET framework makes the task of sending email from a web page unbelievably easy and I'll show you how to do it in a matter of minutes. This article presumes that you have a basic understanding of how .NET and ASP.NET works.

We are going to use the SmtpMail and MailMessage classes, which
belong to the System.Web.Mail namespace. In order to use them, we will need to import the System.Web.Mail namespace like this:

<%@ Import Namespace="System.Web.Mail" %>

The MailMessage class provides properties and methods for constructing an e-mail message. To build our email message we need to create an instance of this class:

Dim objMail As New MailMessage()

On the next step we have to set all the properties of the objMail object:

' The email address of the sender
objMail.From = ""

' The email address of the recipient
objMail.To = ""

' The email address of the Cc recipient
objMail.Cc = ""

' The email address of the Bcc recipient
objMail.Bcc = ""

' The format of the message - it can be MailFormat.Text or MailFormat.Html
objMail.BodyFormat = MailFormat.Text

' The priority of the message - it can be MailPriority.High, MailPriority,Normal
' or MailPriority.Low
objMail.Priority = MailPriority.High

' The subject of the message
objMail.Subject = "My first ASP.NET email"

' The message text
objMail.Body = "This is my first email sent via ASP.NET. It was easier than I thought :-)"

After all the properties of our new email message are set properly, the only
thing left is to send the message. If you have experience sending email from
classic ASP script with CDONTS you might think that the MailMessage class has method Send or something similar. Well the way an email message is sent is a little different in ASP.NET. We need to use the static method Send of the SmtpMail class, passing in our objMail instance. You might
ask what does static member mean? This means that this method is not associated with an instance of a type. Example of an instance of a type is our objMail object. It is illegal to reference a static member of a class through an instance. The proper way to do it is:


If you want to specify different SMTP server, than the default one you need to
set the SmtpServer property of the SmtpMail class:

SmtpMail.SmtpServer = ""

In summary to send an email from your ASP.NET page you need to:

. import the System.Web.Mail namespace in your ASP.NET page
. create an instance of the MailMessage class
. set all the properties of the MailMessage instance
. send the message with SmtpMail.Send method

Finally I'll give you a completely functional code for an ASP.NET page that sends the userís feedback via email:

<%@ Page Language="VB" EnableSessionState="False" EnableViewState="False"
Trace="False" Debug="False"%>
<%@ Import Namespace="System.Web.Mail" %>
<script language="VB" runat=server>

Sub Page_Load(Sender as Object, E as EventArgs)
If Page.IsPostBack Then
lblResponse.Text = "Your email has been sent."
End If
End Sub

Sub btn_Click(sender as Object, e as System.EventArgs)
If Request.Form("Email") <> "" Then
Dim objMail As New MailMessage()
objMail.From = ""
objMail.To = Request.Form("Email")
objMail.Subject = Request.Form("Subject")
objMail.Body = Request.Form("Message")
objMail.BodyFormat = MailFormat.Text
SmtpMail.SmtpServer = ""
lblResponse.Text = "Please enter an email address."
End If
End Sub

.main {font-family:Verdana; font-size:12px;}
.title {font-family:Verdana; font-size:18px; font-weight:bold;}
<span class="title" align="center">Send email from an ASP.NET page</span>

<br><br><asp:Label class="main" id="lblResponse" runat="server"/>

<form method="POST" name="MainForm" runat="server">
<table ID="Table1">
<td class="main" align="right">Email:</td>
<td class="main"><input type="text" class="main" name="Email" value=""></td>
<td class="main" align="right">Subject:</td>
<td class="main"><input type="text" class="main" name="Subject" value=""></td>
<td class="main" align="right" valign="top">Message:</td>
<td class="main"><textarea name="Message" cols="50" rows="8"></textarea></td>

<td class="main"> </td>
<td class="main"><input type="Submit" id="btnSubmit" OnServerClick="btn_Click"
value="Send" runat="server" /></td>

Contact Us