code first, ask questions later

Fluent Email in .NET

I have been working with sending emails with System.Net.Mail and had a few people mention they would like fluent interface. It sounded like a pretty cool idea and I also needed an excuse to learn git/github thus was born FluentEmail for .NET.

Here is a quick example of intended usage, the Smtp details if not provided using the .UsingClient(SmptClient client) method will be taken from the mailSettings config section.

var email = Email
            .From("john@email.com")
            .To("bob@email.com", "bob")
            .Subject("hows it going bob")
            .Body("yo dawg, sup?");

//send normally
email.Send();

//send asynchronously
email.SendAsync(MailDeliveredCallback);

The fluent interface is acheived by using a builder pattern. The .From method is static and returns the underlying email object for the other methods to build upon.

public class Email : IHideObjectMembers
{
    private SmtpClient _client;
    public MailMessage Message { get; set; }

    private Email()
    {
        Message = new MailMessage() { IsBodyHtml = true };
        _client = new SmtpClient();
    }

    public static Email From(string emailAddress, string name = "")
    {
        var email = new Email();
        email.Message.From = new MailAddress(emailAddress, name);
        return email;
    }

    public Email To(string emailAddress, string name = "")
    {
        Message.To.Add(new MailAddress(emailAddress, name));
        return this;
    }

    //other methods left out for readability
}

Its early days at the moment but it does support multiple recipients, BCC and CC. Some of the things I would like to eventually include is support for bulk email sending (sending in batches) and easy support for different Smpt clients such as gmail.

You can grab/fork the code at http://github.com/lukencode/FluentEmail. Let me know if you have any comments or suggestions.

blog comments powered by Disqus