Advertisement
Guest User

Untitled

a guest
Jan 16th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.43 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel.DataAnnotations;
  3.  
  4. using System.Net.Mail;
  5. using System.Reactive.Subjects;
  6. using Serilog;
  7.  
  8.  
  9. namespace Rko.Eip.Dsa.Dap.Pipeline.NotificationService
  10. {
  11.     public class NotificationService : INotificationService
  12.     {
  13.         private readonly SmtpClientConfig _clientConfig;
  14.         private readonly ILogger _logger;
  15.         private readonly Subject<Notification> _mailSentNotification =  new Subject<Notification>();
  16.  
  17.         public NotificationService(SmtpClientConfig config, ILogger logger )
  18.         {
  19.             _clientConfig = config;
  20.             _logger = logger;
  21.             _mailSentNotification.Subscribe(n => DoJob(n));
  22.         }
  23.  
  24.         public void EnqueueNotification(Notification notification, NotificationOptions  options)
  25.         {
  26.  
  27.             var emailChecker = new EmailAddressAttribute();
  28.             //TODO: сделать валидацию и рассылку об ошибках  типа как в IValidatable в эко
  29.             foreach (var target in notification.Targets)
  30.             {
  31.                 if (emailChecker.IsValid(target) == false)
  32.                     throw new ArgumentException($"Target {target} is not a valid e-mail");
  33.             }
  34.  
  35.             if (emailChecker.IsValid(_clientConfig.Email) == false)
  36.                 throw new ArgumentException($"Sender {_clientConfig.Email} is not a valid e-mail");
  37.  
  38.             try
  39.             {
  40.                 _mailSentNotification.OnNext(notification);
  41.  
  42.             }
  43.             catch (Exception e)
  44.             {
  45.                 _logger.Error(e.Message);
  46.                 throw e;
  47.             }
  48.         }
  49.  
  50.         private void DoJob(Notification notification)
  51.         {
  52.             using (var smtp = new SmtpClient(_clientConfig.Host)
  53.             {
  54.                 EnableSsl = _clientConfig.EnableSsl,
  55.                 Port = _clientConfig.Port,
  56.                 Credentials = _clientConfig.Credentials
  57.             })
  58.             {
  59.                 var message = new MailMessage();
  60.                 foreach (var target in notification.Targets)
  61.                 {
  62.                     message.To.Add(target);
  63.                 }
  64.  
  65.                 message.From = new MailAddress(_clientConfig.Email, _clientConfig.Email);
  66.                 message.Subject = notification.Header;
  67.                 message.Body = notification.Text;
  68.  
  69.                 smtp.Send(message);
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement