Send a Razor-formatted email in one line of code from any .NET app
C#
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.nuget
MMDB.RazorEmail.Tests
MMDB.RazorEmail
MMDB.RazorEmailSample
packages
.gitattributes
.gitignore
MMDB.RazorEmail.sln
SampleAppScreenShot.png
license.txt
readme.md

readme.md

MMDB Razor Email

Format an object into HTML with Razor and send it via email in one line of code, from any .NET app.##

NuGet: http://nuget.org/packages/MMDB.RazorEmail

Need to send an email. Have a bunch of data, and want to actually look nice? How about using some of that fancy Razor stuff that the ASP.NET guys have cooked up?

Basic Usage

    new RazorEmailEngine()
          .SendEmail("This is the subject",                       //Give a subject
                new {FirstName="Mike", LastName="Mooney" },       //and a model object
                "Hello <b>@Model.FirstName @Model.LastName</b>!", //and a Razor view
                new List<string> {"to@example.com"},              //and a list of people to send it do
                "from@example.com"                               //and who you are
          );

Include Some Attachments

    new RazorEmailEngine()
          .SendEmail("This is the subject",                       //Give a subject
                new {FirstName="Mike", LastName="Mooney" },       //and a model object
                "Hello <b>@Model.FirstName @Model.LastName</b>!", //and a Razor view
                new List<string> {"to@example.com"},              //and a list of people to send it do
                "from@example.com",                               //and who you are
                new EmailAttachmentData { FileName="Attachement1.txt", AttachmentData="This is the file data"},
                new EmailAttachmentData { FileName="Attachement2.txt", AttachmentData="This is more data"}
          );

Specify SMTP Settings

The previous examples use the default SMTP settings from the app.config or web.config. However, if you wanted to specify different settings at runtime:

    var settings = new EmailServerSettings
    {
          Host="mail.example.com",
          Port=25,
          UserName="testuser",
          Password="ThisIsASuperDuperPassword"
    };
    new RazorEmailEngine(settings)
          .SendEmail("This is the subject",                       //Give a subject
                new {FirstName="Mike", LastName="Mooney" },       //and a model object
                "Hello <b>@Model.FirstName @Model.LastName</b>!", //and a Razor view
                new List<string> {"to@example.com"},              //and a list of people to send it do
                "from@example.com"                               //and who you are
          );

Mock The Email Engine

Want your unit tests to verify that the right emails are being sent, without actually sending the email? You can inject a mock EmailSender class

    Mock<EmailSender> mockEmailSender = new Mock<EmailSender>();
    new RazorEmailEngine(mockEmailSender.Object)
          .SendEmail("This is the subject",                       //Give a subject
                new { FirstName = "Mike", LastName = "Mooney" },       //and a model object
                "Hello <b>@Model.FirstName @Model.LastName</b>!", //and a Razor view
                new List<string> { "to@example.com" },              //and a list of people to send it do
                "from@example.com"                               //and who you are
          );
    mockEmailSender.Verify(i => i.SendEmail("This is the subject", "Hello <b>Mike Mooney</b>!", It.IsAny<IEnumerable<MailAddress>>(), It.IsAny<MailAddress>()), Times.Once());

Sample App

We've included a working sample app you can play with:

FAQ

Thanks

MMDB.RazorEmail makes use of the following open source projects:

We also used the following in our testing:

githalytics.com alpha