Email message jobs
Each message job contains the information required for a message to be sent – actual message text, sender information, and recipient(s). The IMessageJobRequest interface defines the information needed for sending a message. The MessageJobRequestProxy class provides the implementation and you can use where the interface is required.
The processing of message jobs is an asynchronous operation - you are receiving the control immediately after calling the Notification service SendMessage method.
Creating a message job and sending it for execution
There are several ways that you can use to create a message job, depending on the settings you require, the sent content, and to the recipients. Here's a sample with minimum amount of code:
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 CreateMessageJobAndSendForExecution()
{
var ns = SystemManager.GetNotificationService();
var context = new ServiceContext("myNotificationAccount", "MyCustomModule");
//Name of an existing profile
var profileName = "DefaultSmtpProfile";
//Id of an existing template
Guid templateId = Guid.Empty;
//Id of an existing subscription list
Guid subscriptionListId = Guid.Empty;
IMessageJobRequest job = new MessageJobRequestProxy()
{
MessageTemplateId = templateId,
SubscriptionsListId = subscriptionListId,
SenderProfileName = profileName
};
var messageJobId = ns.SendMessage(context, job, null);
}
}
}
When you must create a mailing list before you send a message, you have two alternatives - create explicitly the mailing list or use dynamic mailing lists. For more information, see Mailing lists.
To create a mailing list before sending message, use the following code:
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 CreateSubscribtionListBeforeSendMessage()
{
var ns = SystemManager.GetNotificationService();
var context = new ServiceContext("myNotificationAccount", "MyCustomModule");
var contextDictionary = new Dictionary<string, string>();
contextDictionary.Add("MergeData.Time", DateTime.UtcNow.ToString());
//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
};
ISubscriberRequest subscriber = new SubscriberRequestProxy()
{
Email = "test.subscriber@company.com",
FirstName = "David",
LastName = "Daniels",
ResolveKey = "unique-identifier-in-the-specified-context"
};
//Creates and persists the subscription list
ISubscriptionListRequest subscriptionList = new SubscriptionListRequestProxy()
{
Title = "SampleList",
ResolveKey = "unique-identifier-in-the-specified-context",
Description = "Sample subscirption lsit description."
};
Guid subscriptionListId = ns.CreateSubscriptionList(context, subscriptionList);
//Subscribe the subscriber to the newly created subscription list
ns.Subscribe(context, subscriptionListId, subscriber);
//Create a message job
IMessageJobRequest job = new MessageJobRequestProxy()
{
MessageTemplate = tmpl,
SubscriptionsListId = subscriptionListId,
SenderProfileName = profileName
};
//Send the message job for processing
var messageJobId = ns.SendMessage(context, job, contextDictionary);
}
}
}
To use dynamic mailing lists, without explicitly creating one, use the following code:
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 CreateDynamicSubscriptionListBeforeSendMessage()
{
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-1"
};
subscribers.Add(subscriber1);
var subscriber2 = new SubscriberRequestProxy()
{
Email = "subscriber2@telerik.com",
FirstName = "David",
LastName = "Daniels",
ResolveKey = "unique-identifier-in-the-specified-context-2"
};
subscribers.Add(subscriber2);
var profileName = "DefaultSmtpProfile"; //Name of an existing profile
var subjectTemplate = "Test notification";
var bodyTemplate = "Hi {|Subscriber.FirstName|} {|Subscriber.LastName|}, the time is: {|MergeData.Time|}";
var tmpl = new MessageTemplateRequestProxy() { Subject = subjectTemplate, BodyHtml = bodyTemplate };
IMessageJobRequest job = new MessageJobRequestProxy()
{
MessageTemplate = tmpl,
Subscribers = subscribers,
SenderProfileName = profileName
};
var messageJobId = ns.SendMessage(context, job, contextDictionary);
}
}
}
In order to convert an existing mailing list into a dynamic subscription list, you can use the following code sample:
using System;
using System.Collections.Generic;
using Telerik.Sitefinity.Modules.Newsletters;
using Telerik.Sitefinity.Services.Notifications;
namespace SitefinityWebApp
{
public class ConvertMailingListToDynamic
{
public static IList<ISubscriberRequest> ConvertMailingListToDynamicMaillingList(Guid mailListId)
{
var newslettersManager = NewslettersManager.GetManager();
var mailList = newslettersManager.GetMailingList(mailListId);
var subscribers = mailList.Subscribers();
List<ISubscriberRequest> result = new List<ISubscriberRequest>();
foreach (var subscriber in subscribers)
{
result.Add(new SubscriberRequestProxy()
{
Email = subscriber.Email,
FirstName = subscriber.FirstName,
LastName = subscriber.LastName,
ResolveKey = subscriber.Id.ToString()
});
}
return result;
}
}
}