Click here to Skip to main content
15,893,486 members
Articles / Web Development / IIS

Email Notification Framework

Rate me:
Please Sign up or sign in to vote.
4.05/5 (9 votes)
4 Oct 2009CPOL7 min read 68.9K   1.2K   62  
A .NET managed framework to send template emails in HTML or plain text
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Frameworks.Notification.Core.ResxNotificationController
{
    /// <summary>
    /// This is a notification controller which uses a Resx file as a data store.
    /// </summary>
    class ResxNotificationController : AbstractNotificationController
    {
        /// <summary>
        /// This method returns a subject template.
        /// </summary>
        /// <param name="typeOfNotification">This is the type of notification.</param>
        /// <returns>This is the subject template</returns>
        protected override string GetSubjectTemplate(NotificaionType typeOfNotification)
        {
            switch (typeOfNotification)
            {
                case NotificaionType.SampleNotification:
                    return EmailSubjectDataStore.SampleNotification;
                    break;
            }
            throw new ArgumentException();
        }

        /// <summary>
        /// This method returns a body template.
        /// </summary>
        /// <param name="typeOfNotification">This is the type of notification.</param>
        /// <returns>This is the body template</returns>
        protected override string GetBodyTemplate(NotificaionType typeOfNotification)
        {
            switch (typeOfNotification)
            {
                case NotificaionType.SampleNotification:
                    return EmailBodyDataStore.SampleNotification;
                    break;
            }
            throw new ArgumentException();
        }

        /// <summary>
        /// This method returns the subject data after replacing the placeholder
        /// </summary>
        /// <param name="subjectTemplate">The subject template</param>
        /// <param name="placeholdersDataForSubject">The placeholder data</param>
        /// <returns>The subject</returns>
        protected override string ReplaceSubjectPlaceholders(string subjectTemplate, IList<KeyValuePair<string, string>> placeholdersDataForSubject)
        {
            return this.ReplacePlaceHolders(subjectTemplate, placeholdersDataForSubject);
        }
        /// <summary>
        /// This method returns the subject data after replacing the placeholder
        /// </summary>
        /// <param name="bodyTemplate">The body template</param>
        /// <param name="placeholdersDataForBody">The placeholder data</param>
        /// <returns>The subject</returns>
        protected override string ReplaceBodyPlaceholders(string bodyTemplate, IList<KeyValuePair<string, string>> placeholdersDataForBody)
        {
            return this.ReplacePlaceHolders(bodyTemplate, placeholdersDataForBody);
        }

        /// <summary>
        /// This method replaces the placeholder data in the template
        /// </summary>
        /// <param name="template">The template string</param>
        /// <param name="placeholdersData">placeholder data</param>
        /// <returns>The modified template</returns>
        private string ReplacePlaceHolders(string template, IList<KeyValuePair<string, string>> placeholdersData)
        {
            StringBuilder sb = new StringBuilder(template);        
            foreach (KeyValuePair<string, string> token in placeholdersData)
            {
                sb = sb.Replace(token.Key, HttpUtility.HtmlEncode(token.Value));
            }
            return sb.ToString();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Imfinity
India India
Hi I have been working on enterprise applications for last six years.

Comments and Discussions