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);
}
}