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>