Josh Earl
Josh Earl

Reputation: 18351

.NET frameworks for formatting e-mail messages?

Are there any open source/free frameworks available that take some of the pain out of building HTML e-mails in C#?

I maintain a number of standalone ASP.NET web forms whose main function is to send an e-mail. Most of these are in plain text format right now, because doing a nice HTML presentation is just too tedious.

I'd also be interested in other approaches to tackling this same problem.

EDIT: To be clear, I'm interested in taking plain text form input (name, address, phone number) and dropping it into an HTML e-mail template. That way the receipient would see a nicely formatted message instead of the primitive text output we're currently giving them.

EDIT 2: As I'm thinking more about this and about the answers the question has generated so far, I'm getting a clearer picture of what I'm looking for. Ideally I'd like a new class that would allow me to go:

  HtmlMessage body = new HtmlMessage();
  body.Header(imageLink);
  body.Title("Some Text That Will Display as a Header");
  body.Rows.Add("First Name", FirstName.Text);

The HtmlMessage class builds out a table, drops the images in place and adds new rows for each field that I add. It doesn't seem like it would be that hard to write, so if there's nothing out there, maybe I'll go that route

Upvotes: 1

Views: 483

Answers (3)

StriplingWarrior
StriplingWarrior

Reputation: 156534

I recently implemented what you're describing using MarkDownSharp. It was pretty much painless.

It's the same framework (minus a few tweaks) that StackOverflow uses to take plain-text-formatted posts and make them look like nice HTML.

Another option would be to use something like TinyMCE to give your users a WYWIWYG HTML editor. This would give them more power over the look and feel of their emails, but it might just overcomplicate things.

Bear in mind that there are also some security issues with user-generated HTML. Regardless of which strategy you use, you need to make sure you sanitize the user's input so they can't include scary things like script tags in their input.

Edit

Sorry, I didn't realize you were looking for an email templating solution. The simplest solution I've come up with is to enable text "macros" in user-generated content emails. So, for example, the user could input:

 Dear {RecipientFirstName},

  Thank you for your interest in {ClientCompanyName}. The position you applied for has the following minimum requirements:

 - B.S. or greater in Computer Science or related field
 - ...

And then we'd do some simple parsing to break this down to:

 Dear {0},

 Thank you for your interest in {1}. The position you applied for has the following minimum requirements:

 - B.S. or greater in Computer Science or related field
 - ...

... and ...

0 = "RecipientFirstName"
1 = "ClientCompanyName"
...

We store these two components in our database, and whenever we're ready to create a new instance from this template, we evaluate the values of the given property names, and use a standard format string call to generate the actual content.

string.Format(s, macroCodes.Select(c => EvaluateMacroCode(c, obj)).ToArray());

Then I use MarkdownSharp, along with some HTML sanitizing methods, to produce a nicely-formatted HTML email message:

Dear John,

Thank you for your interest in Microsoft. The position you applied for has the following minimum requirements:

  • B.S. or greater in Computer Science or related field
  • ...

I'd be curious to know if there's something better out there, but I haven't found anything yet.

Upvotes: 0

Ryan Shripat
Ryan Shripat

Reputation: 5704

The C# port of StringTemplate worked well for me. I highly recommend it. The template file can have a number of named tokens like this:

...
    <b>
        Your information to login is as follows:<br />
        Username: $username$<br />
        Password: $password$<br />
    </b>
...

...and you can load this template and populate it like this:

    notificationTemplate.SetAttribute("username", Username);
    notificationTemplate.SetAttribute("password", Password);

At the end, you get the ToString() of the template and assign it to the MailMessage.Body property.

Upvotes: 0

John Sheehan
John Sheehan

Reputation: 78124

Andrew Davey created Postal which lets you do templated emails using any of the ASP.NET MVC view engines. Here's a video where he talks about how to use it.

His examples:

public class HomeController : Controller {
    public ActionResult Index() {
        dynamic email = new Email("Example");
        email.To = "[email protected]";
        email.FunnyLink = DB.GetRandomLolcatLink();
        email.Send();

        return View();
    }
}

And the template using Razor:

To: @ViewBag.To From: [email protected] Subject: Important Message

Hello, You wanted important web links right? Check out this:
@ViewBag.FunnyLink

<3

Upvotes: 1

Related Questions