|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionMost web applications and quite a few desktop apps need to send emails from time to time: registration and activation mails, error warnings, confirmations etc. If not available through a database or CMS, the contents of these emails are mainly stored in text, HTML or XML files. I always considered this handling of template files to be a tedious task, so I came up with this solution that simplifies things remarkably. Basically, this library handles the organization of arbitrary - optionally localized - template files for you. I really wanted to keep things simple and get a generic solution that does not rely on a database. As a result, you just have to create a single XML file that links to your templates to get things working. Using the library at runtime is easy. To retrieve and send a localized template to a given recipient, you're done with three lines of code: //get the template handler
TemplateHandler handler = TemplateHandler.Instance;
//get a french registration template
MailTemplate template = handler["registration",
new CultureInfo("fr"];
//send the template
handler.Send(template, "anybody@xxx.xxx");
Contents:Setting up the XML fileAll templates of your application are configured in a single XML file. You need to tell the framework where to find this file by adding the key <appSettings>
<add key="Evolve.TemplateConfiguration"
value="~/mailings/templates.config" />
</appSettings>
Here is a simple example of a configuration file. It contains the settings for two template groups:
<?xml version="1.0" encoding="utf-8" ?>
<TemplateConfiguration>
<BaseDirectory>template samples</BaseDirectory>
<SmtpServer>mail.xxx.xxx</SmtpServer>
<Groups>
<!-- first group, localized templates -->
<TemplateGroup TemplateGroupId="forumregistration"
Sender="forums@xxx.xxx"
MailFormat="Text">
<Templates>
<Template IsDefault="true">
<Subject>your registration</Subject>
<File>forum/registration_en.txt</File>
</Template>
<Template Locale="de">
<Subject>Ihre Registrierung</Subject>
<File>forum/registration_de.txt</File>
</Template>
</Templates>
</TemplateGroup>
<!-- second group, no localization -->
<TemplateGroup TemplateGroupId="greetingcard"
Sender="ecards@xxx.xxx"
MailFormat="Html">
<Templates>
<Template IsDefault="true">
<Subject>You got an eCard from {0}</Subject>
<File>ecards/notification.txt</File>
</Template>
</Templates>
</TemplateGroup>
</Groups>
</TemplateConfiguration>
TemplateConfiguration elementThis is the root element of the configuration file. It contains general settings that apply to all templates.
TemplateGroup elementAs you can see, a configuration file contains several
Template elementEvery template group contains a
Working with the codeMailTemplateA
You can easily adjust and format the contents of your TemplateHandler
The TemplateHandler handler = TemplateHander.Instance;
//get the default template of the above sample
MailTemplate template1 = handler["forumregistration"];
//get the german template
MailTemplate template2 = handler["forumregistration",
new CultureInfo("de")];
//this returns also the default template, as there is no french template:
MailTemplate template3 = handler["forumregistration",
new CultureInfo("fr")];
//send the template
handler.Send(template3, "info@xxx.xxx");
Hook into the sending processWhile just sending a mail through the public delegate void TemplateCallbackHandler(MailTemplate template,
MailMessage message);
If you use this feature, a callback handler is called by the framework before the private void Send(MailTemplate template)
{
TemplateHandler handler = TemplateHandler.Instance;
//create delegate
TemplateCallbackHandler cb;
cb = new TemplateCallbackHandler(HandleMessageCallback));
//send message
handler.Send(template, "info@xxx.xx", cb);
}
//this is the callback handler
private void HandleMessageCallback(MailTemplate template,
MailMessage message)
{
//add file
MailAttachment att = new MailAttachment("terms.pdf");
message.Attachments.Add(att);
}
Formatting your contentMost of your templates will consist of dynamic parts: you need to include IDs and passwords, email addresses etc. The framework makes no assumptions about this. Let's say you need to format the subject of an email. In this sample, I just included the format placeholder <Template IsDefault="true">
<Subject>You got an eCard from {0}</Subject>
<File>ecards/notification.txt</File>
</Template>
To adjust the subject, my code would look like this: MailTemplate template = handler["greetingcard"];
template.Subject = String.Format(template.Subject, "Mr. Tarantino");
handler.Send(template, "info@xxxx.xx");
This would result in a mail with the following subject: You got an eCard from Mr. Tarantino. The PropertyMapper helper class
However, I've built a simple helper class that automates the assignment of values to placeholders for you. There's no need to use it but it can come handy. The simple idea behind the Example:Hello #?FirstName?#
Your email address is #?EmailAddress?#
Now, let's assume you have a
What you can do now is feed a //get a user class
User user = new User(123);
//get a mail template
MailTemplate template = GetUserTemplate();
//create a mapper class with the User instance
PropertyMapper mapper = new PropertyMapper(user);
//map the object properties to the template
mapper.MapTemplate(template);
A few remarks:
MailTemplate lifecycleThe diagram below shows all processes from the retrieval of a template to its transmission:
Points of interestI haven't covered the internals of the framework in this article. However, in case you want to know how things work, the code is well documented and should be easy to understand. Some points of interest:
The ZIP file contains the source, a sample (WinForms-) application that works upon different templates, and a few NUnit tests. NewsletterThis is the first release of the library, so there will probably be some enhancements / fixes. If you're using the library, I recommend to check back here from time to time. If you prefer to keep notified, you can subscribe to the corresponding newsletter on my site: Newsletter subscription. ConclusionThis is a handy little library which can make your life a bit simpler if you're working with file-based templates. It's dead simple - and that's exactly what it needs to be. Enjoy :-). History
| ||||||||||||||||||||||||||||||||||||||||||||||||||||