How to write a Customized HTML Email in C# .NET dll (dynamic link library) using razor engine

Tech Machinist
Aug 23, 2017 · 1 min read

writing an HTML Email is dll project is different from writing it in MVC project.

We need to write HTML Email code using razor engine.

Razor engine is different from Razor view, which we are familiar in ASP.NET world.

In the executable project or service project where you gonna refer this dll project , write in App.config under arguments section

<arguments>
<add name="EmailBody" value="&lt;html&gt;
&lt;body&gt;
&lt;p&gt;
@Model.Name is going to
@Model.Country*********
&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;" />
<arguments/>

Note: html regular<> tags don’t work in app.config

write following in your dll project c# class

template = config[“Template”].Value;//key need to be unique , the fallowing code line will create unique template keytemplateKey = subject + from + DateTime.UtcNow.ToString(“yyyy-MM-dd hh.mm.ss.ffffff”);string EmailBody= Engine.Razor.RunCompile(template, templateKey, null, model);// we can pass any object in place of model which will acts as model in App.config file mentioned above

Now we can send this ‘EmailBody’ using your respective email client such as SMTP client

Note: you need to get RazorEngine referred in your dll project. for more refer razor engine official documentation .

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade