views:

270

answers:

4

I am about to write a simple email manager for the site I'm working on (asp.net/c#); the site sends out various emails, like on account creation, news, some user actions, etc. So there will be some email templates with placeholders like [$FirstName] which will be replaced by actual values. Pretty standard stuff. I'm just wondering if someone can advise on existing code - again, i need something very simple, without many bells/whistles, and obviously with source code (and free)

Any ideas/comments will be highly appreciated!

Thanks, Andrey

A: 

If you are using ASP.NET, you already have a templating engine available to you. Simply create an ASP.NET page that will produce the results for you (using whatever controls, etc, etc) you want, as well as setting the ContentType of the response to the appropriate type (either text or HTML, depending on the email format)

Make sure that this url is not publically exposed.

Then, in your code, you would create an HttpWebRequest/HttpWebResponse or WebClient and then fetch the URL and get the contents. The ASP.NET engine will process the request and return your formatted results, which you can then email.

If you want something simpler, why not use a RegEx and match? Just make sure you have a fairly unique identifer for your fields (prefix and suffix, which you can guarantee will never be used, or, you can at least write an escape sequence for it) and you could easily use the Match method to do the replace.

The only "gotcha" to the RegEx approach is that if you have to do embedded templating, then that's going to require a little more work.

casperOne
This seems to be the wrong tool for the job to me and you will run into a lot of problems since Outlook does not render what the .NET framework considers standard HTML. And you have to run a whole page cycle for what could be achieved in much less code.
Hogan
A: 

Having just done this myself, there is some great information at: Sending Email Both in HTML and Plain Text. Best part is, you don't need anything other than .NET.

Essentially, you create an HTML page (AKA, your formatted e-mail) with the tags that you want to replace (in the case of this solution, tags will be in the format of: <%TAGNAME%>). You then utilize the information found at the above website to create a mail template with the tags filled with the appropriate data, and the injections will be done for you into your HTML template. Then, you just use the SMTP classes built into .NET and send the mail on its way. It's very simple and straightforward.

Let me know if you have any additional questions. Hope that helps!

JasCav
+1  A: 

I always do the following. Templates = text string with {#} placeholders. To use a template I load the string (from whatever store) and then call string.Format(template,param1,param2..)

Simple and works well. When you need something stronger you can move to a framework of some kind but string.format has always worked well for me.

note

Alison R's link takes this method to the next step using 3.5's anonymous types to great effect. If you are 3.5 I recommend using the FormatWith there (I will) otherwise this way works well.

Hogan
While this may be simple, it doesn't separate content and presentation. And, since e-mails are being used, why not use what the .NET framework already has built in?
JasCav
Not sure what you are saying... the {0} are the content and the template is the presentation.
Hogan
+2  A: 

There are several threads on Stack Overflow about this already, but I ended up rolling my own solution from various suggestions there.

I used this FormatWith extension method to take care of simple templating, and then I made a basic Email base class to take care of common tasks, like pulling in an appropriate template and replacing all the requisite info, as well as providing a Send() method.

All the emails I need to send have their own subclass deriving from the base, and define things unique to them, such as TemplateText, BindingData, Recipients, and Subject. Having them each in their own class makes them very easy to unit test idependently of the rest of the app.

So that your app can work with these email classes without really caring which one it's using, it's also a good idea to implement an interface, with any shared methods (the only one I cared about was Send()), so then your app can instantiate whatever email class it wants and work with them in the same way. Maybe generics could be used, too, but this was what I came up with.

IEmail email = new MyEmailClass();
email.Send();

Edit: There are many more suggestions here: http://stackoverflow.com/questions/620265/can-i-set-up-html-email-templates-in-c-on-asp-net

Alison R.