Need help with MailBody?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

doxakis
150 Stars 8 Forks MIT License 48 Commits 1 Opened issues

Description

Create transactional email with a fluent interface (.net)

Services available

!
?

Need anything else?

Contributors list

# 374,446
C#
Visual ...
dotnetc...
fluent-...
40 commits
# 250,914
Raspber...
Bash
zigbee
C#
2 commits

MailBody Build Status NuGet Status

MailBody is a library for generating transactional email by using a fluent interface.

The current mail template is based on https://github.com/leemunroe/responsive-html-email-template (MIT License 2013 Lee Munroe)

Supported framework

  • dotnet core 1.0
  • .net framework 4.5 (c#, vb)

Install from Nuget

To get the latest version:

Install-Package MailBody

MailBody Editor

If you want to create email with the MailBody library while having a live preview, you can use the MailBody Editor

Preview

Quick Examples

Email Address Confirmation

C# syntax:

var body = MailBody
    .CreateBody()
    .Paragraph("Please confirm your email address by clicking the link below.")
    .Paragraph("We may need to send you critical information about our service and it is important that we have an accurate email address.")
    .Button("https://example.com/", "Confirm Email Address")
    .Paragraph("— [Insert company name here]")
    .ToString();

Visual Basic syntax:

Dim body As String = MailBody.CreateBody() _
    .Paragraph("Please confirm your email address by clicking the link below.") _
    .Paragraph("We may need to send you critical information about our service and it is important that we have an accurate email address.") _
    .Button("https://example.com/", "Confirm Email Address") _
    .Paragraph("— [Insert company name here]") _
    .ToString()

Preview

Password reset

var appName = "My app";

var body = MailBody .CreateBody() .Paragraph("Hi,") .Paragraph("You're receiving this email because someone requested a password reset for your user account at " + appName + ".") .Button("https://www.example.com/", "Reset password") .Paragraph("Thanks for using " + appName + "!") .Paragraph("— [Insert company name here]") .ToString();

Preview

Order confirmation

var products = new string[] { "1 x Product A", "2 x Product B", "3 x Product C" };

// Format product display. var items = products.Select(item => MailBody.CreateBlock().Text(item));

var body = MailBody .CreateBody() .Title("Confirmation of your order") .Paragraph("Hello,") .Paragraph("We confirm having received your order.") .Paragraph("Here is the list of ordered items:") .UnorderedList(items) .Paragraph("Thank you for ordering from us!") .Paragraph("— [Insert company name here]") .ToString();

Preview

Notification

var productName = "ABC";
var productStatus = "available";
var productDescription = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla sagittis nisl ut tellus egestas facilisis. Nulla eget erat dictum, facilisis libero sit amet, sollicitudin tortor. Morbi iaculis, urna eu tincidunt dapibus, sapien ex dictum nibh, non congue urna tellus vitae risus.";
var components = new string[] { "Part A", "Part B" };

// Format product display. var items = components.Select(item => MailBody.CreateBlock().Text(item));

var body = MailBody .CreateBody() .Paragraph("Hello,") .Paragraph("The product " + productName + " is now " + productStatus + ".") .SubTitle("Here is the product summary:") .Paragraph(MailBody.CreateBlock() .StrongText("Product name: ").Text(productName)) .Paragraph(MailBody.CreateBlock() .StrongText("Description: ").Text(productDescription)) .Paragraph(MailBody.CreateBlock() .StrongText("Components:")) .UnorderedList(items) .Paragraph("— [Insert company name here]") .ToString();

Preview

With footer

var footer = MailBody
    .CreateBlock()
    .Text("Follow ")
    .Link("http://twitter.com/example", "@Example")
    .Text(" on Twitter.");

var body = MailBody .CreateBody(footer) .Paragraph("Please confirm your email address by clicking the link below.") .Paragraph("We may need to send you critical information about our service and it is important that we have an accurate email address.") .Button("https://www.example.com/", "Confirm Email Address") .Paragraph("— [Insert company name here]") .ToString();

Preview

With image

Please note You can use CID Embedded Images, Base64 Encoding or use absolute url. Image may not appear on all email client. So, make sure to do some tests.

var body = MailBody
    .CreateBody()
    .Image("https://placehold.it/540x70/ffffff/e8117f?text=My+logo", "My company name")
    .Paragraph("Please confirm your email address by clicking the link below.")
    .Paragraph("We may need to send you critical information about our service and it is important that we have an accurate email address.")
    .Button("https://example.com/", "Confirm Email Address")
    .Paragraph("— [Insert company name here]")
    .ToString();

Preview

Custom theme & Raw html

var template = MailBodyTemplate.GetDefaultTemplate();
template
    .Paragraph(m =>
        "

{m.Content}

") .Body(m => "" + m.Content + "
" + m.Footer + "") .Text(m => $"{m.Content}");

var footer = MailBody .CreateBlock() .Text("Follow ", new { color = "red"}) .Link("http://twitter.com/example", "@Example") .Text(" on Twitter.", new { color = "#009900", backgroundColor = "#CCCCCC", fontWeight = "bold" });

var body = MailBody .CreateBody(template, footer) .Paragraph("Please confirm your email address by clicking the link below.") .Raw("

We may need to send you critical information about our service and it is important that we have an accurate email address.

") .Button("https://www.example.com/", "Confirm Email Address") .Paragraph("— [Insert company name here]", new { color = "white", backgroundColor = "black" }) .ToString();

Preview

Another way to create your email

var body = MailBody.CreateBody();

body.Paragraph("Hi,") .Paragraph("First paragraph..");

// Your code

body.Button("https://www.example.com/", "First button"); body.Paragraph("Another paragraph..");

// Your code

body.Button("https://www.example.com/", "Second button") .Paragraph("— [Insert company name here]");

var htmlBody = body.ToString();

Preview

Override the default template

This example is based on Postmark templates.

var template = MailBodyTemplate.GetDefaultTemplate()
    .Paragraph(m => $"

{m.Content}

") .Link(m => $"{m.Content}") .Title(m => $"

{m.Content}

") .SubTitle(m => $"

{m.Content}

") .Text(m => $"{m.Content}") .StrongText(m => $"{m.Content}") .UnorderedList(m => $"
    {m.Content}
") .OrderedList(m => $"
    {m.Content}
") .ListItem(m => $"
  • {m.Content}
  • ") .LineBreak(m => $"") .Button(m => @"
    " + m.Content + @"
    ") .Block(m => m.Content) .Body(m => @"... (see the examples for the complete source) ...");

    var body = MailBody .CreateBody(template) .Paragraph("Please confirm your email address by clicking the link below.") .Paragraph("We may need to send you critical information about our service and it is important that we have an accurate email address.") .Button("https://example.com/", "Confirm Email Address") .Paragraph("— [Insert company name here]") .ToString();

    Preview

    Copyright and license

    Code released under the MIT license.

    We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.