Click here to Skip to main content
13,256,260 members (46,861 online)
Click here to Skip to main content
Add your own
alternative version


30 bookmarked
Posted 17 Jul 2013

Email Providers (SMTP/GMail) using Provider Design Pattern

, 27 Dec 2013
Rate this:
Please Sign up or sign in to vote.
Configure different email provider sources using the Provider design pattern


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.


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.

public abstract class EmailProvider : ProviderBase
    public abstract bool SendEmail(EmailInfo emailInfo);

Provider Collection Class (clsEmailProviderCollection.cs)

//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.

//That is all you have to do to make providers information available to other class
   public class EmailProviderConfiguration : ConfigurationSection
       public ProviderSettingsCollection Providers
               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.

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)

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

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

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

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

//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.Subject = emailInfo.Subject;
            message.From = new System.Net.Mail.MailAddress(emailInfo.From);
            message.Body = emailInfo.Body;

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

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

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();


Web.Config Entries

Here, we set the default provider as SMTP provider:

  <section name="EmailSend" 

 <EmailSend default="SMTPProvider">
        <add name="SMTPProvider" 

        type="Provider.Email.SMTP.SMTPProvider, Provider.Email.SMTP"/>
        <add name="GmailProvider" 

        type="Provider.Email.Gmail.GMailProvider, Provider.Email.Gmail"/>

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.


  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.


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


About the Author

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171114.1 | Last Updated 27 Dec 2013
Article Copyright 2013 by Kishan Zunjare
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid