Skip to content

Commit af1c978

Browse files
committed
Added logging of invalid email address in EmailMessageRelay.cs.
1 parent 6aab78f commit af1c978

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

ClubService.Domain/Repository/ILoggerService.cs

+1
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,5 @@ public interface ILoggerService<T>
6464
void LogJsonMissingProperties(string jsonValue);
6565
void LogEmailMessageRelayStop();
6666
void LogSystemOperatorRegistered(Guid id);
67+
void LogInvalidEMailAddress(string emailAddress);
6768
}

ClubService.Infrastructure/Logging/LoggerService.cs

+5
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,9 @@ public void LogSystemOperatorRegistered(Guid id)
320320
{
321321
logger.LogInformation("Registered system operator with id '{id}'.", id);
322322
}
323+
324+
public void LogInvalidEMailAddress(string emailAddress)
325+
{
326+
logger.LogError("The following email address is invalid: {emailMessageRecipientEMailAddress}", emailAddress);
327+
}
323328
}

ClubService.Infrastructure/Mail/EmailMessageRelay.cs

+14-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class EmailMessageRelay : BackgroundService
1212
{
1313
private readonly ILoggerService<EmailMessageRelay> _loggerService;
1414
private readonly int _pollingInterval;
15-
private readonly string _senderEmailAddress;
15+
private readonly MailAddress _senderEmailAddress;
1616
private readonly IServiceProvider _serviceProvider;
1717
private readonly SmtpClient _smtpClient;
1818

@@ -24,7 +24,7 @@ public EmailMessageRelay(
2424
_serviceProvider = serviceProvider;
2525
_loggerService = loggerService;
2626
_pollingInterval = smtpConfiguration.Value.PollingInterval;
27-
_senderEmailAddress = smtpConfiguration.Value.SenderEmailAddress;
27+
_senderEmailAddress = new MailAddress(smtpConfiguration.Value.SenderEmailAddress);
2828
_smtpClient = new SmtpClient(smtpConfiguration.Value.Host, smtpConfiguration.Value.Port);
2929
_smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
3030
}
@@ -41,7 +41,18 @@ private async Task SendEmails()
4141
{
4242
await transactionManager.TransactionScope(async () =>
4343
{
44-
var mailMessage = new MailMessage(_senderEmailAddress, emailMessage.RecipientEMailAddress)
44+
MailAddress recipientEmailAddress;
45+
try
46+
{
47+
recipientEmailAddress = new MailAddress(emailMessage.RecipientEMailAddress);
48+
}
49+
catch (FormatException e)
50+
{
51+
_loggerService.LogInvalidEMailAddress(emailMessage.RecipientEMailAddress);
52+
throw;
53+
}
54+
55+
var mailMessage = new MailMessage(_senderEmailAddress, recipientEmailAddress)
4556
{
4657
Subject = emailMessage.Subject,
4758
Body = emailMessage.Body

0 commit comments

Comments
 (0)