Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Configure Email Providers (SMTP/GMail) using Dependency Injection (DI)

0.00/5 (No votes)
9 Aug 2013 2  
Configure email providers like SMTP/GMail using Dependency Injection.

Introduction

This post discusses dependency Injection (DI) with example of email providers. Dependency Injection is a simple way to load plugins dynamically.

Background Of DI

Basically, Dependency injection is a technique for building loosely coupled applications. First, let's see what is tight & loose coupling.

Tight coupling - classes and objects are dependent on one another. Tight coupling is usually bad because it reduces flexibility and re-usability of code and it makes changes much more difficult and impedes test-ability. Loose coupling is a more generic concept, intended to increase the flexibility of a system. It makes the system more maintainable and makes the framework more "stable", also it improves test-ability.

With dependency injection, we can achieve a plug-in architecture with minimum dependencies between objects.

There are three options in order to perform dependency injection:

  1. Constructor Injection: Constructing a new instance of an object we inject the dependencies. Dependencies can be passed in the constructor.
  2. Setter Injection: Component exposes a setter method or property which we will inject a dependency to it.
  3. Method Injection: Dependencies can supply via a method parameter

In the below code sample using example of email providers, we will see how we can implement the above 3 DI injections.

Using the Code

I have attached the source code of this application, here I have created ASP.NET website, and implemented functionality of email sending with email server as SMTP or GMail; by using Dependency Injection pattern.

IEmailService.cs - Here, first we will require injector as interface IEmailService. This interface defines the method that is used to inject one or more dependencies.

//Common method for sending email
public interface IEmailService
{
    bool SendMail(EmailInfo Obj); 
} 

clsEmailInfo.cs - Here, I have taken one common EmailInfo class - where we can set email information properties like From, To, CC, Bcc, etc.

public class EmailInfo
{
    //Contains common properties to send email. 
}

clsSMTPProvider.cs - This is an actual implementation of Email sending using SMTP as server. Here, we have implemented the IEmailService interface to allow objects to be injected. And we put email sending logic here.

public class SMTPProvider : IEmailService
{
    public bool SendMail(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.Port = 25;
        try
        {
            smtp.Send(message);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}

clsGmailProvider.cs - This class is also the same as above, i.e., implementation of email sending using email server as GMail.

public class GmailProvider : IEmailService
{
    public bool SendMail(EmailInfo emailInfo)
    {
        MailMessage mail = new MailMessage();
        mail.To.Add(emailInfo.To);       
        mail.From = new MailAddress(emailInfo.From);
        mail.Subject = emailInfo.Subject;
        mail.Body = emailInfo.Body;
        mail.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.Port = 587;
        smtp.Credentials = new System.Net.NetworkCredential(
          "YourUserName@gmail.com", "YourGmailPassword");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        try
        {
            smtp.Send(mail);
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}

Implementation of Dependency Injection

  1. Constructor Injection - This is the most common dependency injection. Here, Interface is injected into a dependent object.
    //Class requires an instance of a dependency to work,
    //we can supply that dependency through the class’s constructor. 
    public class EmailSender_ConstInjection
    {
        private IEmailService _iEmailService;
        //Implementation of the constructor 
        public EmailSender_ConstInjection(IEmailService _mailService)
        {
            this._iEmailService = _mailService;
        }
        public void Notification(EmailInfo ObjEmailInfo)
        {
            _iEmailService.SendMail(ObjEmailInfo);
        }
    }   
  2. Property Injection - Property or setter injection - it is best used when dependency is optional.
    //writable property that allows a client to supply a different implementation
    public IEmailService EmailService { get; set; }
    public void SendNotification()
    {
        if (EmailService != null)
        {
            EmailService.SendMail(emailInfo);
        }
    }
  3. Method Injection - We can supply dependency via a method parameter.
    public void PromotionalNotification(IEmailService _emailService)
    {
        _emailService.SendMail(emailInfo);
    } public void PromotionalNotification(IEmailService _emailService)
    {
        _emailService.SendMail(emailInfo);
    } 

Interesting Points about DI

  • Reduces tight coupling - loose coupling
  • Easy configuration - We can easily manages injections
  • Major advantage of dependency injection is that it can make testing a lot easier

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here