-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBodyCreator.cs
51 lines (43 loc) · 1.55 KB
/
BodyCreator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.IO;
using System.Reflection;
using System.Text;
namespace Samples.Logic.Emails.Tools
{
internal class BodyCreator
{
private readonly EmailConfiguration _emailConfiguration;
public BodyCreator(EmailConfiguration emailConfiguration)
{
_emailConfiguration = emailConfiguration;
}
public string Execute(EmailMessage emailMessage)
{
var body = new StringBuilder(GetTemplate(emailMessage.TemplateName));
if (emailMessage.Values != null)
{
foreach (var kvp in emailMessage.Values)
{
var value = (kvp.Value == null) ? string.Empty : kvp.Value.ToString();
body.Replace("{" + kvp.Key + "}", value);
}
}
return body.ToString();
}
private string GetTemplate(string templateName)
{
var resourceName = string.Format("Strateloc.Logic.Emails.Templates.{0}.html", templateName);
var assembly = Assembly.GetExecutingAssembly();
using (var stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null)
{
throw new InfrabelException("Missing template: {0}", resourceName);
}
using (var streamReader = new StreamReader(stream))
{
return streamReader.ReadToEnd();
}
}
}
}
}