C# Send Email by RazorEngine

4/1/2014·1 min read

Following demostrate how to use RazorEngine tool to send email by using your Html Template.

sample code for sending email
 		var  model = new  PageModel  { Name = "World" , Email = "someone@somewhere.com"  };
 			string  html = "" ;
 			using  (StreamReader  stream = new  StreamReader ("RazorTest.cshtml" ))
 			{
 				html = stream.ReadToEnd();
 			}
 			string  result3 = Razor .Parse(html, model);
 
 			string  emailReceiver = "" ;
 
 			SmtpClient  mailClient = new  SmtpClient ("localhost" );
 			mailClient.UseDefaultCredentials = true ;
 			mailClient.DeliveryMethod = SmtpDeliveryMethod .PickupDirectoryFromIis;
 
 			MailMessage  message = new  MailMessage ();
 			MailAddress  address = new  MailAddress (emailReceiver);
 			message.From = address;
 			message.To.Add(address);
 			message.Subject = "test email" ;
 			message.SubjectEncoding = Encoding .Unicode;
 			message.BodyEncoding = Encoding .Unicode;
 			StringBuilder  sb = new  StringBuilder ();
 			message.Body = result3;
 			message.IsBodyHtml = true ;
 			message.Priority = MailPriority .Normal;
 			mailClient.Send(message);