Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel.DataAnnotations;
- using System.Net.Mail;
- using System.Reactive.Subjects;
- using Serilog;
- namespace Rko.Eip.Dsa.Dap.Pipeline.NotificationService
- {
- public class NotificationService : INotificationService
- {
- private readonly SmtpClientConfig _clientConfig;
- private readonly ILogger _logger;
- private readonly Subject<Notification> _mailSentNotification = new Subject<Notification>();
- public NotificationService(SmtpClientConfig config, ILogger logger )
- {
- _clientConfig = config;
- _logger = logger;
- _mailSentNotification.Subscribe(n => DoJob(n));
- }
- public void EnqueueNotification(Notification notification, NotificationOptions options)
- {
- var emailChecker = new EmailAddressAttribute();
- //TODO: сделать валидацию и рассылку об ошибках типа как в IValidatable в эко
- foreach (var target in notification.Targets)
- {
- if (emailChecker.IsValid(target) == false)
- throw new ArgumentException($"Target {target} is not a valid e-mail");
- }
- if (emailChecker.IsValid(_clientConfig.Email) == false)
- throw new ArgumentException($"Sender {_clientConfig.Email} is not a valid e-mail");
- try
- {
- _mailSentNotification.OnNext(notification);
- }
- catch (Exception e)
- {
- _logger.Error(e.Message);
- throw e;
- }
- }
- private void DoJob(Notification notification)
- {
- using (var smtp = new SmtpClient(_clientConfig.Host)
- {
- EnableSsl = _clientConfig.EnableSsl,
- Port = _clientConfig.Port,
- Credentials = _clientConfig.Credentials
- })
- {
- var message = new MailMessage();
- foreach (var target in notification.Targets)
- {
- message.To.Add(target);
- }
- message.From = new MailAddress(_clientConfig.Email, _clientConfig.Email);
- message.Subject = notification.Header;
- message.Body = notification.Text;
- smtp.Send(message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement