1

I have C# application that must store some information into MS SQL that would be later sent to email with DB Mail.

Within C# application I have a class with several properties and I need to use it to generate email text. So what I would like is set up a template with placeholders for variables. I need to create text as HTML and plain text.

  1. What tools, libraries would you recommend for HTML?

  2. Is String.Format() best alternative to work with plain text?

2 Answers 2

4

I do this in other applications by having the e-mail body available somewhere (SharePoint list, data table) already in the right format, but with named placeholders, corresponding to the information you have in your application. Then sending the e-mail means replacing the placeholder with the right information. StringBuilder.Replace works fine.

Sign up to request clarification or add additional context in comments.

2 Comments

What is the benefit of storing "Format String" in data table instead of directly in C# program? If you modify placeholders you'll have to modify C# as well. Am I wrong?
Say your data includes 'first name', 'last name', etc. In an external storage, you have an e-mail template with "hello %FirstName%, how is life today?". At run-time, you replace %FirstName% by the 'first name' attribute and you send the resulting text. The user should have a way to modify the templates without asking you to modify the app. Of course, adding a new placeholder (e.g. 'middle name') requires a change of the application.
2

I would say the most important thing you need to decide is when to encode the text. If you are emailing text supplied byusers, you will want to HtmlEncode it before including it in an email. It's probably ok to store it "as recieved" in the data base as long as every consumer encodes it before using it. I typically do this in the data layer that "gets" data from the data base.

1 Comment

Yeah HtmlEncode is the way to go - I use it at the view layer so I can manipulate the text in its raw format in lower layers (eg. Business, Service layers)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.