@@ -12,7 +12,7 @@ public class EmailMessageRelay : BackgroundService
12
12
{
13
13
private readonly ILoggerService < EmailMessageRelay > _loggerService ;
14
14
private readonly int _pollingInterval ;
15
- private readonly string _senderEmailAddress ;
15
+ private readonly MailAddress _senderEmailAddress ;
16
16
private readonly IServiceProvider _serviceProvider ;
17
17
private readonly SmtpClient _smtpClient ;
18
18
@@ -24,7 +24,7 @@ public EmailMessageRelay(
24
24
_serviceProvider = serviceProvider ;
25
25
_loggerService = loggerService ;
26
26
_pollingInterval = smtpConfiguration . Value . PollingInterval ;
27
- _senderEmailAddress = smtpConfiguration . Value . SenderEmailAddress ;
27
+ _senderEmailAddress = new MailAddress ( smtpConfiguration . Value . SenderEmailAddress ) ;
28
28
_smtpClient = new SmtpClient ( smtpConfiguration . Value . Host , smtpConfiguration . Value . Port ) ;
29
29
_smtpClient . DeliveryMethod = SmtpDeliveryMethod . Network ;
30
30
}
@@ -41,7 +41,18 @@ private async Task SendEmails()
41
41
{
42
42
await transactionManager . TransactionScope ( async ( ) =>
43
43
{
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 )
45
56
{
46
57
Subject = emailMessage . Subject ,
47
58
Body = emailMessage . Body
0 commit comments