Question

My Kentico server is unable to send e-mails, so I have to transform my e-mail using MacroResolver, but send it using some other way.

var clients = new List<Client>();

var macroResolver = MacroResolver.GetInstance();
macroResolver.AddDynamicParameter("clients", clients);

var emailMessage = new EmailMessage { 
    From = "someone@somewhere.com", 
    Recipients = "otherone@somewhere.com", 
    Subject = "Whatever" 
};

var template = EmailTemplateProvider.GetEmailTemplate(templateName, siteName);

EmailSender.SendEmailWithTemplateText(siteName, emailMessage, template, macroResolver, true);

In other words, I would like to use Kentico just as a Template Engine. Is there anyway to achieve this?

Was it helpful?

Solution

What SendEmailWithTemplateText method basically does is it fills empty fields of message by its equivalent from a template and resolve macro values in it. If you are only after message body, then you can create the email message by:

emailMessage.Body = macroResolver.ResolveMacros(emailMessage.Body);
emailMessage.PlainTextBody = macroResolver.ResolveMacros(emailMessage.PlainTextBody);

For most scenarios it's also better to tell the resolver to encode resolved values. You can do it by: resolver.EncodeResolvedValues = true;

Also you are passing whole 'clients' collection to the resolver. You'll probably need to take it one by one and generate emails in a loop.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top