Chapter 2: Common ASP.NET Code Techniques
5. Sending Email from an ASP.NET Page
With classic ASP, there were a variety of ways to send an email from an ASP page. Microsoft included its own free component, CDONTS (Collaborative Data Object for NT Server), but there were also a number of commercially (and freely) available third-party emailing components.
The .NET Framework contains two classes (found in the System.Web.Mail namespace) to assist with sending email. The first class, MailMessage, defines the various properties of the email message itself, such as the email message body, the sender, the receiver, the subject, the priority, and so on. The second class, SmtpMail, performs the work of actually sending the email message. To send emails through an ASP.NET page, you will need to have the SMTP Service installed on your Web server. This is a standard installation option when installing IIS.
Listing 2.5.1 depicts a very simple product information request form. The user can enter his name and email address to receive further product information by email. Upon submission of the form, an email is sent to the user providing information on the various product lines.
Listing 2.5.1 The .NET Framework Provides Inherent Email-Sending Support
1: <%@ Import Namespace="System.Web.Mail" %> 2: <script language="VB" runat="server"> 3: Sub btnSubmit_OnClick(sender as Object, e as EventArgs) 4: 'Send an email address with product information 5: Dim objMessage as New MailMessage 6: objMessage.BodyFormat = MailFormat.Html 7: objMessage.To = txtEmail.Text & " (" & txtName.Text & ")" 8: objMessage.From = "email@example.com (Product Information)" 9: objMessage.Headers.Add("Reply-To", "firstname.lastname@example.org") 10: objMessage.Priority = MailPriority.High 11: objMessage.Subject = "Product Information Request" 12: 13: objMessage.Body = "<i>Hello " & txtName.Text & "</i>! To learn more about our " & _ 14: "various products, visit the following URLs:<p><ul>" & _ 15: "<li><a href=""http://www.acme.com/Widgets.htm""> Widgets</a>" & _ 16: "<li><a href=""http://www.acme.com/DooHickies.htm""> DooHickies</a>" & _ 17: "<li><a href=""http://www.acme.com/TAMB.htm""> Thning-a-ma-Bobs</a>" & _ 18: "</ul><p> If you have any " & _ 19: "questions, please simply reply to this email! <p><hr><p>" & _ 20: "<font color=red size=-1><i>Thank you for choosing Acme! </i></font>" 21: 22: 23: 'Send the message 24: SmtpMail.Send(objMessage) 25: 26: 'Display notification of mail being sent 27: Response.Write("<font color=red><b>You have been sent " & _ 28: "product information! Thank you!</b></font>") 29: End Sub 30: </script> 31: 32: <html> 33: <body> 34: <form method=post runat=server> 35: <h1>Information Request</h1> 36: <b>Your Name:</b> 37: <asp:textbox id="txtName" runat="server" /><br> 38: <b>Your Email Address:</b> 39: <asp:textbox id="txtEmail" runat="server" /><p> 40: 41: <asp:button id="btnSubmit" runat="server" OnClick="btnSubmit_OnClick" 42: text="Send me Product Information" /> 43: </form> 44: </body> 45: </html>
Listing 2.5.1 starts by importing the namespace that the MailMessage and SmtpMail classes exist under: System.Web.Mail. Because there is no Page_Load event handler, let's next move on to examining the HTML content from line 32 through line 45. Note that a postback, server-side form control is used (line 34). Next, two text boxes are created: The first, txtName, is for the user's name (line 37), whereas the second, txtEmail is for the user's email address (line 39). Finally, a button control is used on lines 41 and 42. When clicked, the form will be posted back and the btnSubmit_OnClick event handler will fire.
The btnSubmit_OnClick event handler (lines 3 through 29), sends an email to the email address specified by the user in the txtEmail text box. As mentioned earlier, sending emails involves two classes, MailMessage and SmtpMail. On line 5 a new instance of the MailMessage class is created and, in lines 6 through 20, some of its many properties are set.
Some of the properties for the MailMessage include a BodyFormat property (line 6), which can be set to either MailFormat.Text or MailFormat.Html. On line 7 and 8, we set the To and From email addresses using the following format:
Email-address (Display Name)
Many email clients will pick out the text between the parentheses and display that in the From section of the email message as opposed to the actual email address itself. On line 9 we access the Headers property of the MailMessage class. This property is an IDictionary interface, meaning that it can be treated similar to a Hashtable. On line 9 we add the Reply-To header to the outgoing email address so that if the user replies to the product information-request email, it will be sent to email@example.com as opposed to firstname.lastname@example.org. Next, the Priority of the message is set. This property can be set to one of three values: MailPriority.Low, MailPriority.Normal (the default), or MailPriority.High. Line 11 sets the email's subject, whereas line 13 defines the HTML-formatted body for the email message.
If you've worked with the CDONTS component with classic ASP, you'll no doubt feel at home with the properties of the MailMessage class. The MailMessage and CDONTS component are almost syntactically identical. One major difference, however, is that the MailMessage class does not encompass any functionality for sending a message. That, instead, is left up to the SmtpMail class.
The SmtpMail class is a very bare-bones class. It's sole purpose is to send a MailMessage instance using the SMTP protocol. It contains a single shared function, Send, which expects a MailMessage object instance. Line 24 illustrates the use of the Send method.
Finally, on lines 27 and 28, a message is displayed indicating to the user that he can expect to receive an email with product information. Figure 2.18 contains a screenshot of Listing 2.5.1 after the user has entered his name and email address. Figure 2.19 contains a screenshot of the email received by the user. Note that a high priority is set; the email is displayed as coming from Product Information as opposed to just email@example.com; the email is addressed to the user's name, not to his email address; and the email is HTML-formatted.