Skip to content

Commit 00fdc51

Browse files
committed
Implemented email notifications
1 parent 03960e3 commit 00fdc51

13 files changed

+425
-184
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
root = true
22

3-
[*.{cs,vb}]
3+
[*.*]
44
tab_width = 2
55
indent_size = 2
66
indent_style = space

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# appsettings.json file used only during Development
2+
/appsettings.Development.json
3+
14
# Files created by app during run time:
25
/*.log.txt
36
/*.log.csv

Constants.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
namespace bt_sql_backup_service
1+
namespace BtSqlBackupService
22
{
33
public static class Constants
44
{
55
/// <summary>
6-
/// Windows service name. Needs to be unique per system.
6+
/// Windows service name. Needs to be unique per system.
77
/// </summary>
88
public const string serviceName = "bt_sql_backup_service";
99

1010
/// <summary>
11-
/// Windows service display name.
11+
/// Windows service display name.
1212
/// </summary>
1313
public const string displayName = "SQL Backup Service";
1414

1515
/// <summary>
16-
/// Windows service description field.
16+
/// Windows service description field.
1717
/// </summary>
1818
public const string description = "BrokenThorn's SQL Backup Service";
1919
}

Entities/Commands.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace BtSqlBackupService.Entities
2+
{
3+
/// <summary>
4+
/// A SQL command entity.
5+
/// </summary>
6+
/// <remarks>
7+
/// This entity holds information about a SQL command script, a Cron schedule
8+
/// and SQL Server connection string representing the server on which the
9+
/// command should be executed.
10+
/// </remarks>
11+
public class SqlCommandEntity
12+
{
13+
/// <summary>
14+
/// A short name for this command.
15+
/// </summary>
16+
/// <value></value>
17+
public string Name { get; set; }
18+
19+
/// <summary>
20+
/// A description of what this command is or does.
21+
/// </summary>
22+
public string Description { get; set; }
23+
24+
/// <summary>
25+
/// Connection string for the SQL server that the command will be executed on.
26+
/// </summary>
27+
public string ConnectionString { get; set; }
28+
29+
/// <summary>
30+
/// SQL command to execute.
31+
/// </summary>
32+
public string SqlCommand { get; set; }
33+
34+
/// <summary>
35+
/// The number of seconds to wait before terminating the attempt to execute
36+
/// the SqlCommand and returning an error.
37+
/// </summary>
38+
/// <value>Timeout in seconds</value>
39+
public int CommandTimeout { get; set; }
40+
41+
/// <summary>
42+
/// Schedule in cron syntax style.
43+
/// </summary>
44+
public string Cron { get; set; }
45+
46+
/// <summary>
47+
/// An email address or comma separated list of email addresses to send
48+
/// notifications to.
49+
/// </summary>
50+
/// <value></value>
51+
public string NotifyEmails { get; set; }
52+
}
53+
}

Entities/SmtpClientSettings.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace BtSqlBackupService.Entities
2+
{
3+
/// <summary>
4+
/// Settings for the SMTP mail client.
5+
/// </summary>
6+
public class SmtpClientSettings
7+
{
8+
public string Server { get; set; }
9+
public int Port { get; set; }
10+
public string SenderName { get; set; }
11+
public string SenderEmail { get; set; }
12+
public string Username { get; set; }
13+
public string Password { get; set; }
14+
public string AdminEmail { get; set; }
15+
}
16+
}

Mailer.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Threading.Tasks;
3+
using BtSqlBackupService.Entities;
4+
using MailKit.Net.Smtp;
5+
using Microsoft.Extensions.Logging;
6+
using MimeKit;
7+
8+
namespace BtSqlBackupService
9+
{
10+
/// <summary>
11+
/// A simple electronic mail sender.
12+
/// </summary>
13+
public class Mailer
14+
{
15+
private readonly ILogger _logger;
16+
private readonly SmtpClientSettings _smtpClientSettings;
17+
18+
public Mailer(SmtpClientSettings smtpClientSettings, ILogger logger)
19+
{
20+
_smtpClientSettings = smtpClientSettings;
21+
_logger = logger;
22+
}
23+
24+
/// <summary>
25+
/// Sends an email message with an HTML body.
26+
/// </summary>
27+
/// <param name="toEmailAddress">The email address of the receiver</param>
28+
/// <param name="subject">The email's subject</param>
29+
/// <param name="body">The HTML or text body of the emial</param>
30+
/// <returns>A task for the completion of the command.</returns>
31+
/// <exception cref="InvalidOperationException">
32+
/// Thrown when the underlying implementation throws any errors.
33+
/// </exception>
34+
public async Task SendEmailAsync(string toEmailAddress, string subject,
35+
string body)
36+
{
37+
try
38+
{
39+
_logger.LogInformation(
40+
$"Sending email to {toEmailAddress}, subject '{subject}'.");
41+
42+
var message = new MimeMessage();
43+
44+
message.Sender = new MailboxAddress(_smtpClientSettings.SenderName,
45+
_smtpClientSettings.SenderEmail);
46+
47+
foreach (var address in toEmailAddress.Split(';'))
48+
{
49+
message.To.Add(MailboxAddress.Parse(address));
50+
}
51+
52+
message.Subject = subject;
53+
message.Body = new TextPart("html") {Text = body};
54+
55+
using (var client = new SmtpClient())
56+
{
57+
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
58+
59+
await client.ConnectAsync(_smtpClientSettings.Server,
60+
_smtpClientSettings.Port);
61+
await client.AuthenticateAsync(_smtpClientSettings.Username,
62+
_smtpClientSettings.Password);
63+
await client.SendAsync(message);
64+
await client.DisconnectAsync(true);
65+
66+
_logger.LogInformation(
67+
$"Email sent to {toEmailAddress}, subject '{subject}'.");
68+
}
69+
}
70+
catch (Exception e)
71+
{
72+
_logger.LogError(
73+
$"Failed to send email to {toEmailAddress}: {e.Message}");
74+
}
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)