Notifications client

The SDK provides a Notifications client to manage and send notifications.


The client variable in the following code examples refers to the IMClient instance. When using the Web SDK, the variable name can be chosen freely, but it is also called client at instantiation in the documentation.

E-mail templates

Mail template entities contain the templates for sending e-mails. The variables can be resolved at runtime.

IMailTemplateEntity is a subtype of IEntity. It provides C# properties and methods for the members on the M.Mailing.Template entity definition.

The client provides extra getters for getting these mail templates, that are not available on the entities client. It allows fetching mail templates by name. However, every operation on an IEntity still applies to IMailTemplateEntity. Saving and deleting IMailTemplateEntity objects are still done through the entities client.

Getting an e-mail template

To get an e-mail template by name, for example the template for when a user has forgotten their password:

var template = await MClient.Notifications.GetMailTemplateEntityAsync("ForgotPassword");

Note that it is still possible to get the mail template by id as well:

var entity = await MClient.Entities.GetAsync(id);
// create an instance of a typed IMailTemplateEntity
IMailTemplateEntity template = Client.TypedEntityFactory.FromEntity<IMailTemplateEntity>(entity);

Creating an e-mail template

A simple hello world template could look like this:

CultureInfo enUs = CultureInfo.GetCultureInfo("en-US");
var entity = await MClient.EntityFactory.CreateAsync(Constants.MailTemplate.DefinitionName);
var template = Client.TypedEntityFactory.FromEntity<IMailTemplateEntity>(entity);
template.Name = "Hello world template";
template.Subject[enUs] = "Hello there!";
template.Body[enUs] =  "Hello {{Username}}!";

var templateVariable = new TemplateVariable
    Name = "Username",
    VariableType = TemplateVariableType.String
template.SetTemplateVariables(new[] { templateVariable });

await MClient.Entities.SaveAsync(template);

Sending e-mail notifications

E-mails can be sent to users by username, user id or by a broadcast. There are three respective overloads to the SendEmailNotificationAsync. It accepts the following input:

  • MailRequestById: for specifying users by id.
  • MailRequestByUsername: for specifying users by username.
  • MailRequestBroadcast: sends to all users.

An e-mail broadcast could be sent using the template previously created:

var request = new MailRequestBroadcast
    MailTemplateName = "Hello world template"
request.Variables.Add("Username", "world");

await MClient.Notifications.SendEmailNotificationAsync(request);

This would send an e-mail, containing "Hello world!", to everyone.

Sending real-time notifications

Real-time notifications can be sent to users by username, user id or by a broadcast. There are three respective overloads to the SendRealTimeNotificationAsync. It accepts the following input:

  • RealtimeRequestById: for specifying users by id.
  • RealtimeRequestByUsername: for specifying users by username.
  • RealtimeRequestBroadcast: sends to all users.

A real-time notification could be sent to the DemoUser:

var request = new RealtimeRequestByUsername()
    Title = "Your notification title",
    Link = new System.Uri("https://example.com")
request.SetBody("Your description");

await MClient.Notifications.SendRealTimeNotificationAsync(request);

The link is opened when the user clicks on the notification.

This real-time request interface is built on the standard notifications API: https://developer.mozilla.org/en-US/docs/Web/API/notification. This API uses an options object that contains all the notification information. This object is directly passed from the IRealtimeRequest.Options property. Some helper methods have been provided to help configure this options object, for example the body and the icon of the notification.

Sending confirmation e-mails


For SendConfirmationEmailAsync to work, you need to set EnableConfirmationMail to true in the settings.

Sending confirmation e-mails to users can be done using ids or usernames:

await MClient.Notifications.SendConfirmationEmailAsync("DemoUser");

This will send an e-mail to the specified user containing a secure link to activate their account.

Can we improve this article ? Provide feedback