Click here to Skip to main content
15,881,757 members
Articles / Web Development / ASP.NET

Email Providers (SMTP/GMail) using Provider Design Pattern

Rate me:
Please Sign up or sign in to vote.
4.56/5 (7 votes)
27 Dec 2013CPOL2 min read 18.5K   328   29  
Configure different email provider sources using the Provider design pattern
#region Comments
/*
 * Date             Initials    Comments
 * 2013-07-17       Kishan      Retrieve the configuration section for Email provider.
 */
#endregion Comments

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web.Configuration;

namespace Provider.EmailSender
{
   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.");
            }
        }
    } 
}

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)



Comments and Discussions