| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- using Microsoft.Extensions.Options;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Net.Mail;
- using System.Threading.Tasks;
- namespace GreenTree.Maschinenbestellungen.Services.Notification
- {
- public class MailNotificationService : INotificationService
- {
- #region DI fields
- // The global mail notification options object
- private MailNotificationOptions _mailNotificationOptions;
- #endregion
- #region Fields
- // The SMTP client to send the mail notification messages
- private SmtpClient _smtpClient;
- #endregion
- #region Ctor
- /// <summary>
- /// Initializes a new instance of the MailNotificationService class
- /// </summary>
- /// <param name="mailNotificationOptions">The dependant mail notification options.</param>
- public MailNotificationService(
- IOptionsMonitor<MailNotificationOptions> mailNotificationOptions)
- {
- ConfigureService(mailNotificationOptions.CurrentValue);
- mailNotificationOptions.OnChange(config =>
- {
- ConfigureService(mailNotificationOptions.CurrentValue);
- });
- }
- #endregion
- #region Configuration
- /// <summary>
- /// Configure current service
- /// </summary>
- /// <param name="options">The service options.</param>
- private void ConfigureService(MailNotificationOptions options)
- {
- _mailNotificationOptions = options;
- // Create private SMTP client based to mail notification options
- _smtpClient = new SmtpClient(_mailNotificationOptions.SmtpServerAddress);
- // Check if Username is configured
- if (String.IsNullOrEmpty(_mailNotificationOptions.SmtpServerUsername)) return;
- // Use credentials on SMTP client
- _smtpClient.Credentials = new NetworkCredential(_mailNotificationOptions.SmtpServerUsername,
- _mailNotificationOptions.SmtpServerPassword, _mailNotificationOptions.SmtpServerDomain);
- }
- #endregion
- #region Implementation
- /// <summary>
- /// Sends a notification to a specific target
- /// </summary>
- /// <param name="target">The single target.</param>
- /// <param name="subject">The subject.</param>
- /// <param name="message">The message.</param>
- public void SendNotification(string target, string subject, string message)
- {
- // Parameter validation check
- if (String.IsNullOrEmpty(target))
- throw new ArgumentException("Cannot send notification to empty target.", "target");
- // Send message via SMTP client
- _smtpClient.Send(new MailMessage(_mailNotificationOptions.From, target, subject, message));
- }
- /// <summary>
- /// Sends a notification to multiple targets
- /// </summary>
- /// <param name="targets">The targets.</param>
- /// <param name="subject">The subject.</param>
- /// <param name="message">The message.</param>
- public void SendNotification(string[] targets, string subject, string message)
- {
- // Paramter validation check
- if (targets == null)
- throw new ArgumentException("Cannot send notification to empty targets.", "targets");
- // Loop through targets
- foreach (var target in targets)
- {
- // Use single SendNotification method for multiple targets
- SendNotification(target, subject, message);
- }
- }
- #endregion
- }
- }
|