Advertisement
Guest User

Untitled

a guest
Mar 31st, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. // Creating and initializing
  2. var recipients = GetRecipientsList();
  3.  
  4. var notificationManager = new NotificationManager();
  5. var emailServiceSettings = new EmailServiceSettings
  6. {
  7.     Host = "10.19.13.2",
  8.     Port = 25,
  9.     UserName = "user",
  10.     Password = "password",
  11.     EnableSsl = false,
  12. };
  13. var emailSender = new DefaultSmtpEmailSender();
  14. emailSender.SetUpSettings(emailServiceSettings);
  15. var emailNotifier = new EmailNotifier(emailSender);
  16. notificationManager.Subscribers.Add(emailNotifier);
  17. notificationManager.Notify(new NotifyObject());
  18.  
  19. // Declaration
  20.  public class NotificationManager
  21.     {
  22.         public NotificationManager()
  23.         {
  24.             Subscribers = new List<IObserver<NotifyObject>>();
  25.         }
  26.  
  27.         public List<IObserver<NotifyObject>> Subscribers { get; private set; }
  28.  
  29.         public void Notify(NotifyObject message)
  30.         {
  31.             Subscribers.ForEach(x => x.OnNext(message));
  32.         }
  33.     }
  34.  
  35.     public class EmailNotifier : IObserver<NotifyObject>
  36.     {
  37.         private IEmailSender _emailSender;
  38.  
  39.         public EmailNotifier(IEmailSender emailSender)
  40.         {
  41.             _emailSender = emailSender;
  42.         }
  43.  
  44.         public void OnNext(NotifyObject notifyObject)
  45.         {
  46.             var email = new EmailContext
  47.             {
  48.                 From = "file2rest@pixabit-solutions.de",
  49.                 EmailSubject = "File2Rest Notificator",
  50.                 Message = notifyObject.Message,
  51.                 Encoding = Encoding.UTF8,
  52.                 IsHtmlBody = false
  53.             };
  54.  
  55.             _emailSender.SendEmail(new List<string> { "andrey.zherditskiy@pixabit-solutions.de" }, email);
  56.         }
  57.  
  58.         public void OnError(Exception error)
  59.         {
  60.             throw error;
  61.         }
  62.  
  63.         public void OnCompleted()
  64.         {
  65.          
  66.         }
  67.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement