Monday, 25 July 2016

MVC - Create email template with cshtml file.

 Step 1: Install RazorEngine nuget package.


 After successfuly install this nuget package you can see the RazorEngine dll in your Refrences:




Step 2: Create new cshtml file under Views > Shared > UserEmailTemplate.chtml file.

@model Project.Web.Models.UserModel
@using TranzgridDataManager.Core.Common;
@{
    Layout = null;
}
<table style="background-color:skyblue" cellspacing="10">
    <thead>
        <tr>
            <th style="border-bottom:1px solid white;text-align:left; padding-left:10px;" colspan="3"><h2> User info</h2></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td style="padding-left:10px;">First&nbsp;Name </td>
            <td>:</td>
            <td>@Model.FirstName</td>
        </tr>
        <tr>
            <td style="padding-left:10px;">Last&nbsp;Name </td>
            <td>:</td>
            <td>@Model.LastName</td>
        </tr>
        <tr>
            <td style="padding-left:10px;">Email</td>
            <td>:</td>
            <td>@Model.Email</td>
        </tr>
        <tr>
            <td style="padding-left:10px;">Role</td>
            <td>:</td>
            <td>@Model.RoleName
            </td>
        </tr>
    </tbody>
</table>

Step 3: Now write the following code to get the html of created html file:-
var templateFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Views//Shared");
var templateService = new TemplateService();
var emailHtmlBody = templateService.Parse(File.ReadAllText(templateFolderPath + "//RegisterUserEmailTemplate.cshtml"), model, null, null);

No comments:

Post a Comment