summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.lib/Clients/EmailClient.cs19
-rw-r--r--src/main.lib/RenewalCreator.cs2
-rw-r--r--src/main.lib/RenewalManager.cs6
-rw-r--r--src/main.lib/Services/NotificationService.cs13
4 files changed, 22 insertions, 18 deletions
diff --git a/src/main.lib/Clients/EmailClient.cs b/src/main.lib/Clients/EmailClient.cs
index 5e78920..5c98239 100644
--- a/src/main.lib/Clients/EmailClient.cs
+++ b/src/main.lib/Clients/EmailClient.cs
@@ -66,13 +66,13 @@ namespace PKISharp.WACS.Clients
public bool Enabled { get; internal set; }
- public void Send(string subject, string content, MessagePriority priority)
+ public async Task Send(string subject, string content, MessagePriority priority)
{
if (Enabled)
{
+ using var client = new SmtpClient();
try
{
- using var client = new SmtpClient();
var options = SecureSocketOptions.None;
if (_secure)
{
@@ -99,10 +99,10 @@ namespace PKISharp.WACS.Clients
options = SecureSocketOptions.StartTls;
}
}
- client.Connect(_server, _port, options);
+ await client.ConnectAsync(_server, _port, options);
if (!string.IsNullOrEmpty(_user))
{
- client.Authenticate(new NetworkCredential(_user, _password));
+ await client.AuthenticateAsync(new NetworkCredential(_user, _password));
}
foreach (var receiverAddress in _receiverAddresses)
{
@@ -121,17 +121,21 @@ namespace PKISharp.WACS.Clients
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = content + $"<p>Sent by win-acme version {_version} from {_computerName}</p>";
message.Body = bodyBuilder.ToMessageBody();
- client.Send(message);
+ await client.SendAsync(message);
}
}
catch (Exception ex)
{
_log.Error(ex, "Problem sending e-mail");
+ }
+ finally
+ {
+ await client.DisconnectAsync(true);
}
}
}
- internal Task Test()
+ internal async Task Test()
{
if (!Enabled)
{
@@ -140,12 +144,11 @@ namespace PKISharp.WACS.Clients
else
{
_log.Information("Sending test message...");
- Send("Test notification",
+ await Send("Test notification",
"<p>If you are reading this, it means you will receive notifications about critical errors in the future.</p>",
MessagePriority.Normal);
_log.Information("Test message sent!");
}
- return Task.CompletedTask;
}
}
}
diff --git a/src/main.lib/RenewalCreator.cs b/src/main.lib/RenewalCreator.cs
index 404ee76..4a198db 100644
--- a/src/main.lib/RenewalCreator.cs
+++ b/src/main.lib/RenewalCreator.cs
@@ -370,7 +370,7 @@ namespace PKISharp.WACS
try
{
_renewalStore.Save(renewal, result);
- _notification.NotifyCreated(renewal, _log.Lines);
+ await _notification.NotifyCreated(renewal, _log.Lines);
}
catch (Exception ex)
{
diff --git a/src/main.lib/RenewalManager.cs b/src/main.lib/RenewalManager.cs
index 93ef1df..af7ba9f 100644
--- a/src/main.lib/RenewalManager.cs
+++ b/src/main.lib/RenewalManager.cs
@@ -494,18 +494,18 @@ namespace PKISharp.WACS
_renewalStore.Save(renewal, result);
if (result.Success)
{
- notification.NotifySuccess(renewal, _log.Lines);
+ await notification.NotifySuccess(renewal, _log.Lines);
}
else
{
- notification.NotifyFailure(runLevel, renewal, result.ErrorMessages, _log.Lines);
+ await notification.NotifyFailure(runLevel, renewal, result.ErrorMessages, _log.Lines);
}
}
}
catch (Exception ex)
{
_exceptionHandler.HandleException(ex);
- notification.NotifyFailure(runLevel, renewal, new List<string> { ex.Message }, _log.Lines);
+ await notification.NotifyFailure(runLevel, renewal, new List<string> { ex.Message }, _log.Lines);
}
}
diff --git a/src/main.lib/Services/NotificationService.cs b/src/main.lib/Services/NotificationService.cs
index cff3803..7795fac 100644
--- a/src/main.lib/Services/NotificationService.cs
+++ b/src/main.lib/Services/NotificationService.cs
@@ -5,6 +5,7 @@ using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Linq;
+using System.Threading.Tasks;
using System.Web;
namespace PKISharp.WACS.Services
@@ -33,7 +34,7 @@ namespace PKISharp.WACS.Services
/// </summary>
/// <param name="runLevel"></param>
/// <param name="renewal"></param>
- internal void NotifyCreated(Renewal renewal, IEnumerable<MemoryEntry> log)
+ internal async Task NotifyCreated(Renewal renewal, IEnumerable<MemoryEntry> log)
{
// Do not send emails when running interactively
_log.Information(
@@ -42,7 +43,7 @@ namespace PKISharp.WACS.Services
renewal.LastFriendlyName);
if (_settings.Notification.EmailOnSuccess)
{
- _email.Send(
+ await _email.Send(
$"Certificate {renewal.LastFriendlyName} created",
@$"<p>Certificate <b>{HttpUtility.HtmlEncode(renewal.LastFriendlyName)}</b> succesfully created.</p>
{NotificationInformation(renewal)}
@@ -56,7 +57,7 @@ namespace PKISharp.WACS.Services
/// </summary>
/// <param name="runLevel"></param>
/// <param name="renewal"></param>
- internal void NotifySuccess(Renewal renewal, IEnumerable<MemoryEntry> log)
+ internal async Task NotifySuccess(Renewal renewal, IEnumerable<MemoryEntry> log)
{
// Do not send emails when running interactively
_log.Information(
@@ -65,7 +66,7 @@ namespace PKISharp.WACS.Services
renewal.LastFriendlyName);
if (_settings.Notification.EmailOnSuccess)
{
- _email.Send(
+ await _email.Send(
$"Certificate renewal {renewal.LastFriendlyName} completed",
@$"<p>Certificate <b>{HttpUtility.HtmlEncode(renewal.LastFriendlyName)}</b> succesfully renewed.</p>
{NotificationInformation(renewal)}
@@ -79,7 +80,7 @@ namespace PKISharp.WACS.Services
/// </summary>
/// <param name="runLevel"></param>
/// <param name="renewal"></param>
- internal void NotifyFailure(
+ internal async Task NotifyFailure(
RunLevel runLevel,
Renewal renewal,
List<string> errors,
@@ -93,7 +94,7 @@ namespace PKISharp.WACS.Services
}
if (runLevel.HasFlag(RunLevel.Unattended))
{
- _email.Send(
+ await _email.Send(
$"Error processing certificate renewal {renewal.LastFriendlyName}",
@$"<p>Renewal for <b>{HttpUtility.HtmlEncode(renewal.LastFriendlyName)}</b> failed, will retry on next run.<br><br>Error(s):
<ul><li>{string.Join("</li><li>", errors.Select(x => HttpUtility.HtmlEncode(x)))}</li></ul></p>