Can I set up HTML/Email Templates with ASP.NET?
There's a ton of answers already here, but I stumbled upon a great article about how to use Razor with email templating. Razor was pushed with ASP.NET MVC 3, but MVC is not required to use Razor. This is pretty slick processing of doing email templates
As the article identifies, "The best thing of Razor is that unlike its predecessor(webforms) it is not tied with the web environment, we can easily host it outside the web and use it as template engine for various purpose. "
Generating HTML emails with RazorEngine - Part 01 - Introduction
Leveraging Razor Templates Outside of ASP.NET: They’re Not Just for HTML Anymore!
Smarter email templates in ASP.NET with RazorEngine
Similar Stackoverflow QA
Templating using new RazorEngine API
Using Razor without MVC
Is it possible to use Razor View Engine outside asp.net
Creating Email Template in ASP.NET MVC
The better way to send HTML formatted Email your code will be in "ConfirmAccount.htm"
<!doctype html>
<html lang="tr">
<head>
<meta charset="utf-8">
</head>
<body>
<p>Dear [Firstname] [Surname],</p>
<p>Kindly Confirm your Email by clicking the link below</p>
<p>[Code]</p>
</body>
Read HTML file Using System.IO.File.ReadAllText. get all HTML code in string variable.
string Body = System.IO.File.ReadAllText(HttpContext.Current.Server.MapPath("EmailTemplates/ConfirmAccount.htm"));
Replace a Particular string with your custom value.
Body = Body.Replace("[Firstname]", user.Firstname);
Call SendEmail(string Body) Function and do a procedure to send an email.
Replace the Session email and Configuration app settings with your ones.
public static void SendEmail(string Body)
{
MailMessage message = new MailMessage();
message.From = new MailAddress(Session["Email"].Tostring());
message.To.Add(ConfigurationSettings.AppSettings["RequesEmail"].ToString());
message.Subject = "Request from " + SessionFactory.CurrentCompany.CompanyName + " to add a new supplier";
message.IsBodyHtml = true;
message.Body = Body;
SmtpClient smtpClient = new SmtpClient();
smtpClient.UseDefaultCredentials = true;
smtpClient.Host = ConfigurationSettings.AppSettings["SMTP"].ToString();
smtpClient.Port = Convert.ToInt32(ConfigurationSettings.AppSettings["PORT"].ToString());
smtpClient.EnableSsl = true;
smtpClient.Credentials = new System.Net.NetworkCredential(ConfigurationSettings.AppSettings["USERNAME"].ToString(), ConfigurationSettings.AppSettings["PASSWORD"].ToString());
smtpClient.Send(message);
}
You can check out in message.IsBodyHtml = true
SendEmailAsyn Method
How can i add html email template for email confirmation in asp.net mvc5 identity 2?
We can store the html tag in a variable and then pass that variable to the method UserManager.SendEmailAsync() method.
string message = "<h3>Your Email Tempalte</h3>";
await UserManager.SendEmailAsync(user.Id, "Confirm your account", message);
For more details please check this link: http://forums.asp.net/t/2026812.aspx?How+can+i+add+html+email+template+UI+for+email+confirmation+in+asp+net+mvc5+identity+2+
HTML templates could not be found in Production (NET Core 3.1)
As per asp.net core 3.1
static files are accessible via a path relative to the web root. So any files outside of the wwwroot
cannot be accessed in production. In that case you have to move your EmailTemplate
folder inside the wwwroot
as you can see the picture below:
And then in
Startup.Configure
you have to add reference of this
middlewareapp.UseStaticFiles();
while you would browse the directory you have to modify code like this way
Note: In that case your path would be like
wwwroot/EmailTemplate/EmailTemplate.html
and your code should be like
:private const string templatePath = @"wwwroot/EmailTemplate/{0}.html";
Hope above steps guided you accordingly.
Related Topics
No Console Output When Using Allocconsole and Target Architecture X86
How to Save Image in Database Using C#
Event Bubbling and Mvp: ASP.NET
Repository Pattern Step by Step Explanation
Why Are C# Interface Methods Not Declared Abstract or Virtual
Http 404 Page Not Found in Web API Hosted in Iis 7.5
Copy Rows from One Datatable to Another Datatable
C# How to Wait for a Webpage to Finish Loading Before Continuing
How to Make a Hyperlink Work in a Richtextbox
Passing Arrays by Value and by Reference
Windows 7 and Vista Uac - Programmatically Requesting Elevation in C#
Why Appdomain.Currentdomain.Basedirectory Not Contains "Bin" in ASP.NET App
Why Doesn't Xmlserializer Support Dictionary
Nunit VS. Visual Studio 2008's Test Projects for Unit Testing
Build Error: You Must Add a Reference to System.Runtime
Optimal Way to Read an Excel File (.Xls/.Xlsx)
How to Convert Ienumerable to Observablecollection
Is It Appropriate to Extend Control to Provide Consistently Safe Invoke/Begininvoke Functionality