Skip to content

Send a Razor-formatted email in one line of code from any .NET app

License

Notifications You must be signed in to change notification settings

mmooney/MMDB.RazorEmail

Repository files navigation

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> {"[email protected]"},              //and a list of people to send it do
                "[email protected]"                               //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> {"[email protected]"},              //and a list of people to send it do
                "[email protected]",                               //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> {"[email protected]"},              //and a list of people to send it do
                "[email protected]"                               //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> { "[email protected]" },              //and a list of people to send it do
                "[email protected]"                               //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

About

Send a Razor-formatted email in one line of code from any .NET app

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages