3. Create variables to use into the template
First you need to create a partial class for the T4 template that you created in the step 2.
In your partial add a property for each value that you want to inject into the template (destination email or signature for example).
Create a constructor to initialize the properties that you have created.
using System.Net.Mail;
namespace MailT4Template
{
public partial class WelcomeMail
{
public MailAddress To { get; set; }
public WelcomeMail(MailAddress to)
{
this.To = to;
}
}
}
4. Adapt the template to use the injected values
Just open a C# block code with the tags <# and #> and use the equal operator to get the variable value.
<strong>Hello <#= To.DisplayName #>!</strong>
5. Transform the text template
Invoke the template with the parameters to get the transformed text.
var to = new MailAddress("luke@starwars.com", "Luke");
WelcomeMail mailTemplate = new WelcomeMail(to);
mailTemplate.TransformText();
6. Compose your email
Use the transformed text and send the email.
var from = new MailAddress("mail@mail.com");
var to = new MailAddress("luke@starwars.com", "Luke");
WelcomeMail mailTemplate = new WelcomeMail(to);
var mail = new MailMessage(from, to);
var client = new SmtpClient();
client.Port = 587;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.EnableSsl = true;
client.Credentials = new NetworkCredential("admin@mail.com","UseTheForce#");
client.Host = "mail.mail.com";
mail.Subject = "This is a welcom email.";
mail.IsBodyHtml = true;
mail.Body = mailTemplate.TransformText();
client.Send(mail);
I hope that this helps you.