C# How to dynamically load email template file dynamically

T.Zacks 3,986 Reputation points
2022-02-21T18:00:49.653+00:00

i have a send mail routine which send mail and mail body taken from html file. when i call that routine then i send html file path with name. routine load html from that file and prepare body text.

now i want to develop a routine where mail html body will be injected dynamically to send mail routine. anyone please discuss how can i do it very nicely using interface. thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,577 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,291 Reputation points
    2022-02-23T21:08:15.547+00:00

    Use Json

    [
      {
        "Name": "Template1",
        "Subject": "Monthly news letter",
        "Body": "<style>.fg-white {color: crimson;}</style><p>Hello ##NAME##</p><p class=\"fg-white\">##BODY##</p>"
      },
      {
        "Name": "Template2",
        "Subject": "Notice of intention",
        "Body": "<p>Hello ##NAME##</p><p class=\"fg-white\">##BODY##</p>"
      }
    ]
    

    Class

    public class EmailTemplate
    {
        public string Name { get; set; }
        public string Subject { get; set; }
        public string Body { get; set; }
    }
    

    Mockup

    public class MockupEmail
    {
        public static string FileName => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates.json");
    
        public static List<EmailTemplate> Read()
        {
            return  JsonConvert.DeserializeObject<List<EmailTemplate>>(File.ReadAllText(FileName));
        }
        public static void Write()
        {
            List<EmailTemplate> list = new List<EmailTemplate>
            {
                new EmailTemplate()
                {
                    Name = "Template1", 
                    Subject = "Monthly news letter", 
                    Body = "<style>.fg-white {color: crimson;}</style><p>Hello ##NAME##</p><p class=\"fg-white\">##BODY##</p>"
                },
                new EmailTemplate()
                {
                    Name = "Template2",
                    Subject = "Notice of intention",
                    Body = "<p>Hello ##NAME##</p><p class=\"fg-white\">##BODY##</p>"
                }
            };
    
            File.WriteAllText(FileName, JsonConvert.SerializeObject(list, Formatting.Indented));
        }
    }
    

    Quick example

    MockupEmail.Write();
    
    var templates = MockupEmail.Read();
    
    var Template2 = templates.FirstOrDefault(x => x.Name == "Template1");
    
    List<string> names = new List<string>() { "Mary","Bob","Jim" };
    
    foreach (var name in names)
    {
        var body = Template2
            .Body
            .Replace("##NAME##", name)
            .Replace("##BODY##", $"Great news {name}");
    
        Console.WriteLine(body);
        Console.WriteLine();
    }
    
    • Create the HTML in an editor or Visual Studio, flatten to a one liner and escape any characters as needed.
    • Polish up the replace aspect if needed.

    Viewed from Visual Studio output window

    <style>.fg-white {color: crimson;}</style><p>Hello Mary</p><p class="fg-white">Great news Mary</p>
    
    <style>.fg-white {color: crimson;}</style><p>Hello Bob</p><p class="fg-white">Great news Bob</p>
    
    <style>.fg-white {color: crimson;}</style><p>Hello Jim</p><p class="fg-white">Great news Jim</p>
    
    1 person found this answer helpful.

  2. David Moschkau 0 Reputation points
    2023-12-01T21:18:58.9433333+00:00

    I like the answer by Karen. In the past (way past), I have used xslt transformations with xml data to build email templates.

    0 comments No comments