MailNotificationService.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using Microsoft.Extensions.Options;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Mail;
  7. using System.Threading.Tasks;
  8. namespace GreenTree.Maschinenbestellungen.Services.Notification
  9. {
  10. public class MailNotificationService : INotificationService
  11. {
  12. #region DI fields
  13. // The global mail notification options object
  14. private MailNotificationOptions _mailNotificationOptions;
  15. #endregion
  16. #region Fields
  17. // The SMTP client to send the mail notification messages
  18. private SmtpClient _smtpClient;
  19. #endregion
  20. #region Ctor
  21. /// <summary>
  22. /// Initializes a new instance of the MailNotificationService class
  23. /// </summary>
  24. /// <param name="mailNotificationOptions">The dependant mail notification options.</param>
  25. public MailNotificationService(
  26. IOptionsMonitor<MailNotificationOptions> mailNotificationOptions)
  27. {
  28. ConfigureService(mailNotificationOptions.CurrentValue);
  29. mailNotificationOptions.OnChange(config =>
  30. {
  31. ConfigureService(mailNotificationOptions.CurrentValue);
  32. });
  33. }
  34. #endregion
  35. #region Configuration
  36. /// <summary>
  37. /// Configure current service
  38. /// </summary>
  39. /// <param name="options">The service options.</param>
  40. private void ConfigureService(MailNotificationOptions options)
  41. {
  42. _mailNotificationOptions = options;
  43. // Create private SMTP client based to mail notification options
  44. _smtpClient = new SmtpClient(_mailNotificationOptions.SmtpServerAddress);
  45. // Check if Username is configured
  46. if (String.IsNullOrEmpty(_mailNotificationOptions.SmtpServerUsername)) return;
  47. // Use credentials on SMTP client
  48. _smtpClient.Credentials = new NetworkCredential(_mailNotificationOptions.SmtpServerUsername,
  49. _mailNotificationOptions.SmtpServerPassword, _mailNotificationOptions.SmtpServerDomain);
  50. }
  51. #endregion
  52. #region Implementation
  53. /// <summary>
  54. /// Sends a notification to a specific target
  55. /// </summary>
  56. /// <param name="target">The single target.</param>
  57. /// <param name="subject">The subject.</param>
  58. /// <param name="message">The message.</param>
  59. public void SendNotification(string target, string subject, string message)
  60. {
  61. // Parameter validation check
  62. if (String.IsNullOrEmpty(target))
  63. throw new ArgumentException("Cannot send notification to empty target.", "target");
  64. // Send message via SMTP client
  65. _smtpClient.Send(new MailMessage(_mailNotificationOptions.From, target, subject, message));
  66. }
  67. /// <summary>
  68. /// Sends a notification to multiple targets
  69. /// </summary>
  70. /// <param name="targets">The targets.</param>
  71. /// <param name="subject">The subject.</param>
  72. /// <param name="message">The message.</param>
  73. public void SendNotification(string[] targets, string subject, string message)
  74. {
  75. // Paramter validation check
  76. if (targets == null)
  77. throw new ArgumentException("Cannot send notification to empty targets.", "targets");
  78. // Loop through targets
  79. foreach (var target in targets)
  80. {
  81. // Use single SendNotification method for multiple targets
  82. SendNotification(target, subject, message);
  83. }
  84. }
  85. #endregion
  86. }
  87. }