-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
64 lines (52 loc) · 2.48 KB
/
Program.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
52
53
54
55
56
57
58
59
60
61
62
63
64
using System.Data.SqlClient;
using System.IO;
using System.Linq;
namespace DDDEastAnglia.EmailSender
{
internal class Program
{
private readonly Configuration configuration;
private readonly EmailSender emailSender;
public Program(Configuration configuration)
{
this.configuration = configuration;
emailSender = new EmailSender(configuration);
}
public void Run(EmailTypes emailTypeToSend)
{
var emailType = EmailTypesFactory.Create()[emailTypeToSend];
string templatePath = Path.Combine(configuration.Templates_Directory, emailType.EmailTemplateFilename);
string templateHtml = File.ReadAllText(templatePath);
using (var connection = new SqlConnection(configuration.Database_ConnectionString))
{
connection.Open();
using (var command = connection.CreateCommand())
{
command.CommandText = emailType.Query;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
string speakerName = reader.GetString(reader.GetOrdinal("Name"));
string speakerEmailAddress = reader.GetString(reader.GetOrdinal("EmailAddress"));
string sessionTitle = reader.GetString(reader.GetOrdinal("Title"));
string emailContents = PrepeareEmail(emailTypeToSend, templateHtml, speakerName, sessionTitle);
emailSender.SendEmail(speakerEmailAddress, emailContents);
}
}
}
}
}
private string PrepeareEmail(EmailTypes emailTypeToSend, string templateHtml, string speakerName, string sessionTitle)
{
string speakerFirstName = speakerName.Split(' ').First();
string html = templateHtml.Replace(configuration.Email_SpeakerNamePlaceholder, speakerFirstName)
.Replace(configuration.Email_SessionTitlePlaceholder, sessionTitle);
var directory = Path.Combine(configuration.SentEmails_OutputDirectory, emailTypeToSend.ToString());
Directory.CreateDirectory(directory);
string path = Path.Combine(directory, speakerName) + ".html";
File.WriteAllText(path, html);
return html;
}
}
}