Generating HTML email body in C#
You can use the MailDefinition class.
This is how you use it:
MailDefinition md = new MailDefinition();
md.From = "test@domain.example";
md.IsBodyHtml = true;
md.Subject = "Test of MailDefinition";
ListDictionary replacements = new ListDictionary();
replacements.Add("{name}", "Martin");
replacements.Add("{country}", "Denmark");
string body = "<div>Hello {name} You're from {country}.</div>";
MailMessage msg = md.CreateMailMessage("you@anywhere.example", replacements, body, new System.Web.UI.Control());
Also, I've written a blog post on how to generate HTML e-mail body in C# using templates using the MailDefinition class.
Generate HTML Email with .NET
Because there's no designation of IsBodyHTML
set to true when using the Send function the way you've done. I think IsBodyHTML
is within the MailMessage
object.
I'm not entirely sure what IsBodyHTML
sets in the body but it may create fully formed HTML (<html><head><body>etc....
) so maybe give that a try.
How to send HTML-formatted email?
Setting isBodyHtml
to true
allows you to use HTML tags in the message body:
msg = new MailMessage("xxxx@gmail.com",
"yyyy@gmail.com", "Message from PSSP System",
"This email sent by the PSSP system<br />" +
"<b>this is bold text!</b>");
msg.IsBodyHtml = true;
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
Related Topics
Passing Strings from C# to C++ Dll and Back - Minimal Example
How to Properly Clean Up Excel Interop Objects
Fastest Way of Inserting in Entity Framework
How to Get a Consistent Byte Representation of Strings in C# Without Manually Specifying an Encoding
Returning Ienumerable≪T≫ Vs. Iqueryable≪T≫
How to Escape a Double Quote in a Verbatim String Literal
Difference Between Task and Thread
How to Enable External Request in Iis Express
Easiest Way to Split a String on Newlines in .Net
Using a Class Defined in a C++ Dll in C# Code
What Are Good Ways to Prevent SQL Injection
Create Generic Method Constraining T to an Enum
Creating a Byte Array from a Stream
Evaluating String "3*(4+2)" Yield Int 18