.NET C# Email: using Email Template

13,010

Solution 1

If your needs are more complex than can be achieved with @Anuraj's suggestion then I'd suggest looking at XSLT - you package your data as a lump of XML and transform the XML into whatever (HTML in this case) using an XSLT template.

Support in .NET for this kind of transformation is excellent and once you have got over the initial challenges (XSLT is different) you will have added a very capable set of tools to your toolkit.

Solution 2

string emailTemplate = @"
Hi, ##USERNAME##
bla bla bla dear ##USERNAME## bla bla bla!

Best regards, 
##MYNAME##";

string email = emailTemplate
    .Replace("##USERNAME##", userName)
    .Replace("##MYNAME##", myName);

Solution 3

Place place holders in the HTML content with {0},{1} etc and use String.format() to replace it.

Solution 4

DotLiquid is another option. You specify values from a class model as {{ user.name }} and then at runtime you provide the data in that class, and the template with the markup, and it will merge the values in for you. The nice thing is these are "safe" so that user's who create the templates can't crash your system or write unsafe code: http://dotliquidmarkup.org/try-online

Share:
13,010
Posto
Author by

Posto

I am not an extremely confidant person, I cant stick a conversation with just any one, But when it comes to my profession, a degree of self Confidence that I have.

Updated on June 05, 2022

Comments

  • Posto
    Posto almost 2 years

    I want to send a HTML email using HTML template. I would like to just replace some value's from that HTML template. Any idea how to achieve this?