I try to send an e-mail using a template in Kentico. The mail is sent correctly but the macros in the template are not resolved. resolver.ResolveMacros(template.TemplateText) returns "".
This is the method:
private void SendEmailUsingTemplate(string recipientEmail, string[,] replacements, string eventName)
{
// Set resolver
ContextResolver resolver = CMSContext.CurrentResolver;
resolver.SourceParameters = replacements;
// Get the email template
var template = EmailTemplateProvider.GetEmailTemplate("MembershipTest-RegistrationConfirmation", _applicationSettings.KenticoSiteName);
if (template != null)
{
// Email message
var emailMessage = new EmailMessage
{
EmailFormat = EmailFormatEnum.Default,
Recipients = recipientEmail,
From = EmailHelper.GetSender(template, SettingsKeyProvider.GetStringValue(CMSContext.CurrentSiteName + ".CMSNoreplyEmailAddress")),
CcRecipients = template.TemplateCc,
BccRecipients = template.TemplateBcc,
Subject = resolver.ResolveMacros(template.TemplateSubject),
PlainTextBody = resolver.ResolveMacros(template.TemplatePlainText)
};
// Enable macro encoding for body
resolver.EncodeResolvedValues = true;
emailMessage.Body = resolver.ResolveMacros(template.TemplateText);
// Disable macro encoding for plaintext body and subject
resolver.EncodeResolvedValues = false;
try
{
MetaFileInfoProvider.ResolveMetaFileImages(emailMessage, template.TemplateID, EmailObjectType.EMAILTEMPLATE, MetaFileInfoProvider.OBJECT_CATEGORY_TEMPLATE);
// Send the e-mail immediately
EmailSender.SendEmail(CMSContext.CurrentSiteName, emailMessage, true);
}
catch (Exception ex)
{
var eventLogProvider = new EventLogProvider();
eventLogProvider.LogEvent("E", eventName, ex);
throw;
}
}
}
And this is the method call:
string[,] replacements = new string[1, 2];
replacements[0, 0] = "username";
replacements[0, 1] = username;
SendEmailUsingTemplate("eapeldoorn@snelstart.nl", replacements, "Test - Sending Emails");
Mailtemplate only contains the following:
{%username%}
Does someone have any idea?