Shopping Cart Application Part-04 (Email Settings)

By | April 10, 2015

@. The email service also functionally same as Logging and Application settings implementation.
@. So add an interface named it IEmailService with following code definition:

namespace Shoppingcart.Infrastructure.Email
{
    public interface IEmailService
    {
        void SendMail(string from, string to, string subject, string body);
    }
}

@. To retrieve the implementation of IEmailService define a class which will contain Factory method. So name it to EmailServiceFactory with following definition:

namespace Shoppingcart.Infrastructure.Email
{
    public class EmailServiceFactory
    {
        private static IEmailService _emailService;

        public static void InitializeEmailServiceFactory(IEmailService emailService)
        {
            _emailService = emailService;
        }

        public static IEmailService GetEmailService()
        {
            return _emailService;
        }
    }

}

@. The .NET framework has a built-in e‑mail support within the System.Net.Mail code namespace. Create a new class named SMTPService that utilizes this to send e‑mails:

namespace Shoppingcart.Infrastructure.Email
{
    public class SMTPService : IEmailService
    {
        public void SendMail(string from, string to, string subject, string body)
        {

            MailMessage message = new MailMessage();

            message.From = new MailAddress(from);
            message.To.Add(new MailAddress(to));
            message.Subject = subject;
            message.Body = body;
            message.IsBodyHtml = true;
            message.Priority = MailPriority.Normal;
            
            string userName = ApplicationSettingsFactory.GetApplicationSettings().EmailAddress;
            string password = ApplicationSettingsFactory.GetApplicationSettings().Credential;


            SmtpClient smtp = new SmtpClient("smtp.gmail.com", Convert.ToInt32(587));
            smtp.Credentials = new NetworkCredential(userName, password);
            smtp.EnableSsl = true;
            smtp.Send(message);

        }
    }

}

@. In above code we are using ApplicationSettingsFactory to obtain the UserName and Password to create new instance of NetworkCredential.
@. So open IApplicationSettings and add following readonly properties to it:

string EmailAddress { get; }
string Credential { get; }	

@. So the complete definition of IApplicationSettings look follows:

namespace Shoppingcart.Infrastructure.Configuration
{
    public interface IApplicationSettings
    {
        string LoggerName { get; }
        string EmailAddress { get; }
        string Credential { get; }
    }
}

@. Then implement them inside WebConfigApplicationSettings as follows:

public string EmailAddress
{
     get { return ConfigurationManager.AppSettings["EmailAddress"]; }
}

public string Credential
{
     get { return ConfigurationManager.AppSettings["Credential"]; }
}

@. Now the complete definition of WebConfigApplicationSettings is look follows:

namespace Shoppingcart.Infrastructure.Configuration
{
    public class WebConfigApplicationSettings : IApplicationSettings
    {
        public string LoggerName
        {
            get { return ConfigurationManager.AppSettings["LoggerName"]; }
        }

        public string EmailAddress
        {
            get { return ConfigurationManager.AppSettings["EmailAddress"]; }
        }

        public string Credential
        {
            get { return ConfigurationManager.AppSettings["Credential"]; }
        }
    }

}

@.Inside above property implementation we used AppSettings. So go to WebConfig and add following AppSettings:



So we have completed our EmailSettings.

One can download entire source from here

547 Total Views 1 Views Today
Md. Mojammel Haque

CSM, CSPO, CSD, CSP-SM, CSP-PO (ScrumAlliance.org)
Certification Profile Link-
https://www.scrumalliance.org/community/profile/mhaque13

Currently working as Lead Team (Application Architecture) at Raven Systems Ltd. Passion for software development especially agile practices such as TDD with in depth knowledge of Object Oriented Programming, SOLID Principles, Gang of Four Design Patterns, Some Enterprise Application Architectural Patterns. Over 8 years of software development experience ASP.NET. Has the ability to understand and transform complex business requirements into software ensuring applications are delivered on time. Also experience in non Microsoft .NET technologies such as Dapper.Net, Git, Structure Map & Angular, Bootstrap, HTML-5, CSS-3 etc.

Category: Asp.net MVC Learn by doing - ASP.Net MVC Tags: , , , ,
Md. Mojammel Haque

About Md. Mojammel Haque

CSM, CSPO, CSD, CSP-SM, CSP-PO (ScrumAlliance.org) Certification Profile Link- https://www.scrumalliance.org/community/profile/mhaque13 Currently working as Lead Team (Application Architecture) at Raven Systems Ltd. Passion for software development especially agile practices such as TDD with in depth knowledge of Object Oriented Programming, SOLID Principles, Gang of Four Design Patterns, Some Enterprise Application Architectural Patterns. Over 8 years of software development experience ASP.NET. Has the ability to understand and transform complex business requirements into software ensuring applications are delivered on time. Also experience in non Microsoft .NET technologies such as Dapper.Net, Git, Structure Map & Angular, Bootstrap, HTML-5, CSS-3 etc.

Leave a Reply

Your email address will not be published. Required fields are marked *