The Client API for Email Experience Manager has changed significantly from Sitecore 8.2 and Sitecore 9.1. The example that you provided is using a fairly clunky way of sending a message to a contact using the SendingManager
which is only available on the Content Management server and can not be instantiated on the Content Delivery servers.
To better understand what methods that should be used to send email to specific contacts (which I'll refer to as triggered messages or Automated Message) depend on what the use cases are. Unless there's a very specific, custom use case for your business, using code / API should be a last resort.
Sending Triggered Email to Contacts
Method 1: Through Marketing Automation
Use Case: If Contact triggers Goal A, I want to send an email.
In this simple example (which can be more complex and still utilize Marketing Automation), you are actually creating a Marketing Automation plan that detects the goal being triggered and then sends an EXM Automated Message. From my blog post Marketing Automation with EXM here is an image detailing how you would construct such a plan.
Method 2: Through Sitecore Forms
Use Case: If Contact completes a form, send an email.
In Sitecore 9+, Sitecore Forms has been introduced which allows forms that accept form data and execute actions (called Submit Actions) once the form has been submitted. One of these submit actions is "Send Email Campaign" that will send an email campaign to the contact that is submitting the form.
There is a note here that, there's an expectation that the contact is already known and that we have an email address already in the Contact Profile. This is not always the case. In my Sitecore Virtual Developer Day 2018 session, I demonstrated how to create a custom submit action that can save the email address to the contact profile, for use in a follow-up action, such as sending an email campaign.
Method 3: Through EXM Client API
The Client API is enabled via Dependency Injection by simply adding IClientApiService
as a parameter of your controller doing the work. This API has been described in length in my blog post Managing the Unsubscribe: Part 5 as well as in my EXM Developer Guide.
namespace Sitecore.EmailCampaign.Cd.Services
{
public interface IClientApiService
{
void SendAutomatedMessage(AutomatedMessage automatedMessage);
void ConfirmSubscription(ConfirmSubscriptionMessage confirmSubscriptionMessage);
void RegisterEmailOpen(EmailOpenMessage emailOpenMessage);
void UpdateListSubscription(UpdateListSubscriptionMessage message);
}
}
To Send an Automated Message
Here is the sample code that I provided to accomplish what you are trying to perform.
public IClientApiService ClientApiService { get; }
public ClientSamples(IClientApiService clientApiService)
{
ClientApiService = clientApiService;
}
public void SendAutomatedExample(Guid messageId, ContactIdentifier identifier)
{
var messageOpitons = new AutomatedMessage();
//Required Parameters
messageOpitons.MessageId = messageId;
messageOpitons.ContactIdentifier = identifier;
//Custom Tokens - Optional
var tokens = new Dictionary<string, object> {{"specialvalue", "Something Useful Here"}};
messageOpitons.CustomTokens = tokens;
//Query String Params - Optional
var queryParams = new Dictionary<string, string> { { "utm_campaign", "Some Value" },{"utm_media","Something Useful"} };
messageOpitons.CustomQueryStringParameters = queryParams;
//Language - Optional
messageOpitons.TargetLanguage = "en";
//Send Message
ClientApiService.SendAutomatedMessage(messageOpitons);
}
}