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.
public abstract class EmailProvider : ProviderBase
{
public abstract bool SendEmail(EmailInfo emailInfo);
}
Provider Collection Class (clsEmailProviderCollection.cs)
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.
public class EmailProviderConfiguration : ConfigurationSection
{
[ConfigurationProperty("providers")]
public ProviderSettingsCollection Providers
{
get
{
return (ProviderSettingsCollection)base["providers"];
}
}
[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;
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();
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):
public class SMTPProvider : EmailProvider
{
public override bool SendEmail(EmailInfo emailInfo)
{
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.Send(message);
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;
EmailProviderManager obj = new EmailProviderManager();
obj.InitializeProvider();
obj.MyEmailProvider.SendEmail(emailInfo);
}
Web.Config Entries
Here, we set the default provider as SMTP provider:
<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
- Here, presentation and business logic layers remain unchanged.
- Here, we have no need to explicitly instantiate classes.
- The .NET Framework will automatically manage class instantiate including re-using classes that have already been instantiated.
Benefits
- Switching email providers is much easier.
- Changing the source of data for your application from the current database to any database, whether SQL Server, Oracle, XML, or other.