Sending email campaigns programmatically

Abstract

Send regular and automated email campaigns programmatically from the Content Delivery (CD) role using the Email Experience Manager API.

The Email Experience Manager API (IClientApiService) can only be used on the Content Delivery (CD) role. To send regular and automated email campaigns programmatically from the Content Management (CM) role, follow the examples below.

To send automated email campaigns, use the message bus, which is also used on the CD role.

IMessageBus<AutomatedMessagesBus> automatedMessageBus = 
ServiceLocator.ServiceProvider.GetService<IMessageBus<AutomatedMessagesBus>>();

AutomatedMessage automatedMessage = new AutomatedMessage()
{
    ContactIdentifier = contactIdentifier,
    MessageId = emailCampaignId
};
automatedMessageBus.Send(automatedMessage);

To send regular email campaigns, use the Sending Manager.

Note

The ISendingManagerFactory is provided in the the Sitecore.EmailCampaign.Cm package, under the  Sitecore.EmailCampaign.Cm.Factories+ namespace.

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

MessageItem emailCampaign = campaignService.GetMessageItem(emailCampaignId);

ISendingManager sendingManager =
sendingManagerFactory.GetAsyncSendingManager(emailCampaign);

sendingManager.SendMessage();