-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathEmailClient.cs
69 lines (57 loc) · 2.43 KB
/
EmailClient.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
using KCert.Models;
using System.Net;
using System.Net.Mail;
namespace KCert.Services;
[Service]
public class EmailClient(ILogger<EmailClient> log, KCertConfig cfg)
{
private const string TestSubject = "KCert Test Email";
private const string TestMessage = "If you received this, then KCert is able to send emails!";
public async Task SendTestEmailAsync()
{
log.LogInformation("Attempting to send a test email.");
await SendAsync(TestSubject, TestMessage);
}
public async Task NotifyRenewalResultAsync(string secretNamespace, string secretName, RenewalException? ex)
{
await SendAsync(RenewalSubject(secretNamespace, secretName, ex), RenewalMessage(secretNamespace, secretName, ex));
}
public async Task NotifyFailureAsync(string message, Exception ex)
{
var subject = "KCert encountered an unexpected error";
var body = $"{message}\n\n{ex.Message}\n\n{ex.StackTrace}";
await SendAsync(subject, body);
}
private async Task SendAsync(string subject, string text)
{
if (cfg.SmtpHost == null || cfg.SmtpUser == null || cfg.SmtpPass == null || cfg.SmtpEmailFrom == null)
{
log.LogInformation("Cannot send email email because it's not configured correctly");
return;
}
var client = new SmtpClient(cfg.SmtpHost, cfg.SmtpPort)
{
EnableSsl = true,
Credentials = new NetworkCredential(cfg.SmtpUser, cfg.SmtpPass),
};
var message = new MailMessage(cfg.SmtpEmailFrom, cfg.AcmeEmail, subject, text);
await client.SendMailAsync(message);
}
private static string RenewalSubject(string secretNamespace, string secretName, RenewalException? ex = null)
{
var isSuccess = ex == null;
var status = isSuccess ? "succeeded" : "failed";
return $"KCert Renewal of secret [{secretNamespace}:{secretName}] {status}";
}
private static string RenewalMessage(string secretNamespace, string secretName, RenewalException? ex = null)
{
var lines = new List<string>() { $"Renewal of secret [{secretNamespace}:{secretName}] completed with status: " + (ex == null ? "Success" : "Failure") };
if (ex != null)
{
lines.Add("\nLogs:\n");
lines.Add(string.Join('\n', ex.Logs));
lines.Add($"Error:\n\n{ex.Message}\n\n{ex.StackTrace}");
}
return string.Join('\n', lines);
}
}