Posto
Posto

Reputation: 7550

.NET C# Email: using Email Template

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?

Upvotes: 2

Views: 13448

Answers (4)

AaronLS
AaronLS

Reputation: 38392

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

Upvotes: 1

user215303
user215303

Reputation: 157

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

Best regards, 
##MYNAME##";

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

Upvotes: 3

Murph
Murph

Reputation: 10190

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.

Upvotes: 5

Anuraj
Anuraj

Reputation: 19618

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

Upvotes: 2

Related Questions