HowTo - MailTemplate Service

By default the mail templates are stored in the WebServices\App_Data\MailTemplates directory.

This path can be changed in the Web.config file.

<mailTemplateServiceConfiguration
  path="App_Data\MailTemplates" />

Note

All the *.xml files in the WebServices\App_Data\MailTemplates folder will be copied in the Tests\App_Data\MailTemplates\ when the WebServices project is compiled (thus templates should be tested in the unit tests context).

1. Mail Template

Here an example of a mail template (OnUserCreated.xml) used when a new user is created to the database.

<?xml version="1.0" encoding="utf-8" ?>
<Templates>
  
  <Template culture="EN">
    <Subject>
      <![CDATA[Welcome %User.Username%]]>
    </Subject>
    <Message>
      <![CDATA[
<div id="header">
    <em>Site logo here...</em>
    <!--<img src="http://www.your-site.com/images/mail-logo.png" alt="Site slogan here" />-->
</div>
<div id="content">
    <p>Welcome and thanks for joining!</p>
    <p>Here is your login information:</p>
    <p>Username: %User.Username%</p>
    <p>Password: ********</p>
    <p>Email: %User.Email%</p>
    <p>Secret question: %User.PasswordQuestion%</p>
    <p>Secret response: %User.PasswordResponse%*******</p>
    <p>If you have any questions, please do not hesitate to contact us.</p>
</div>
<div id="footer">
    <br />
    <p>Best regards,<p/>
    <p>Com.Example.Labs Staff<br />
      <a href="http://www.your-site.com">http://www.your-site.com</a><br />
      <a href="mailto:contact@your-site.com">contact@your-site.com</a>
    </p>
</div>
      ]]>
    </Message>
  </Template>
  
  <Template culture="FR">
    <Subject>
      <![CDATA[...]]>
    </Subject>
    <Message>
      <![CDATA[...]]>
    </Message>
  </Template>
  
  <Template culture="ES">
    <Subject>
      <![CDATA[...]]>
    </Subject>
    <Message>
      <![CDATA[...]]>
    </Message>
  </Template>
  
  <Template culture="DE">
    <Subject>
      <![CDATA[...]]>
    </Subject>
    <Message>
      <![CDATA[...]]>
    </Message>
  </Template>

</Templates>

2. Code Sample

Here how to load a template, fill it and send it by mail.

// Tests

namespace Com.Example.Labs.Tests
{
    using System.Collections;
    using System.Collections.Generic;

    using Microsoft.VisualStudio.TestTools.UnitTesting;

    using LayerCake.Generator.Core;
    using LayerCake.Generator.Models;

    [TestClass]
    public class MailTemplateServiceTests
    {
        [TestMethod]
        public void MailTemplateServiceTests_Example()
        {
            User user = new User
            {
                Username = "jdoe",
                Email = "john@doe.com",
                PasswordQuestion = "My pet's name",
                PasswordResponse = "Deus",
                Culture = Cultures.EN
            };

            // Save this User in the database...
            // ... 

            // 1. Create the service with the 'OnUserCreated' template
            // (name of the xml file without the extension)
            var template = new MailTemplateService("OnUserCreated");

            // 2. Declare the tokens (key, value) to replace in the template
            var tokens = new Dictionary<string, object>();

            tokens.Add("%User.Username%", user.Username);
            tokens.Add("%User.Email%", user.Email);
            tokens.Add("%User.PasswordQuestion%", user.PasswordQuestion);
            tokens.Add("%User.PasswordResponse%", user.PasswordResponse[0]);

            // 3. Load the template with the User's culture and the token collection
            template.Load(user.Culture, tokens);

            // 4. Send the transformed template
            template.SendByMail(user.Email, new SmtpServiceOptions { IsHtmlBody = true });
        }
    }
}