diff --git a/LeaderboardBackend/Services/Impl/AccountRecoveryService.cs b/LeaderboardBackend/Services/Impl/AccountRecoveryService.cs index f67c82c4..8d61cd7f 100644 --- a/LeaderboardBackend/Services/Impl/AccountRecoveryService.cs +++ b/LeaderboardBackend/Services/Impl/AccountRecoveryService.cs @@ -14,18 +14,21 @@ public class AccountRecoveryService : IAccountRecoveryService private readonly IEmailSender _emailSender; private readonly IClock _clock; private readonly AppConfig _appConfig; + private readonly ILogger _logger; public AccountRecoveryService( ApplicationContext applicationContext, IEmailSender emailSender, IClock clock, - IOptions appConfig + IOptions appConfig, + ILogger logger ) { _applicationContext = applicationContext; _emailSender = emailSender; _clock = clock; _appConfig = appConfig.Value; + _logger = logger; } public async Task CreateRecoveryAndSendEmail(User user) @@ -55,8 +58,9 @@ await _emailSender.EnqueueEmailAsync( GenerateAccountRecoveryEmailBody(user, recovery) ); } - catch + catch (Exception e) { + _logger.LogWarning("Failed to send recovery email: {err}", e.Message); return new EmailFailed(); } diff --git a/LeaderboardBackend/Services/Impl/EmailSender.cs b/LeaderboardBackend/Services/Impl/EmailSender.cs index a74ad6a4..f8dcce5e 100644 --- a/LeaderboardBackend/Services/Impl/EmailSender.cs +++ b/LeaderboardBackend/Services/Impl/EmailSender.cs @@ -63,15 +63,34 @@ private async Task SendEmail(MimeMessage message) if (!_smtpClient.IsConnected) { - await _smtpClient.ConnectAsync(_config.Smtp!.Host, _config.Smtp.Port, _config.Smtp.UseSsl); + _logger.LogInformation("Connecting to client"); + try + { + await _smtpClient.ConnectAsync(_config.Smtp!.Host, _config.Smtp.Port, _config.Smtp.UseSsl); + _logger.LogError("Client connected"); + } + catch (Exception e) + { + _logger.LogError("Connecting failed: {message}", e.Message); + } } if ((_config.Smtp!.Username is not null || _config.Smtp.Password is not null) && !_smtpClient.IsAuthenticated) { - await _smtpClient.AuthenticateAsync(_config.Smtp.Username, _config.Smtp.Password); + _logger.LogInformation("Logging into client"); + try + { + await _smtpClient.AuthenticateAsync(_config.Smtp.Username, _config.Smtp.Password); + } + catch (Exception e) + { + _logger.LogError("Auth failed: {message}", e.Message); + } } - await _smtpClient.SendAsync(message); + _logger.LogInformation("Calling SendAsync"); + string res = await _smtpClient.SendAsync(message); + _logger.LogInformation("SendAsync done: {res}", res); } }