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.
Pingback: DotNetShoutout
Pingback: Simple C# Synchronous / Asynchronous Email Sender « lukencode
If you are going to make fluent interfaces then please make sure that they are “progressive fluent” interfaces. The whole point for fluent interfaces is discoverability and without the progressive part discoverability is somewhat lost.
Cheers
Kane
public Email Body(string body, bool isHtml = true)
{}
“bool isHtml = true” looks like .net 4.0 syntax…
Well I will give you credit for reading the comments to your posts as the Fluent EMail API was my suggestion on your last post. I am glad to see that you put it in action. I have a single class file that I have been using in all of my projects that provides the same API. I just need to add the Async option.
Hi,
Very nice implementation. I’ve done something similar missing only async sending. You can check it out here:
http://acarrilho.com/acarrilhograffiti/dot-net/my-net-c-global-class-does-almost-everything/
and in more detail here (with bulk sending):
http://acarrilho.com/acarrilhograffiti/dot-net/a-new-feature-on-the-mailhelper-class/
Basically it’s an assembly with a bunch of functionalities: HttpHelper, RestHelper, SerializerHelper (XmlSerializer and DataContractSerializer), MailHelper (Fluent API) and others.
Cheers
@Bret
cool bret, is it open sourced somewhere? What I’ve done so far is more of a proof of concept, I’d like to create/find something a bit more robust.
@Dan
Yeah dan thats .NET 4. I wanted to get something up quickly, If I do continue to work on it I think I will move it to 3.5
@Kane – what do you mean by “progressive fluent” this is the first fluent interface I’ve attempted and I’d like to find out.
Pingback: User Control Email Templates in asp.net « lukencode
Check out this fluent API for .NET 4.0
http://www.avantprime.com/products/view-product/8/fluent-mail
Nice api, I like the template stuff. Was thinking of trying to do something with razor for fluent email templates.