MailNotificationService.cs 3.1 KB

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