Sunday, October 9, 2011
Send mail From Asp.Net
Introduction
Email serves as a ubiquitous, asynchronous notification and information distribution system. Not surprisingly, there are many web development scenarios where server-side code needs to generate an email and scuttle it off to the intended recipient. The email may be destined for a user of the website, informing them of their newly created user account, reminding them of their forgotten password, or emailing them an invoice. Or it may be destined for a web developer or site administrator, providing information of an unhandled exception that just transpired or user feedback. Fortunately, ASP.NET makes sending email a breeze. The .NET Framework version 1.x included a number of classes in the
System.Web.Mail class that allowed programmatically sending an email with a few scant lines of code. While this namespace and these classes still exist in the .NET Framework version 2.0, they have been deprecated in favor of new mail-related classes found in the System.Net.Mail namespace. (For an article on sending email in ASP.NET version 1.x, see Sending Email from an ASP.NET 1.x Web Page or consult www.SystemWebMail.com.) In this article we'll look at the classes in the
System.Net.Mail namespace and see how to send an email from an ASP.NET 2.0 page's code-behind class. We'll also look at specifying relay server information in Web.config and how this information can be used in some of the built-in ASP.NET server controls for sending emails (such as when a user creates an account or needs a password reminder/reset). Read on to learn more! After reading this article, be sure to check out Sending Email in ASP.NET 2.0: HTML-Formatted Emails, Attachments, and Gracefully Handling SMTP Exceptions, where we'll look at sending HTML-formatted emails, emails with attachments, and gracefully handling SMTP exceptions! Then mosey over to Sending Email in ASP.NET 2.0: Reply-To, Priority, and Read Receipts for even more great email content. Exploring the Classes in the |
<configuration> |
The
host attribute contains the relayServerHostname. If you are using an external relay server, the relayServerHostname might be something like smtp.yourisp.com. If the relay server's port number is something other than the typical port 25, specify it through the port attribute. Most external relay servers require authentication of some sort (in order to prevent anonymous spammers from sending their garbage through the relay). The userName and password attributes can be provided in the case where username/password authentication is needed. Only a subset of the
SmtpClient properties can be specified through settings in Web.config. To customize the other SmtpClient properties - EnableSsl, Timeout, and so on - set them programmatically when sending the email (step 4 from the list of five steps examined earlier in this article). Sending an Administrator Email Through a Feedback Web Page
To illustrate sending an email using the
MailMessage and SmtpClient classes, I've created a simple feedback page example. In this page the user is prompted for their email address, the subject of their feedback, and their feedback. <table border="0"> |
Click event fires. Inside the event handler, a MailMessage object is created and its To, From, Subject, and Body properties are set according to the information provided by the user. With the MailMessage object created and its properties populated, the email is then sent through the SmtpClient class's Send method. Protected Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click |
We didn't need to set any of the
SmtpClient class's properties here in code because they have been specified in Web.config (download the complete code at the end of this article to run this application on your computer).
Conclusion
Along with a plethora of other improvements from ASP.NET 1.x, the email sending capabilities in ASP.NET 2.0 have been updated and moved to a new namespace,
System.Net.Mail. In 2.0 the relay server settings can easily be decoupled from the ASP.NET code and moved into the Web.config file, as we saw in this example. Moreover, there's better support for relay server authentication. Future articles will explore more advanced email scenarios, such as: crafting HTML-formatted emails, sending attachments, embedding objects within the email body, handling SMTP/relay server-related exceptions, and so on.