public async Task <HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default) { try { using (var smtpConnection = new SmtpConnection(_options)) { if (await smtpConnection.ConnectAsync()) { if (_options.AccountOptions.Login) { var(user, password) = _options.AccountOptions.Account; if (!await smtpConnection.AuthenticateAsync(user, password).WithCancellationTokenAsync(cancellationToken)) { return(new HealthCheckResult(context.Registration.FailureStatus, description: $"Error login to smtp server {_options.Host}:{_options.Port} with configured credentials")); } } return(HealthCheckResult.Healthy()); } else { return(new HealthCheckResult(context.Registration.FailureStatus, description: $"Could not connect to smtp server {_options.Host}:{_options.Port} - SSL : {_options.ConnectionType}")); } } } catch (Exception ex) { return(new HealthCheckResult(context.Registration.FailureStatus, exception: ex)); } }
private async Task TestSmtpSettingsAsync() { Testing = new TestingContext { Message = Resources.TestingSmtpStatus }; try { using (var connection = new SmtpConnection { Security = SmtpSecurity }) { using (var auth = await connection.ConnectAsync(SmtpHost, SmtpPort)) { var username = UseImapCredentialsForSmtp ? ImapUsername : SmtpUsername; var password = UseImapCredentialsForSmtp ? ImapPassword : SmtpPassword; await auth.LoginAsync(username, password); } } } catch (Exception ex) { Testing = new TestingContext { IsFaulted = true, Message = ex.Message, }; Logger.Error(ex); } }
public async Task <(string, bool)> IsHealthy(HttpContext context, LivenessExecutionContext livenessContext, CancellationToken cancellationToken = default) { try { using (var smtpConnection = new SmtpConnection(_options)) { if (await smtpConnection.ConnectAsync()) { if (_options.AccountOptions.login) { var(user, password) = _options.AccountOptions.account; if (!await smtpConnection.AuthenticateAsync(user, password)) { return($"Error login to smtp server{_options.Host}:{_options.Port} with configured credentials", false); } } return(BeatPulseKeys.BEATPULSE_HEALTHCHECK_DEFAULT_OK_MESSAGE, true); } else { return($"Could not connect to smtp server {_options.Host}:{_options.Port} - SSL : {_options.ConnectionType}", false); } } } catch (Exception ex) { var message = !livenessContext.IsDevelopment ? string.Format(BeatPulseKeys.BEATPULSE_HEALTHCHECK_DEFAULT_ERROR_MESSAGE, livenessContext.Name) : $"Exception {ex.GetType().Name} with message ('{ex.Message}')"; return(message, false); } }
public async Task <LivenessResult> IsHealthy(LivenessExecutionContext context, CancellationToken cancellationToken = default) { try { _logger?.LogInformation($"{nameof(SmtpLiveness)} is checking SMTP connections."); using (var smtpConnection = new SmtpConnection(_options)) { if (await smtpConnection.ConnectAsync()) { if (_options.AccountOptions.Login) { var(user, password) = _options.AccountOptions.Account; if (!await smtpConnection.AuthenticateAsync(user, password)) { _logger?.LogWarning($"The {nameof(SmtpLiveness)} check fail with invalid login to smtp server {_options.Host}:{_options.Port} with configured credentials."); return(LivenessResult.UnHealthy($"Error login to smtp server{_options.Host}:{_options.Port} with configured credentials")); } } _logger?.LogInformation($"The {nameof(SmtpLiveness)} check success."); return(LivenessResult.Healthy()); } else { _logger?.LogWarning($"The {nameof(SmtpLiveness)} check fail for connecting to smtp server {_options.Host}:{_options.Port} - SSL : {_options.ConnectionType}."); return(LivenessResult.UnHealthy($"Could not connect to smtp server {_options.Host}:{_options.Port} - SSL : {_options.ConnectionType}")); } } } catch (Exception ex) { _logger?.LogWarning($"The {nameof(SmtpLiveness)} check fail with the exception {ex.ToString()}."); return(LivenessResult.UnHealthy(ex)); } }
internal async Task ProcessOutgoingMessagesAsync() { var requests = await GetPendingSmtpRequestsAsync(); if (!requests.Any() || _sendingMessages) { return; } _sendingMessages = true; foreach (var request in requests) { try { using (var connection = new SmtpConnection { Security = _account.SmtpSecurity }) { using (var auth = await connection.ConnectAsync(_account.SmtpHost, _account.SmtpPort)) { using (var session = await auth.LoginAsync(_account.SmtpUsername, _account.SmtpPassword)) { await session.SendAsync(request.Mime); } } } var context = _smtpRequests.FirstOrDefault(x => x.Id == request.Id); if (context != null) { _smtpRequests.Remove(context); } await DeleteRequestFromDatabaseAsync(request); await CountSmtpRequestsAsync(); } catch (Exception ex) { Logger.Error(ex); } finally { _sendingMessages = false; } } }