-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
104 lines (82 loc) · 3.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.IO;
using System.Net;
using System.Net.Mail;
namespace EmailTemplateBuilder
{
class Program
{
public static IConfigurationRoot Configuration { get; set; }
static void Main(string[] args)
{
var sendProductionEmail = false; // Change it to true to use secrets.json and send real emails.
var path = Path.Combine(Environment.CurrentDirectory, @"Templates\dist\sample.html");
var recipients = new[] { "emailtemplatebuildertest@mailinator.com" };
var subject = $"Email Template Builder Test ({DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")})";
var settings = InitializeAndGetSettings();
using (var client = InitializeAndGetSmtpClient(!sendProductionEmail, settings))
{
Console.WriteLine("Building email with the following settings:\n" +
$"--Server: {(!sendProductionEmail ? "127.0.0.1" : settings.Server)}\n" +
$"--Recipient(s): {string.Join(", ", recipients)}\n" +
$"--Subject: {subject}\n" +
$"--Template: {path.Replace(Environment.CurrentDirectory, "")}");
var mailMessage = new MailMessage
{
From = new MailAddress(settings.SenderEmail),
Body = LoadEmailTemplate(path),
Subject = subject,
IsBodyHtml = true
};
foreach (var recipient in recipients)
{
mailMessage.To.Add(recipient);
}
Console.WriteLine("Sending email...");
client.Send(mailMessage);
Console.WriteLine("Email successfully sent.");
}
Console.ReadKey();
}
private static Settings InitializeAndGetSettings()
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
// This shouldn't be added in non-development mode. If it is used in test then further development is
// required.
builder.AddUserSecrets<Settings>();
Configuration = builder.Build();
var services = new ServiceCollection()
.Configure<Settings>(Configuration.GetSection(nameof(Settings)))
.AddOptions()
.BuildServiceProvider();
var settings = services.GetService<IOptions<Settings>>();
return settings.Value;
}
private static SmtpClient InitializeAndGetSmtpClient(bool localhost, Settings settings)
{
var client = new SmtpClient(localhost ? "127.0.0.1" : settings.Server)
{
UseDefaultCredentials = false
};
if (!localhost)
{
client.Credentials = new NetworkCredential(settings.Username, settings.Password);
client.Port = settings.Port;
if (settings.Secure)
{
client.EnableSsl = true;
}
}
return client;
}
private static string LoadEmailTemplate(string path)
{
return File.ReadAllText(path);
}
}
}