# Email message templates

The message templates are used in the message generation process. A message job can use a message template to provide notification. The template is the content of the message that is sent. For example, a message template can be a template for password recovery or a custom newsletters. Each message template can contain placeholders that will be replaced with specific property values. The `IMessageTemplateRequest` class specifies the required information and the `MessageTemplateRequestProxy` class implements it and you can use it as a parameter where the interface is needed.

## Creating a persistent message template and using it with a new message job

The sample below demonstrates how to create a persistent message template with new message job:

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Notifications;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.NotificationService
{
    public partial class NotificationServiceSnippets
    {
        public static void CreatePersistentMessageTemplateWithNewMessageJob()
        {
            var ns = SystemManager.GetNotificationService();
            var context = new ServiceContext("myNotificationAccount", "MyCustomModule");
            var cntxDictionary = new Dictionary<string, string>();
            cntxDictionary.Add("MergeData.Time", DateTime.UtcNow.ToString());
            var subjectTemplate = "Test notification";
            var bodyTemplate = "Hi {|Subscriber.FirstName|} {|Subscriber.LastName|}, the time is: {|MergeData.Time|}";
            //Name of an existing profile
            var profileName = "DefaultSmtpProfile";
            //Id of an existing subscription list
            Guid subscriptionListId = Guid.Empty; 

            IMessageTemplateRequest tmpl = new MessageTemplateRequestProxy()
            {
                Subject = subjectTemplate,
                BodyHtml = bodyTemplate
            };

            var templateId = ns.CreateMessageTemplate(context, tmpl);

            var job = new MessageJobRequestProxy()
            {
                MessageTemplateId = templateId,
                SubscriptionsListId = subscriptionListId,
                SenderProfileName = profileName
            };

            var messageJobId = ns.SendMessage(context, job, cntxDictionary);
        }
    }
}

```

## Creating a new message template and use it with a new message job

The sample below demonstrates how to create a new message template with new message job:

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Notifications;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.NotificationService
{
    public partial class NotificationServiceSnippets
    {
        public static void CreateMessageTemplateWithNewMessageJob()
        {
            var ns = SystemManager.GetNotificationService();
            var context = new ServiceContext("myNotificationAccount", "MyCustomModule");
            var cntxDictionary = new Dictionary<string, string>();
            cntxDictionary.Add("MergeData.Time", DateTime.UtcNow.ToString());
            var profileName = "DefaultSmtpProfile"; //Name of an existing profile
            Guid subscriptionListId = Guid.Empty; //Id of an existing subscription list

            var subjectTemplate = "Test notification";
            var bodyTemplate = "Hi {|Subscriber.FirstName|} {|Subscriber.LastName|}, the time is: {| MergeData.Time|}";
            IMessageTemplateRequest tmpl = new MessageTemplateRequestProxy()
            {
                Subject = subjectTemplate,
                BodyHtml = bodyTemplate
            };

            var job = new MessageJobRequestProxy()
            {
                MessageTemplate = tmpl,
                SubscriptionsListId = subscriptionListId,
                SenderProfileName = profileName
            };

            var messageJobId = ns.SendMessage(context, job, cntxDictionary);
        }
    }
}

```

## Merging message templates with additional information

Merge operations take place when the `Notification service` prepares the message for sending. In general, the template placeholders are replaced with specified merge tags. There are system tags, such as `Subscriber.FirstName` `Subscriber.LastName`, but you can use custom tags, that you pass through the `contextItems` parameter of the `Send` method. For every merge tag to be parsed, it must be surrounded by **{|** and **|}** asin the code sample below. The custom merge tags have a higher priority than the system merge tags. If you specify a custom merge tag (pass it in the `contextItems` dictionary) that is identical to a system merge tag, that is used in the template, the custom merge tag value will be used to replace the placeholder.

## Merging tags while creating a message template

The sample below demonstrates how to send message using merge tags in the message template:

```C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Services.Notifications;

namespace Telerik.Sitefinity.Documentation.CodeSnippets.DeepDive.NotificationService
{
    public partial class NotificationServiceSnippets
    {
        public static void CreateMessageTemplateWithMergeTags()
        {
            var ns = SystemManager.GetNotificationService();
            var context = new ServiceContext("myNotificationAccount", "MyCustomModule");
            var contextDictionary = new Dictionary<string, string>();
            contextDictionary.Add("MergeData.Time", DateTime.UtcNow.ToString());

            List<ISubscriberRequest> subscribers = new List<ISubscriberRequest>();

            var subscriber1 = new SubscriberRequestProxy()
            {
                Email = "subscriber1@telerik.com",
                FirstName = "John",
                LastName = "Davis",
                ResolveKey = "unique-identifier-in-the-specified-context"
            };
            subscribers.Add(subscriber1);

            //Name of an existing profile
            var profileName = "DefaultSmtpProfile";

            var subjectTemplate = "Test notification";
            var bodyTemplate = "Hi {|Subscriber.FirstName|} {|Subscriber.LastName|}, the time is: {|MergeData.Time|}";
            var tmpl = new MessageTemplateRequestProxy() { Subject = subjectTemplate, BodyHtml = bodyTemplate };

            var job = new MessageJobRequestProxy()
            {
                MessageTemplate = tmpl,
                Subscribers = subscribers,
                SenderProfileName = profileName
            };

            var messageJobId = ns.SendMessage(context, job, contextDictionary);
        }
    }
}

```
