Sending Email (Part 1)
Email functionality is a common feature in many applications, which can be triggered by user action (ex.: email confirmation after buying a online product) or direct email (ex.: newsletter).Normally this is done in about 4 steps:
- Get template from some source: database, file, etc..
- Get a business object with information to be personalized;
- Configure message body to be sent;
- Send Email.
Solution 1: Make template with tags to be replaced later. Example:
var template = "Hi [Name], Here's some activity you may have missed on Facebook."; string messageBody = template.Replace("[Name]", "Adilson"); Problem of this Solution:
|
Solution 2: Using Razor Engine(open source project) to build templates. Example:
string template = "Hi @Model.Name, Here's some activity you may have missed on Facebook."; string messageBody = Razor.Parse(template, new { Name = "Adilson" }); Problem of this Solution:
|
Solution 3: Using XML and XSLT to transform documets and to get HTML.
This solution is more flexible than the previous two, as it allow us to use the power of XSLT to Transform documents. More about XSLT can be found in any search engine, but this is my suggestion: w3schools
How to get message body using solution 3:
- Get XSLT file (template);
- Get XML: serializing the business object;
- Call the processor given the XSLT and XML to generate the HTML(message body).
Suppose we are looking to send this newsletter:
|
|||
Newsletter Title Here
|
|||
|
|||
|
|||
|
|||
Copyright
© 2013 Your Company Name. All Rights Reserved.
|
So to implement solution 3, lets summarize the steps:
- Get template from some source: XSLT File;
- Get a business object with information to personalize: Serializing to XML;
- Configure message body to be sent: Applying XSLT Processor to get the message body;
- Send Email.
Sem comentários:
Enviar um comentário