Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Email Providers (SMTP/GMail) using Provider Design Pattern

4.56/5 (7 votes)
27 Dec 2013CPOL2 min read 18.8K   328  
Configure different email provider sources using the Provider design pattern

Introduction

Here, we are creating an example of EmailProvider selection among different email sender sources like Gmail, SMTP, Exact Target, etc. We can dynamically change Email provider, just by updating web.config settings. By using ASP.NET provider design pattern, I have created a sample class library to manage email sending functionality using different email providers.

Background

ASP.NET uses the Provider pattern in a number of places. These include providers for the Membership and Role Management system, etc. Each provider is configured in the Web.config file, with the default settings configured in the server's root Web.Config. We can create their own providers based on an interface that defines the requirements, or by inheriting from a base class containing common functionality for that type of provider.

Using the Code

Please refer to the attached class library Provider.EmailSender which manages email sending functionality like dynamically set provider. Also, I have attached related provider libraries like Provider.Email.Gmail and Provider.Email.SMTP.

Following are the details of classes being used in this application:

Provider class (clsEmailProvider.cs)

The main data provider class containing abstract methods that needs to override in inherited class in all providers. A provider implementation must derive from an abstract base class, which is used to define a contract for a particular feature.

C#
public abstract class EmailProvider : ProviderBase
{
    public abstract bool SendEmail(EmailInfo emailInfo);
}   

Provider Collection Class (clsEmailProviderCollection.cs)

C#
//Provider configuration, which is specified in Configuration file
public class EmailProviderCollection : ProviderCollection
    {
        public new EmailProvider this[string name]
        {
            get { return (EmailProvider)base[name];}
        }  
    } 

Provider Configuration Class (clsEmailProviderConfiguration.cs)

Reads all providers defined in your web.config. This class provides access to the provider settings collection of data. Each provider setting is information about your Email provider such as Name, Type, etc.

C#
//That is all you have to do to make providers information available to other class
   public class EmailProviderConfiguration : ConfigurationSection
   {
       [ConfigurationProperty("providers")]
       public ProviderSettingsCollection Providers
       {
           get
           {
               return (ProviderSettingsCollection)base["providers"];
           }
       }

       //Here we can default SMTP Provider
       [ConfigurationProperty("default", DefaultValue = "SMTPProvider")]
       public string Default
       {
           get { return (string)base["default"]; }
           set { base["default"] = value; }
       }
   }

Provider Manager Class (clsEmailProviderManager.cs)

Retrieve the configuration section for Email provider.

C#
public class EmailProviderManager
    {
        private static EmailProviderCollection m_providers;

        private EmailProvider _MyEmailProvider;
        public EmailProvider MyEmailProvider
        {
            get { return _MyEmailProvider; }
            set { _MyEmailProvider = value; }
        }

        public EmailProviderCollection Providers
        {
            get { return m_providers; }
        }

        public void InitializeProvider()
        {
            EmailProviderConfiguration ObjConfiguration = default(EmailProviderConfiguration);

            string sConfigEntry = string.Empty;
            //This will be Web.config settings name
            sConfigEntry = "EmailSend";
            ObjConfiguration = (EmailProviderConfiguration)
            ConfigurationManager.GetSection(sConfigEntry);

            if ((ObjConfiguration != null))
            {
                m_providers = new EmailProviderCollection();
                ProvidersHelper.InstantiateProviders
                (ObjConfiguration.Providers, m_providers, typeof(EmailProvider));
                m_providers.SetReadOnly();

                //Set default provider.
                MyEmailProvider = m_providers[ObjConfiguration.Default];

                if (MyEmailProvider == null)
                {
                    throw new Exception("Currently this Provider is not available.");
                }
            }
            else
            {
                throw new ConfigurationErrorsException
                ("This Email Provider configuration section is not set correctly.");
            }
        }
    } 

Actual Implementation of SMTP Provider class (Provider.Email.SMTP.clsSMTPProvider.cs):

C#
//Implementation, inherit from base EmailProvider class
 public class SMTPProvider : EmailProvider
    { 
        public override bool SendEmail(EmailInfo emailInfo)
        {
            //Set SMTP related settings
            MailMessage message = new MailMessage();
            message.To.Add(emailInfo.To);
            message.Subject = emailInfo.Subject;
            message.From = new System.Net.Mail.MailAddress(emailInfo.From);
            message.Body = emailInfo.Body;

            SmtpClient smtp = new SmtpClient("localhost");
            //smtp.Port = 25;
            smtp.Send(message);
            return true;
        }
    } 

Uses of this in ASP.NET website (EmailSend.aspx.cs):

C#
private void SendEmail()
       {
           EmailInfo emailInfo = new EmailInfo();
           emailInfo.To = txtTo.Text.Trim();
           emailInfo.From = txtFrom.Text.Trim();
           emailInfo.Subject = txtSubject.Text;
           emailInfo.Body = txtBody.Text;

           //Create object of manager class and call
           //sendEmail method which will do rest of things.
           EmailProviderManager obj = new EmailProviderManager();
           obj.InitializeProvider();

           obj.MyEmailProvider.SendEmail(emailInfo);
       }

Web.Config Entries

Here, we set the default provider as SMTP provider:

XML
<configSections>
  <section name="EmailSend" 
     type="Provider.EmailSender.EmailProviderConfiguration, 
     Provider.EmailSender"/>
  </configSections>
 <EmailSend default="SMTPProvider">
      <providers>
        <add name="SMTPProvider" 
        type="Provider.Email.SMTP.SMTPProvider, Provider.Email.SMTP"/>
        <add name="GmailProvider" 
        type="Provider.Email.Gmail.GMailProvider, Provider.Email.Gmail"/>
      </providers>
 </EmailSend>

Points of Interest

  1. Here, presentation and business logic layers remain unchanged.
  2. Here, we have no need to explicitly instantiate classes.
  3. The .NET Framework will automatically manage class instantiate including re-using classes that have already been instantiated.

Benefits

  1. Switching email providers is much easier.
  2. Changing the source of data for your application from the current database to any database, whether SQL Server, Oracle, XML, or other.

License

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