Send email from email template with parameters
Can anyone share example how to send email from email template with parameters. Like example SendMailFromTemplate("MyTemplate", param1, param2 );
Hi,
To send an e-mail you will need an instance of a Sender. The sender has SendMessage method that takes MailMessage as a parameter. MailMessage is created with Composer.ComposeMessage. Here you can put parameters. Here is a sample:
var subscriber = new Subscriber() FirstName = "First Name", LastName = "Last name", Email = "sample@email.com" ;var templateBody = NewslettersManager.GetManager().GetMessageBody(templateId);var parameters = new object[] subscriber ;var message = Composer.ComposeMessage("reply@email.com", subscriber, "mail subject", templateBody, parameters);var sender = new Sender();sender.SendMessage(message);Hi Boyko,
I tried to implement your snippet, but it seems it can't get the smtp profile to use for this. Through SMTP host was not found. How do you point to the correct SMTP profile with your code?
I used this:
var abandonRecord = App.WorkWith().DynamicContent(abandonedRecordsType).Get().Where(p => p.Id == AbdonEmailID).First(); //Execute the email Task Subscriber subscriber = new Subscriber() FirstName = abandonRecord.GetValue<Lstring>("FirstName").ToString(), LastName = abandonRecord.GetValue<Lstring>("LastName").ToString(), Email = abandonRecord.GetValue<Lstring>("EmailAddress").ToString() ; //Fetch the correct template (here you have a dummy Template) Check DB for sf_message_body guid; MessageBody templateBody = NewslettersManager.GetManager().GetMessageBody(Guid.Parse("9B8D6757-40FB-6773-AE46-FF000078E617")); RawMessageSource templateSource = new RawMessageSource(templateBody, NewslettersManager.GetRootUrl(), false); object[] parameters = new object[] subscriber ; MailMessage message = Composer.ComposeMessage("no-reply@domain.com", subscriber, "Want to join?", templateSource, parameters); var sender = new Telerik.Sitefinity.Modules.Newsletters.Communication.Sender(); sender.SendMessage(message);Hi Ivan,
Checking your code snippet, you can manipulate the SMTP profile through the NewslettersConfig which is used for getting the SMTP settings in this case.
Regards,
Boyko Nistorov
Telerik