Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Sending Email in Asp.net

0.00/5 (No votes)
11 Oct 2013 2  
Sending email is very common in Asp.net application. In most of asp.net applications I was asked to send email to client or admin etc. Meanwhile,

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

Sending email is very common task in ASP.NET application. In most of ASP.NET applications I was asked to send email to client or admin etc. Meanwhile, almost always I was asked to change content of email many times. Like change a sentence, put space between sentences. Sometimes to create email the way client needs I had to change email content 10 times. Finally I come up with the idea that I need to make this process configurable and allow clients change the content themselves easily. Another issue is that since email is dependent to other factors like mail server sometimes your application may not be successful to send email so you need detect unsuccessful emails and send email in later steps. This post will talk about best practice to send email to client. download the Code

Understanding the issue

We are going to send email to users. We need these functionalities:

  1. The Email content should be configurable
  2. We may need to have a link in our email to somewhere in our site or in other sites
  3. If sending email is not successful we need to log that email in database to be able to send email in future

You can send email in .net using System.Web.Mail.SmtpMail which is obsolete. Also you can use System.Net.Mail.SmtpClient. I highly recommend the second one. Also try to use System.Net.MailMessage which is a new class in .NET 2.0 unless you are still using .NET 1.1  By using SmtpClient class you can configure your smtp server information in web.config.  Take a look at this code:

Quick look to class diagram:  

I need to remind you that I did not use multi tier architecture here. I highly recommend that Accessing to database should be done through DataAccessLayer.

EmailHelper, EmailLog, EmailLogProvider, DataAccessHelper

I will call EmailHelper class methods to send email in our case we have just one type of email if I had 10 different emails (of course with different content) I would have 10 methods to send Emails. All of those 10 methods would use SendEmail method (a private method) to send email. Also in this class there is another method called LogException used to Log the exception in database. Meanwhile, the email information is being saved so we can send email in later step to client. In this method an object of email log is being created then it is passed to EmailLogProvider Insert method to insert into database. DataAccessHelper is a utility class that you may use to add parameter to a command and to run a stored procedure. In our example it is being used by EmailLogProvider class. EmailReader, EmailLinkLoader:When a method of EmailHelper is being called to send an email first a MailMessage object is created afterwords, it is passed to EmailReader class to prepare the body and other information. In this method we load information form an XML file. So it is highly maintainable because as long as client gets back to us to change content of email we can apply those changes in this XML file not to our code. Take a look of this XML file.

<?xml version="1.0" encoding="utf-8" ?>
<Email>

<subject>Thank you for registering in CsharpCourses</subject>

<body>

<![CDATA[ Dear $firstName$

<p>Thank you for registering with Csharpcourses. </p>

<p>We are happy to offer you free csharp training. </p>

<p>Please note you are bound by the $TandCLink$ of this website. Should you have any trouble with Courses,

please contact csharpcourses support on (02) xxxx xxxx.</p>

<p>Kind regards,<br/><br/>

Csharpcourses Support Group

</p>

]]>

</body>

<fromEmail>Support@CsharpCourses.com</fromEmail>

<fromEmailName>CshaarpCourses Support</fromEmailName>

<bcc>emad@CsharpCourses.com;Support@CsharpCourses.com</bcc>

</Email>

As you see we even have “fromEmail" and “fromEmailName” also as many as required bcc email can be configured. A very nice trick in this code is that if you look closer to the xml file you will find $firstName$ . This is a key for our code and we will replace this value in EmailLoader.LoadRegistration. So the email can have dynamic information for example another good example is that you may want to send username and password to a user but this information may vary from one user to another user so we use other keys and we replace this information in our code exactly the same way for $firstName$. In most of the sites that I worked I had more than one email also many of them had some special links like Terms and Condition link that should be placed into email. A good practice always suggests that never put a link hard coded as long as you have more than one email content connecting to the same link. So imagine you have 5 email and all of them somewhere in their content have a link to Terms and condition. You better make this link configurable because when this link changes you need change just one configuration rather than looking for this link in all emails. So EmailLinkLoader actually load another xml file and find the link address then cache this information. So next time it does not need to look up link again. This link information is passed to EmailReader to be applied to body content.

<?xml version="1.0" encoding="utf-8" ?>
<links>
   <TandCLink>
      <a href='http://www.CshaarpCourses.com/TandC.aspx'>terms and conditions</a>
   </TandCLink>
</links>

Important (how to run the code)

Open Visual studio and form file menu select open web site and form file system select the project folder. Afterwords you can run the project. You need to apply smtpserver address. A good test is without applying smtpserver if you run application then application ask your first Name and your email address. Afterwords since sending email is not successful the email information is saved into database. And it shows you unsuccessful message. In later steps we can use EmailLogProvider class and load all EmailLog objects so we can retry and resend email.

Ready reusable class for sending e-mails

http://forums.asp.net/t/1356329.aspx

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here