M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

Email Templates

This tutorial explains another very useful interface IEmailTemplate defined in M# framework. Implementing this interface helps making email contents dynamic and more manageable.

IEmailTemplate Interface

This interface is defined in "MSharp.Framework.Services" and simply defines four properties, which are commonly required to populate "IEmailQueueItem" type instances. Implementing IEmailTemplate interface in the project gives you full control and management of email contents, as you can develop management UI interfaces to maintain email templates i.e. List and Form modules on the entity which implements IEmailTemplate.

Important: "MandatoryPlaceholders" property defined in IEmailTemplate interface as shown above, is used to insert place holders in the "Body" text, which are then verified on saving an email template and are replaced with actual contents at the time of sending email as we will practice later in this tutorial.

Implementing Interface

In order to implement IEmailTemplate interface we have added a new entity EmailTemplate to our HelloWorld project in M# (For more information on creating entities in M#, please read tutorial Entity, Page, Module).

The screenshot above shows that we have developed an entity EmailTemplate in M# and have implemented four properties as defined IEmailTemplate interface.

The screenshot above demonstrates the partial logic class of our EmailTemplate entity, which implements the IEmailTemplate interface (For details on creating business logic please read tutorial Partial Classes and Business Logic). The highlighted method "EnsurePlaceholders" is an extension method for IEmailTemplate type. M# framework exposes extension methods for IEmailTemplate type to validate and merge place holders. The screenshots below show all the extension methods on IEmailTemplate type:


Utilizing Email Template

We have Implemented IEmailTemplate interface in our business logic. Now, we need to utilize the EmailTemplate entity instance to send an email. In our previous lesson Sending Emails we set up an employee registration confirmation email. In this lesson we will update the code in our Employee business logic to utilize EmailTemplate instance.

For the purpose of this lesson we have used M# "Data" tab to add a new record for our EmailTemplate as shown below. Developing a proper UI to manage email templates is beyond the scope of this tutorial (Please read tutorial Entities, Page, Module for more details on entity Data tab and UI modules)

Important: Please remember that "MandatoryPlaceHolder" property contents must be in uppercase and should be a comma (,) separated list. When you use the place holders in "Body" contents they must be in "[#PLACEHOLDER#]" format. M# extension method "GetPlaceholderTokens" can be used to display place holders in correct format for management purpose.

As shown above, we have updated "SendRegistrationConfirmationEmail" method in our Employee logic class to utilize the email template instance we previously created. The first highlighted code line accesses the email template instance using its "Key" property we specified at the time of saving the instance. If you remember, while creating the Email Template entity in M# we marked our entity as "Instance Accessor". Setting this attribute to "true" allows M# to generate a property for each record and the name of property is chosen based on the "Default Property" attribute.

Next, we have built an anonymous object, which has the properties corresponding to the placeholder list. When we call "MergeSubject" and "MergeBody" methods on our template instance, the M# extension methods use this anonymous type to replace the place holders in body and subject text.