using MimeKit; using ZB.MOM.WW.ScadaBridge.Commons.Entities.Notifications; namespace ZB.MOM.WW.ScadaBridge.NotificationService; /// /// Validates the sender and recipient email addresses before an SMTP /// delivery is attempted, so a malformed address surfaces as a clean error /// string rather than a ParseException escaping the delivery path. /// /// Public so the central Notification Outbox's EmailNotificationDeliveryAdapter /// can share this exact pre-send validation rather than carry a divergent copy. /// /// public static class EmailAddressValidator { /// /// Validates the sender and recipient email addresses, returning a /// human-readable error string if any is malformed, or null if all parse. /// /// The sender email address to validate. /// The list of recipient addresses to validate. /// A human-readable error string if any address is malformed; null if all addresses parse successfully. public static string? ValidateAddresses( string fromAddress, IReadOnlyList recipients) { if (!MailboxAddress.TryParse(fromAddress, out _)) { return $"Invalid sender (from) email address: '{fromAddress}'"; } var invalid = recipients .Where(r => !MailboxAddress.TryParse(r.EmailAddress, out _)) .Select(r => r.EmailAddress) .ToList(); return invalid.Count > 0 ? $"Invalid recipient email address(es): {string.Join(", ", invalid)}" : null; } }