In this article, we will discuss how to send emails using a Template file in ASP.NET Core Application. Sending emails using a Template file is similar to sending emails using plain text. The only difference is that now, the source of the content is a text file rather than direct content for email.
If you don’t know how to send email in ASP.NET Core applications, please refer to my previous article Configure Email Service In ASP.NET Core Using MailKit. Let's assume, we have already configured the email service in the ASP.NET Core application.
Milestones
- Create a Simple Email Template.
- Configure Email Setting in ASP.NET Core Application.
- Read content from Email Template using StreamReader()
- Personalize Email Content
- Send Email to User
Creating a simple email template
We will keep the template file separate so that we can change the file design and content anytime. Let's keep these template files under wwwroot => Templates => EmailTemplate.
Step 1. For this, create a new folder named Templates under the wwwroot folder.
Step 2. Create a new folder named Email Template under the Templates folder.
Step 3. Add a new Item in the Email Template (right-click on Email Template folder >> add New Item).
Step 4. Select ASP.NET Core and then HTML Page.
Step 5. Set a name for the HTML file. In my case, I have named it Welcome_EmailTemplate.html (But I will be using Confirm_Account_Registration.htm for demo purposes whose design is the same with a different file name).

Once you have created Email Templates, the Solution Explorer looks like below. (In my case, there are 5 Email Templates).

Now, you can write your own design codes using inline CSS and HTML to create Email. In my case, the template looks like this.

The code for the above sample Email Template is given below.
Code Snippet
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome Email from TCP</title>
</head>
<body>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" valign="top" bgcolor="#ffe77b" style="background-color:#ffe77b;">
<br>
<br>
<table width="600" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="70" align="left" valign="middle"></td>
</tr>
<tr>
<td align="left" valign="top"><img src="http://localhost:2131/Templates/EmailTemplate/images/top.png" width="600" height="13" style="display:block;"></td>
</tr>
<tr>
<td align="left" valign="top" bgcolor="#564319" style="background-color:#564319; font-family:Arial, Helvetica, sans-serif; padding:10px;">
<div style="font-size:36px; color:#ffffff;">
<b>{0}</b>
</div>
<div style="font-size:13px; color:#a29881;">
<b>{1} : ASP.NET Core Demp App</b>
</div>
</td>
</tr>
<tr>
<td align="left" valign="top" bgcolor="#ffffff" style="background-color:#ffffff;">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" valign="middle" style="padding:10px; color:#564319; font-size:28px; font-family:Georgia, 'Times New Roman', Times, serif;">
Congratulations! <small>You are registered.</small>
</td>
</tr>
</table>
<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="40%" align="center" valign="middle" style="padding:10px;">
<img src="http://localhost:2131/Templates/EmailTemplate/images/Weak_Password.gif" width="169" height="187" style="display:block">
</td>
<td align="left" valign="middle" style="color:#525252; font-family:Arial, Helvetica, sans-serif; padding:10px;">
<div style="font-size:16px;">
Dear {2},
</div>
<div style="font-size:12px;">
Thank you for showing your interest in our website.
All you need to do is click the button below (it only takes a few seconds).
You won’t be asked to log in to your account – we're simply verifying ownership of this email address.
<hr>
<center>
<button type="button" title="Confirm Account Registration" style="background: #1b97f1">
<a href="{6}" style="font-size:22px; padding: 10px; color: #ffffff">
Confirm Email Now
</a>
</button>
</center>
</div>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" valign="middle" style="padding:5px;">
<img src="http://localhost:2131/Templates/EmailTemplate/images/divider.gif" width="566" height="30">
</td>
</tr>
</table>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0" style="margin-bottom:15px;">
<tr>
<td align="left" valign="middle" style="padding:15px; font-family:Arial, Helvetica, sans-serif;">
<div style="font-size:20px; color:#564319;">
<b>Please keep your credentials confidential for future use.</b>
</div>
<div style="font-size:16px; color:#525252;">
<b>Email :</b> {2}
<br />
<b>Username :</b> {3}
<br />
<b>Password :</b> {4}
</div>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" style="margin-bottom:10px;">
<tr>
<td align="left" valign="middle" style="padding:15px; background-color:#564319; font-family:Arial, Helvetica, sans-serif;">
<div style="font-size:20px; color:#fff;">
<b>Update your password now.</b>
</div>
<div style="font-size:13px; color:#ffe77b;">
Weak passwords get stolen and lead to hacked accounts. Celebrate World Password Day with a new, strong password.
<br>
<br>
<a href="#" style="color:#ffe77b; text-decoration:underline;">CLICK HERE</a> TO CHANGE PASSWORD
</div>
</td>
</tr>
</table>
<table width="95%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="50%" align="left" valign="middle" style="padding:10px;">
<table width="75%" border="0" cellspacing="0" cellpadding="4">
<tr>
<td align="left" valign="top" style="font-family:Verdana, Geneva, sans-serif; font-size:14px; color:#000000;">
<b>Follow Us On</b>
</td>
</tr>
<tr>
<td align="left" valign="top" style="font-family:Verdana, Geneva, sans-serif; font-size:12px; color:#000000;">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="33%" align="left" valign="middle">
<a href="https://twitter.com" title="Facebook">
<img src="http://localhost:2131/Templates/EmailTemplate/images/tweet48.png" width="48" height="48">
</a>
</td>
<td width="34%" align="left" valign="middle">
<a href="https://linkedin.com" title="Linkedin">
<img src="http://localhost:2131/Templates/EmailTemplate/images/in48.png" width="48" height="48">
</a>
</td>
<td width="33%" align="left" valign="middle">
<a href="https://facebook.com" title="Facebook">
<img src="http://localhost:2131/Templates/EmailTemplate/images/face48.png" width="48" height="48">
</a>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td width="50%" align="left" valign="middle" style="color:#564319; font-size:11px; font-family:Arial, Helvetica, sans-serif; padding:10px;">
<b>Hours:</b> Mon-Fri 9:30-5:30, Sat. 9:30-3:00, Sun. Closed <br>
<b>Customer Support:</b> <a href="mailto:[email protected]" style="color:#564319; text-decoration:none;">[email protected]</a><br>
<br>
<b>Company Address</b><br>
Company URL: <a href="http://www.yourcompanyname.com" target="_blank" style="color:#564319; text-decoration:none;">http://www.yourcompanyname.com</a>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="left" valign="top"><img src="http://localhost:2131/Templates/EmailTemplate/images/bot.png" width="600" height="37" style="display:block;"></td>
</tr>
</table>
<br>
<br>
</td>
</tr>
</table>
</body>
</html>



Sanjay ShekhawatPosted Jun 25, 2021, 1:14 PM
Thanks for sharning
Ethan SebastianPosted Feb 6, 2021, 11:28 AM
Why the images are not getting downloaded by default?
Pritesh MaturkarPosted Oct 3, 2019, 9:05 PM
it is very nice template. thanks
Benoit RobinPosted Apr 12, 2019, 4:42 PM
This is nice ... but why dont you simply use a view with a model then use RenderPartialView ?
JohnPosted Nov 27, 2018, 5:23 PM
How would the html template handle more complex objects than replacing strings? For example , a List of items in an email?
Komal SharmaPosted Jul 26, 2018, 8:54 AM
Thank you for the article. But i am getting Input string was not in a correct format,error .Please help me in this
Saheb JanaPosted May 15, 2018, 2:46 AM
Sir in the mail template images are not showing please help me
Nasrin BagheriPosted Dec 20, 2017, 8:41 AM
Thank you for your article,nice
Adha DimasPosted Nov 22, 2017, 1:33 PM
Thank you for your article nice post
Rijwan AnsariPosted Jul 21, 2017, 3:30 PM
Nice Post about Email Template
Thiruppathi RPosted May 31, 2017, 9:48 PM
Nice Article........