Sending Email with ASP.NET
Written by: Christoph Wille
Something found on every web page today is an email form of some sort. In all probability this will not change anytime soon, therefore I will demonstrate today how to send email via ASP.NET: from plain to HTML mail and attachments.
The use of the source code in this article requires an installation of Microsoft's .NET Framework SDK on a Web server. I also assume a certain familiarity of the reader with the C# programming language.
The quickest way
There always is a way that can be classified under the heading 'Quick and Dirty'. And I again want to begin with this way because we can test the server configuration quite easily without having to take any side effects into consideration (SimpleMail.aspx).
<% @Page Language="C#" %> <% @Import Namespace="System.Web.Mail" %> <% string strTo = "christophw@sleeper.Dev.AlfaSierraPapa.Com"; string strFrom = "firstname.lastname@example.org"; string strSubject = "Hi Chris"; SmtpMail.Send(strFrom, strTo, strSubject, "A real nice body text here"); Response.Write("Email was queued to disk"); %>
If everything went well, the result looks like the following screenshot:
What actually happens in the script? The complete email support resides in the System.Web.Mail namespace. In this namespace we find the class SmtpMail, whose static Send method can accept four parameters:
SmtpMail.Send(From, To, Subject, BodyText);
As I said, it is very simple, but it has one thing in common with the more functional email sending options: the SMTP Server must be the locally installed SMTP Service of the IIS - no other can be used. In this respect the email support of ASP.NET is identical with the CDONTS of ASP 3.0.
Sending HTML Email
But now we leave this all too simple method behind and look at a very functional object: the MailMessage class. This class 'encapsulates' everything one can wish for in an email - the following example demonstrates the use with a short HTML email (SimpleMailMessage.aspx).
<% @Page Language="C#" %> <% @Import Namespace="System.Web.Mail" %> <% MailMessage msgMail = new MailMessage(); msgMail.To = "christophw@sleeper.Dev.AlfaSierraPapa.Com"; msgMail.Cc = "webmaster@sleeper.Dev.AlfaSierraPapa.Com"; msgMail.From = "email@example.com"; msgMail.Subject = "Hi Chris, another mail"; msgMail.BodyFormat = MailFormat.Html; string strBody = "<html><body><b>Hello World</b>" + " <font color=\"red\">ASP.NET</font></body></html>"; msgMail.Body = strBody; SmtpMail.Send(msgMail); Response.Write("Email was queued to disk"); %>
The code looks much better now and above all, there are significantly more options than there are in the first Send method. First we set the To, From and Subject properties of the MailMessage then we set the BodyFormat to the value Html out of the MailFormat enumeration. And then we already are set to send a HTML email.
Worth mentioning is the fact that all overloads of the Send method do not have return values about the success of dispatching the email message. The reason for this is that the emails simply are written into the Pickup folder of the Inetpub directory from where they are read and then sent by the SMTP Service. Failed emails (dispatching errors) also are written into files, this time however in the Badmail folder.
As an addendum, I want to show a screenshot of the email:
With certain email components, sending attachments can be a bit of an adventure. With .Net, it is just another easy to use component: MailAttachment. The following code demonstrates how an attachment can be added to the MailMessage (MailAttachment.aspx).
<% @Page Language="C#" %> <% @Import Namespace="System.Web.Mail" %> <% MailMessage msgMail = new MailMessage(); msgMail.To = "christophw@sleeper.Dev.AlfaSierraPapa.Com"; msgMail.From = "firstname.lastname@example.org"; msgMail.Subject = "Attachment Test"; msgMail.BodyFormat = MailFormat.Text; msgMail.Body = "Check out the attachment!"; msgMail.Attachments.Add(new MailAttachment("c:\\temp\\annual-report.pdf")); SmtpMail.Send(msgMail); Response.Write("Email was queued to disk"); %>
might also be programmed as follows
MailAttachment maAttach = new MailAttachment("c:\\temp\\annual-report.pdf"); IList msgAttachments = msgMail.Attachments; msgAttachments.Add(maAttach);
The point I want to make is that the attachments are handled through an IList interface (to be found in the System.Collections namespace) - and this supplies the Add method. This IList interface can also be used to enumerate or delete attachments. An additional remark has to be made about the constructor of MailAttachment: the encoding type can be set (this is probably rarely needed).
I still owe you a screenshot - this is what the attachment looks like in Outlook Express. Small programming effort, big effect (when something meaningful is sent).
This is it for today's crash course in sending email in ASP.NET. I have presented the classes that can be used to send email to your customers - or yourself - through the Web server using onboard means.
Downloading the Code
Click here to start the download.