Skip to content

Commit

Permalink
Added logging of invalid email address in EmailMessageRelay.cs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Smightym8 committed Jul 10, 2024
1 parent 6aab78f commit af1c978
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions ClubService.Domain/Repository/ILoggerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,5 @@ public interface ILoggerService<T>
void LogJsonMissingProperties(string jsonValue);
void LogEmailMessageRelayStop();
void LogSystemOperatorRegistered(Guid id);
void LogInvalidEMailAddress(string emailAddress);
}
5 changes: 5 additions & 0 deletions ClubService.Infrastructure/Logging/LoggerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,9 @@ public void LogSystemOperatorRegistered(Guid id)
{
logger.LogInformation("Registered system operator with id '{id}'.", id);
}

public void LogInvalidEMailAddress(string emailAddress)
{
logger.LogError("The following email address is invalid: {emailMessageRecipientEMailAddress}", emailAddress);
}
}
17 changes: 14 additions & 3 deletions ClubService.Infrastructure/Mail/EmailMessageRelay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class EmailMessageRelay : BackgroundService
{
private readonly ILoggerService<EmailMessageRelay> _loggerService;
private readonly int _pollingInterval;
private readonly string _senderEmailAddress;
private readonly MailAddress _senderEmailAddress;
private readonly IServiceProvider _serviceProvider;
private readonly SmtpClient _smtpClient;

Expand All @@ -24,7 +24,7 @@ public EmailMessageRelay(
_serviceProvider = serviceProvider;
_loggerService = loggerService;
_pollingInterval = smtpConfiguration.Value.PollingInterval;
_senderEmailAddress = smtpConfiguration.Value.SenderEmailAddress;
_senderEmailAddress = new MailAddress(smtpConfiguration.Value.SenderEmailAddress);
_smtpClient = new SmtpClient(smtpConfiguration.Value.Host, smtpConfiguration.Value.Port);
_smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
}
Expand All @@ -41,7 +41,18 @@ private async Task SendEmails()
{
await transactionManager.TransactionScope(async () =>
{
var mailMessage = new MailMessage(_senderEmailAddress, emailMessage.RecipientEMailAddress)
MailAddress recipientEmailAddress;
try
{
recipientEmailAddress = new MailAddress(emailMessage.RecipientEMailAddress);
}
catch (FormatException e)
{
_loggerService.LogInvalidEMailAddress(emailMessage.RecipientEMailAddress);
throw;
}

var mailMessage = new MailMessage(_senderEmailAddress, recipientEmailAddress)
{
Subject = emailMessage.Subject,
Body = emailMessage.Body
Expand Down

0 comments on commit af1c978

Please sign in to comment.