Fluent Email in .NET

See an updated (2018) example of sending in .NET core email using FluentEmail.

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("[email protected]")
            .To("[email protected]", "bob")
            .Subject("hows it going bob")
            .Body("yo dawg, sup?");

//send normally
email.Send();

//send asynchronously
email.SendAsync(MailDeliveredCallback);

The fluent interface is achieved 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.

👋 Are you an 🇦🇺 Australian developer or IT professional? Go check out my site Australian Technology Jobs - a Premium developer and IT job board and find awesome Australian dev jobs (and people).
Questions or comments? Follow me on twitter!
Follow @lukencode