API Questions on Kentico API.
Version 5.x > API > how to use the E-mail templates in C#? View modes: 
User avatar
Member
Member
wtzeng-micro-datanet - 8/18/2011 4:01:05 PM
   
how to use the E-mail templates in C#?
Hi
I am using custom event handler do some data process and at the end of precess is to send out email. Now I use membership registration just for testing but I have hard time to replace the Marco in the template. I have the code below, could you tell me what went wrong?

thanks

Wei

EmailTemplateInfo temp = EmailTemplateProvider.GetEmailTemplate("Membership.Registration", CMSContext.CurrentSiteName);

EmailMessage msg = new EmailMessage();
string[,] replacestring = new string[2, 2];
replacestring[0, 0] = "username";
replacestring[0,1] = Profiles[x].username;
replacestring[1, 0] = "password";
replacestring[1, 1] = Profiles[x].password;
EmailSender.SendEmailWithTemplateText(CMSContext.CurrentSiteName, msg, temp,replacestring);

User avatar
Member
Member
kentico_michal - 8/19/2011 2:44:29 AM
   
RE:how to use the E-mail templates in C#?
Hello,

You need to assign the replacestring array to SourceParameters of a MacroResolver class and use it as a input parameter of the SendEmailWithTemplateText method.

Please take a look at following code snippet:


EmailMessage msg = new CMS.EmailEngine.EmailMessage();
EmailTemplateInfo eti = EmailTemplateProvider.GetEmailTemplate("myTemplate", CMSContext.CurrentSiteID);

MacroResolver resolver = new MacroResolver();

string[,] replacestring = new string[2, 2];
replacestring[0, 0] = "username";
replacestring[0, 1] = Profiles[x].username;
replacestring[1, 0] = "password";
replacestring[1, 1] = Profiles[x].password;

resolver.SourceParameters = replacements;

msg.EmailFormat = EmailFormatEnum.Both;
msg.From = eti.TemplateFrom;
msg.Recipients = "name@domain.com";
msg.Subject = eti.TemplateSubject;
msg.Body = eti.TemplateText;

EmailSender.SendEmailWithTemplateText(CMSContext.CurrentSiteName, msg, eti, resolver, true );



I hope this will help.

Best regards,
Michal Legen