Form mail - the ASP.Net 2.0 way

How to send mail from a web form

OK, so you have not yet attained Black Belt in The Way of ASP.Net. Learning how to use ASP.Net 2.0 will be worth the effort, and we have posted the sample code at the end of this article to get you started.
For an easy ride, however, please check the following article:

...that one will get you up and running within 5 minutes!

Now for those of you who are willing to try ASP.Net 2.0 (System.Web.Mail NameSpace) please check out the original Microsoft MSDN notes at
And here is a sample piece of code:

<% @Import Namespace="System.Web.Mail" %>
<script language="vb" runat="server">

Sub btnSendFeedback_Click(sender as Object, e as EventArgs)

'Create an instance of the MailMessage class
Dim objMM as New MailMessage()

'Set the properties - send the email to the person who filled out the
'feedback form.
objMM.To = ""
objMM.From = txtEmail.Text

'If you want to CC this email to someone else, uncomment the line below
'objMM.Cc = ""

'If you want to BCC this email to someone else, uncomment the line below
'objMM.Bcc = ""

'Send the email in text format
objMM.BodyFormat = MailFormat.Text
'(to send HTML format, change MailFormat.Text to MailFormat.Html)

'Set the priority - options are High, Low, and Normal
objMM.Priority = MailPriority.Normal

'Set the subject
objMM.Subject = "Web form - Feedback"

'Set the body
objMM.Body = "At " + DateTime.Now + " feedback was sent from an ASP.NET " & _
"Web page. Below you will find the feedback message " & _
"send by " & txtName.Text & "." & vbCrLf & vbCrLf & _
"---------------------------------------" & vbCrLf & vbCrLf & _
txtMessage.Text & vbCrLf

'Specify to use the default Smtp Server
SmtpMail.SmtpServer = ""

'Now, to send the message, use the Send method of the SmtpMail class

panelSendEmail.Visible = false
panelMailSent.Visible = true
End Sub


<asp:panel id="panelSendEmail" runat="server">
<form runat="server">
<h2>We are interested in your feedback! Please enter the following
requested information below to send us your comments.</h2>

<b>Your Name:</b>
<asp:textbox id="txtName" runat="server" />

<b>Your Email Address:</b>
<asp:textbox id="txtEmail" runat="server" />

<b>Your Message:</b><br>
<asp:textbox id="txtMessage" TextMode="MultiLine"
Columns="40" Rows="10" runat="server" />

<asp:button runat="server" id="btnSendFeedback" Text="Send Feedback!"
OnClick="btnSendFeedback_Click" />

<asp:panel id="panelMailSent" runat="server" Visible="False">
An email has been sent to the email address you specified. Thanks!

Article ID: 17, Created On: 5/22/2008, Modified: 5/22/2008

Feedback (0)