Skip to content

Commit 5fb5034

Browse files
committed
SMTP health check changes
1 parent 027090a commit 5fb5034

1 file changed

Lines changed: 9 additions & 15 deletions

File tree

src/Exceptionless.Insulation/Mail/MailKitMailSender.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,15 @@ private MimeMessage CreateMailMessage(MailMessage notification)
110110

111111
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
112112
{
113-
if (_lastSuccessfulConnection.IsAfter(_timeProvider.GetUtcNow().UtcDateTime.Subtract(TimeSpan.FromMinutes(5))))
114-
return HealthCheckResult.Healthy();
115-
116113
string? host = _emailOptions.SmtpHost;
117114
if (String.IsNullOrEmpty(host))
118-
return HealthCheckResult.Unhealthy("Email is not configured");
115+
{
116+
_logger.LogWarning("Email is not configured");
117+
return HealthCheckResult.Healthy("Email is not configured");
118+
}
119+
120+
if (_lastSuccessfulConnection.IsAfter(_timeProvider.GetUtcNow().UtcDateTime.Subtract(TimeSpan.FromMinutes(5))))
121+
return HealthCheckResult.Healthy();
119122

120123
var sw = Stopwatch.StartNew();
121124

@@ -126,22 +129,13 @@ public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context
126129
var encryption = GetSecureSocketOption(_emailOptions.SmtpEncryption);
127130

128131
await client.ConnectAsync(host, port, encryption, cancellationToken);
129-
130-
// Note: since we don't have an OAuth2 token, disable the XOAUTH2 authentication mechanism.
131-
client.AuthenticationMechanisms.Remove("XOAUTH2");
132-
133-
string? user = _emailOptions.SmtpUser;
134-
if (!String.IsNullOrEmpty(user))
135-
{
136-
await client.AuthenticateAsync(user, _emailOptions.SmtpPassword, cancellationToken);
137-
}
138-
139132
await client.DisconnectAsync(true, cancellationToken);
140133
_lastSuccessfulConnection = _timeProvider.GetUtcNow().UtcDateTime;
141134
}
142135
catch (Exception ex) when (ex is not OperationCanceledException)
143136
{
144-
return HealthCheckResult.Unhealthy("Email Not Working.", ex);
137+
// Proactive check failure — report degraded so liveness probe (HTTP 200) still passes.
138+
return HealthCheckResult.Degraded("SMTP connection check failed.", ex);
145139
}
146140
finally
147141
{

0 commit comments

Comments
 (0)