Sending Email from a Web Page with ASP.NET 1.1

Here is a quick code example of how to send an email message from a web page using ASP.NET. First, I drew up a quick and dirty web page UI.

 <IMG src="/photos/jpapa/images/132245/original.aspx" border=0>

I added some required field validator controls for the To, From and Message fields. I also added some regular expression validator controls for all of the email address fields. It really is nice that the regular expression validator control comes with predefined expressions that you can choose form a drop down list in the property window. It offerred this expression for the email address, for example:

<BLOCKQUOTE dir=ltr style=”MARGIN-RIGHT: 0px”> <P><FONT face=”Courier New” size=2>ValidationExpression=”\w+([-+.]\w+)@\w+([-.]\w+).\w+([-.]\w+)*”</FONT></P></BLOCKQUOTE> <P dir=ltr style=”MARGIN-RIGHT: 0px”>For those of you familiar with CDO or the good ol’ CDONTS code that was popular for sending emails with traditional VB and ASP, there is a different namespace and class structure for this purpose in ASP.NET called System.Web.Mail.It is very easy to use. This code executes when the user clicks the send button. It creates an instance of the MailMessage class and sets the basic properties. Then it sets the priority and format using the provided enumerators. You can also send attachments, too, if needed.</P> <DIV style="BORDER-RIGHT: windowtext 1pt solid; PADDING-RIGHT: 0pt; BORDER-TOP: windowtext 1pt solid; PADDING-LEFT: 0pt; FONT-SIZE: 10pt; BACKGROUND: white; PADDING-BOTTOM: 0pt; BORDER-LEFT: windowtext 1pt solid; COLOR: black; PADDING-TOP: 0pt; BORDER-BOTTOM: windowtext 1pt solid; FONT-FAMILY: Courier New"> <P style="MARGIN: 0px">    1         private void btnSend_Click(object sender, System.EventArgs e)</P> <P style="MARGIN: 0px">    2         {</P> <P style="MARGIN: 0px">    3             MailMessage msg = new MailMessage();</P> <P style="MARGIN: 0px">    4             msg.From = txtFrom.Text;</P> <P style="MARGIN: 0px">    5             msg.To = txtTo.Text;</P> <P style="MARGIN: 0px">    6             msg.Cc = txtCc.Text;</P> <P style="MARGIN: 0px">    7             msg.Bcc = txtBcc.Text;</P> <P style="MARGIN: 0px">    8             msg.Subject = txtSubject.Text;</P> <P style="MARGIN: 0px">    9             msg.Body = txtBody.Text;</P> <P style="MARGIN: 0px">   10             msg.Priority = MailPriority.Normal;</P> <P style="MARGIN: 0px">   11             msg.BodyFormat = MailFormat.Text;</P> <P style="MARGIN: 0px">   12             SmtpMail.SmtpServer = “”; </P> <P style="MARGIN: 0px">   13             SmtpMail.Send(msg);</P> <P style="MARGIN: 0px">   14         }</P></DIV> <P dir=ltr style=”MARGIN-RIGHT: 0px”>Here I set the SmtpServer property to an empty string. This tells the MailMessage object to use the default mail server. SmtpServer is a static property so if you change it, every ASP.NET page that sends a message will be effected. To avoid any possible confusion, I always like to set this property explictly so I don’t have to worry about some ther page causing me a debugging heartache. You might also notice that the MailMessage class does not have a Send method. Instead, you  create an instance of the SmtpMail class and execute its Send method. The Send method can accept either a MailMessage object or the series properties that make up a message (a string for from, to, subjetc, etc.)</P> <P dir=ltr style=”MARGIN-RIGHT: 0px”>That’s it really. There are other features and lots of creative thing that you can do with this. But for those looking just to send a simple email message this is a nice quick example.</P> <P dir=ltr style=”MARGIN-RIGHT: 0px”>Update: If you are looking for a quick ASP.NET 2.0 example, click here to go to</P>

Tweet Post Share Update RSS

Hi, I'm John Papa. I author this blog, create courses for Pluralsight and am a Google Developer Expert and Microsoft Regional Director. I travel speaking at events and train technology thought leaders