/// <inheritdoc/> public void Run() { var watcher = new FileSystemWatcher { Path = _setUpManager.ReadSetting(SetUpConstants.SourcePath), Filter = EXTENSION }; watcher.Created += new FileSystemEventHandler(OnCreated); watcher.Deleted += new FileSystemEventHandler(OnDeleted); watcher.EnableRaisingEvents = true; }
private void DeleteFile(FileSystemEventArgs e) { try { var timeOut = int.Parse(_setUpManager.ReadSetting(SetUpConstants.TimeOut)); Thread.Sleep(timeOut); File.Delete(e.FullPath); } catch (Exception ex) { _logger.LogError("TimeOut value hasn't been parsed.", ex); } }
/// <inheritdoc/> public bool Send(string fileName) { bool result = false; var smtp = new SmtpClient { EnableSsl = true }; smtp.SendCompleted += delegate(object sender, AsyncCompletedEventArgs e) { if (e.Cancelled) { _logger.LogInfo($"The message with {fileName} was cancelled."); } if (e.Error != null) { _logger.LogError($"The message with {fileName} wasn't sent.", e.Error); } else { _logger.LogInfo("Message was sent."); result = true; } }; using (var mail = _mailGenerator.GenerateMessage(fileName)) { smtp.SendMailAsync(mail); try { var timeOut = int.Parse(_setUpManager.ReadSetting(SetUpConstants.TimeOut)); Thread.Sleep(timeOut); } catch (Exception ex) { _logger.LogError("TimeOut value hasn't been parsed.", ex); } } return(result); }
/// <inheritdoc/> public MailMessage GenerateMessage(string fileName) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentException("Name of file can't be null or empty!", nameof(fileName)); } var mail = new MailMessage(); mail.To.Add(new MailAddress(_setUpManager.ReadSetting(SetUpConstants.ReceiverMailAddress))); mail.Subject = "The file was added."; mail.Body = $"The file {fileName} was added."; var attach = new Attachment(fileName, MediaTypeNames.Application.Octet); CreateAttachment(fileName, attach); mail.Attachments.Add(attach); return(mail); }