Sitecore EXM Send Multi-Language Automated Email Campaign Programmatically

To send automated email campaigns programmatically, we need to use Message Bus.

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

To send multi-language email in Regular email campaign, as long as contact has been setup language in profile and the email campaign has also been enabled the multi-language, the email should be sent as contact’s language profile.

But during the dispatch of automated messages programmatically, I found the above approach not working to dynamic created contact with language facet preference. So here is alternative way to select the language of the message by setting TargetLanguage attribute.

Here is the sample code:

 public void SendAutomatedEmail(string messageId, string emailAddress, string prefLang)
{
	using (XConnectClient client = Sitecore.XConnect.Client.Configuration.SitecoreXConnectClientConfiguration.GetClient())
	{
		var identifier = new ContactIdentifier("test", emailAddress, ContactIdentifierType.Known);
		var newContact = new Contact(identifier);
					
		IMessageBus<AutomatedMessagesBus> automatedMessageBus = ServiceLocator.ServiceProvider.GetService<IMessageBus<AutomatedMessagesBus>>();
		var messageInfo = new AutomatedMessage
		{
			MessageId = Guid.Parse(messageId),
			ContactIdentifier = newContact.Identifiers.First(),
			TargetLanguage=prefLang,
		};
						
		automatedMessageBus.Send(messageInfo);	
	}
}

This entry was posted in Sitecore and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s