{"id":520,"date":"2010-02-19T17:47:38","date_gmt":"2010-02-19T15:47:38","guid":{"rendered":"http:\/\/www.limilabs.com\/blog\/?p=520"},"modified":"2019-03-15T16:51:20","modified_gmt":"2019-03-15T14:51:20","slug":"email-template-engine","status":"publish","type":"post","link":"https:\/\/www.limilabs.com\/blog\/email-template-engine","title":{"rendered":"Email template engine"},"content":{"rendered":"<p>Newest version of Mail.dll <a href=\"\/mail\">email library for .NET<\/a> includes easy to use template engine.<\/p>\n<p>It allows you to easily create html template for your emails:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/blog\/wp-content\/uploads\/2010\/02\/TemplateSample.png\" alt=\"\" title=\"TemplateSample\" width=\"459\" height=\"420\" class=\"aligncenter size-full\" \/><\/p>\n<p>Loading and rendering such template requires one line of code:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# version\r\n\r\nContact templateData = ...;\r\n\r\n string html = Template\r\n     .FromFile(&quot;template.txt&quot;)\r\n     .DataFrom(templateData )\r\n     .Render();\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET version\r\n\r\nDim templateData As Contact = ....\r\n\r\nDim html As String = Template _\r\n     .FromFile(&quot;template.txt&quot;) _\r\n     .DataFrom(templateData) _\r\n     .Render()\r\n<\/pre>\n<p>This is how the template looks like:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;Your order&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\tHi &#x5B;FirstName] &#x5B;LastName],\r\n\t&lt;br \/&gt;\r\n\t&lt;p&gt;\r\n\tYour account &#x5B;if Verified]has&#x5B;else]&lt;strong&gt;has not&lt;\/strong&gt;&#x5B;end] been verified. &lt;br\/&gt;\r\n\tYour password is: &#x5B;Password].\r\n\t&lt;\/p&gt;\r\n\t&lt;p&gt;\r\n\tHere are your orders:\r\n\t&lt;\/p&gt;\r\n\t&#x5B;foreach Orders]\r\n\t\t&lt;p&gt;\r\n\t\tOrder sent to &lt;strong&gt;&#x5B;Street]&lt;\/strong&gt;:\r\n\t\t&lt;\/p&gt;\r\n\t\t&lt;table style=&quot;width: 30%;&quot;&gt;\r\n\t\t\t&#x5B;foreach Items]\r\n\t\t\t\t&lt;tr style=&quot;background-color: #E0ECFF;&quot;&gt;\r\n\t\t\t\t\t&lt;td&gt;&#x5B;Name]&lt;\/td&gt;&lt;td&gt;&#x5B;Price]&lt;\/td&gt;\r\n\t\t\t\t&lt;\/tr&gt;\r\n\t\t\t&#x5B;end]\r\n\t\t&lt;\/table&gt;\r\n\t&#x5B;end]\r\n\t&lt;p&gt;\r\n\t\tThank you for your orders.\r\n\t&lt;p&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>Here&#8217;s the sample code that loads, fills the template and sends it using Mail.dll:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# version\r\n\r\nstring rendered = Template\r\n    .FromFile(&quot;template.txt&quot;)\r\n    .DataFrom(templateData)\r\n    .Render();\r\n\r\nMailBuilder builder = new MailBuilder();\r\nbuilder.Html = rendered;\r\nbuilder.Subject = &quot;Your order&quot;;\r\nbuilder.From.Add(new MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;));\r\nbuilder.To.Add(new MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;));\r\nIMail email = builder.Create();\r\n\r\nusing (Smtp client = new Smtp())\r\n{\r\n    client.ConnectSSL(&quot;smtp.example.org&quot;);\r\n    client.UseBestLogin(&quot;alice@example.org&quot;, &quot;password&quot;);\r\n    client.SendMessage(email);\r\n    client.Close();\r\n}\r\n<\/pre>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# fluent interface version\r\n\r\nusing Fluent = Limilabs.Mail.Fluent;\r\n\r\nFluent.Mail.Html(Template\r\n              .FromFile(&quot;template.txt&quot;)\r\n              .DataFrom(templateData)\r\n              .Render())\r\n    .From(new MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;))\r\n    .To(new MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;))\r\n    .Subject(&quot;Your order&quot;)\r\n    .UsingNewSmtp()\r\n    .WithCredentials(&quot;alice@example.org&quot;, &quot;password&quot;)\r\n    .Server(&quot;smtp.example.org&quot;)\r\n    .WithSSL()\r\n    .Send();\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET version\r\n\r\n    Dim rendered As String = Template _\r\n        .FromFile(&quot;template.txt&quot;) _\r\n        .DataFrom(templateData) _\r\n        .Render()\r\n\r\n    Dim builder As MailBuilder = New MailBuilder()\r\n    builder.Html = rendered\r\n    builder.Subject = &quot;Your order&quot;\r\n    builder.From.Add(New MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;))\r\n    builder.&#x5B;To].Add(New MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;))\r\n    Dim email As IMail = builder.Create()\r\n\r\n    Using client As Smtp = New Smtp()\r\n        client.ConnectSSL(&quot;smtp.example.org&quot;)\r\n        client.UseBestLogin(&quot;alice@example.org&quot;, &quot;password&quot;)\r\n        client.SendMessage(email)\r\n        client.Close()\r\n    End Using\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET fluent interface version\r\n\r\nImports Fluent = Limilabs.Mail.Fluent;\r\n\r\nFluent.Mail.Html(Template _\r\n               .FromFile(&quot;template.txt&quot;) _\r\n               .DataFrom(templateData) _\r\n               .Render()) _\r\n     .From(New MailBox(&quot;alice@mail.com&quot;, &quot;Alice&quot;)) _\r\n     .&#x5B;To](New MailBox(&quot;bob@mail.com&quot;, &quot;Bob&quot;)) _\r\n     .Subject(&quot;Your order&quot;) _\r\n     .UsingNewSmtp() _\r\n     .WithCredentials(&quot;alice@example.org&quot;, &quot;password&quot;) _\r\n     .Server(&quot;smtp.example.org&quot;) _\r\n     .WithSSL() _\r\n     .Send()\r\n<\/pre>\n<p>You can also specify text version of the message, by using <em>Text(string)<\/em> method. In most cases this is unnecessary, as Mail.dll automatically extracts plain text from html and adds the plain text version to the email message.<\/p>\n<p>And this is how the data (<em>templateData <\/em> variable), used by the template, look like:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ C# version\r\n\r\npublic class Contact\r\n{\r\n    public List&lt;order&gt; Orders { get; private set; }\r\n\r\n    public string FirstName { get; set; }\r\n    public string LastName { get; set; }\r\n    public bool Verified;\r\n    private string Password { get; set; }\r\n\r\n    public Contact()\r\n    {\r\n        Orders = new List&lt;order&gt;();\r\n    }\r\n} ;\r\n<\/pre>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\">\r\n' VB.NET version\r\n\r\nPublic Class Contact\r\n\tPublic Property Orders() As List(Of Order)\r\n\t\tGet\r\n\t\t\tReturn m_Orders\r\n\t\tEnd Get\r\n\t\tPrivate Set\r\n\t\t\tm_Orders = Value\r\n\t\tEnd Set\r\n\tEnd Property\r\n\tPrivate m_Orders As List(Of Order)\r\n\r\n\tPublic Property FirstName() As String\r\n\t\tGet\r\n\t\t\tReturn m_FirstName\r\n\t\tEnd Get\r\n\t\tSet\r\n\t\t\tm_FirstName = Value\r\n\t\tEnd Set\r\n\tEnd Property\r\n\tPrivate m_FirstName As String\r\n\tPublic Property LastName() As String\r\n\t\tGet\r\n\t\t\tReturn m_LastName\r\n\t\tEnd Get\r\n\t\tSet\r\n\t\t\tm_LastName = Value\r\n\t\tEnd Set\r\n\tEnd Property\r\n\tPrivate m_LastName As String\r\n\tPublic Verified As Boolean\r\n\tPrivate Property Password() As String\r\n\t\tGet\r\n\t\t\tReturn m_Password\r\n\t\tEnd Get\r\n\t\tSet\r\n\t\t\tm_Password = Value\r\n\t\tEnd Set\r\n\tEnd Property\r\n\tPrivate m_Password As String\r\n\r\n\tPublic Sub New()\r\n\t\tOrders = New List(Of Order)()\r\n\tEnd Sub\r\nEnd Class\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Newest version of Mail.dll email library for .NET includes easy to use template engine. It allows you to easily create html template for your emails: Loading and rendering such template requires one line of code: \/\/ C# version Contact templateData = &#8230;; string html = Template .FromFile(&quot;template.txt&quot;) .DataFrom(templateData ) .Render(); &#8216; VB.NET version Dim templateData [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[33,50,57],"class_list":["post-520","post","type-post","status-publish","format-standard","hentry","category-mail-dll","tag-email-component","tag-smtp","tag-vb-net"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/520"}],"collection":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/comments?post=520"}],"version-history":[{"count":6,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/520\/revisions"}],"predecessor-version":[{"id":5479,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/posts\/520\/revisions\/5479"}],"wp:attachment":[{"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/media?parent=520"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/categories?post=520"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.limilabs.com\/blog\/wp-json\/wp\/v2\/tags?post=520"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}