1

I want to have endpoint Email\Email on my sitecore instance which could send email to specific user, so other applications will be able to use email templates from sitecore.

Previously on 8.2 I used Applciation.Instance.EmailDispatch.SendTriggered(Guid messageId, RecipientId, bool, Dictionary<string, object> tokens). This way provided possibility to send emails by messageId with specific email and person tokens.

Now I am not able to find possibility to send an email to specific email.

This is an example of sending campaign programmatically from sitecore documentation:

var sendingManagerFactory = 
ServiceLocator.ServiceProvider.GetService<ISendingManagerFactory>();
var campaignService = ServiceLocator.ServiceProvider.GetService<IExmCampaignService>();

MessageItem emailCampaign = campaignService.GetMessageItem(emailCampaignId);

ISendingManager sendingManager = sendingManagerFactory.GetAsyncSendingManager(emailCampaign);

sendingManager.SendMessage();

I want to send an email for specific user from request, so I decided to use sendingManager.SendStandartMessage(Sitecore.XConnect.ContactIdentifier contact) instead, but this method depends on Sitecore.Xconnect and Sitecore.Xconnect is not supported by .NetFramework 4.7.1 because it depends on NETStandard.v2.0.

So, how to send an email message to specific email with custom tokens based on message from sitecore?

2
  • Try using .net 4.7.1
    – Marek Musielak
    Commented Mar 20, 2019 at 15:11
  • Sitecore.Xconnect v 9.1.0 depends on .NETStandartd2.0 And I was wrong, I use .NetFramework 4.7.1 for sitecore projects
    – Mickey P
    Commented Mar 20, 2019 at 15:17

1 Answer 1

2

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.

enter image description here

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.

enter image description here

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);
    }
}
4
  • Third approach looks like best option for me, but it uses Sitecore.Xconnect.ContactIdentifier from Sitecore.Xconnect which depends on .NETStandartd.2.0 unfortunately.
    – Mickey P
    Commented Mar 20, 2019 at 16:36
  • I'm not aware of problems with that requirement. Having run many 9.0 and now getting some 9.1's under my belt, I've not run into issues with the .NET Standard 2.0 library. Fairly confident that .NET Core 2.0 (which is the .NET Standard) can live in peaceful harmony with .NET Framework 4.7.x. I say that because.. well.. That's what Sitecore does. Commented Mar 20, 2019 at 16:41
  • This was caused by version of VS and nuget. To work properly with .NET Standard 2.0 in projects with .NET Framework 4.7.x you should install NuGet 3.6 - dist.nuget.org/visualstudio-2015-vsix/v3.6.0/NuGet.Tools.vsix and support package - aka.ms/netstandard-build-support-netfx from github.com/dotnet/standard/issues/514. So I will accept your answer because it has great examples and edit question to provide more detailed problem with mine explanation as well. BTW, my version could start to work now because I had blocker with installation of Xconnect library
    – Mickey P
    Commented Mar 20, 2019 at 17:07
  • Yeah, that makes more sense. NuGet versions are important there. Your way could work, but I would advocate for not using the Sending Manager directly in that manner. Better off to use the Client API as designed. Commented Mar 20, 2019 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.