Fluent Interface Design in .NET: Listing 1

EmailSender non-fluent API implementation.

EmailSender.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;
 
namespace VSMFluentInterfaces.Email
{
    public partial class EmailSender
    {
        public String EmailServer { get; set; }
        public String Username { get; private set; }
        public String Password { get; private set; }
 
        public EmailSender(String outgoingServer)
        {
            EmailServer = outgoingServer;
        }
 
        public EmailSender()
        {
        }
        
        public void SetCredentials(String username, String password)
        {
            Username = username;
            Password = password;
        }
 
        public void Send(EmailMessage message)
        {
            SmtpClient mailServer = new SmtpClient(EmailServer);
            mailServer.Credentials = new NetworkCredential(Username, Password);
            MailMessage mailMessage = new MailMessage(message.FromAddress,
                message.ToAddress, message.Subject, message.Message);
            mailServer.Send(mailMessage);
        }
    }
}

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at vogelvision@gmail.com.