Example #1
0
        /// <summary>
        /// Tries to send an email message.
        /// </summary>
        /// <param name="recipients">Intended recipients of the message.</param>
        /// <param name="subject">Subject of the message.</param>
        /// <param name="body">Body of the message.</param>
        /// <returns>Successful sending?</returns>
        public bool TrySend(IEnumerable<string> recipients, string subject, string body)
        {
            if (recipients == null)
            throw new ArgumentNullException("recipients");
              if (subject == null)
            throw new ArgumentNullException("subject");
              if (body == null)
            throw new ArgumentNullException("body");

              List<string> goodRecipients = new List<string>();
              foreach (string recipient in recipients)
              {
            if (recipient.IsNullOrEmpty())
            {
              this.logger.Log(LogLevel.Warning, "Recpient is null or empty.");
            }
            else if (!Mailer.IsEmailAddressValid(recipient))
            {
              this.logger.Log(LogLevel.Warning, "Recpient {0} is not valid.", recipient);
            }
            else
            {
              goodRecipients.Add(recipient);
            }
              }

              if (goodRecipients.Count > 0)
              {
            this.logger.Log(LogLevel.Debug, "Sending mail to {0}.", string.Join(", ", goodRecipients.ToArray()));

            try
            {
              MailMessage message = new MailMessage();
              message.From = new MailAddress(this.serverConfig.MailAuthorityAddress);
              message.Subject = subject;
              message.Body = body;
              goodRecipients.Foreach(recipient => message.To.Add(recipient));

              SmtpClient client = new SmtpClient(this.serverConfig.MailServerAddress, this.serverConfig.MailServerPort);
              client.Send(message);

              this.logger.Log(LogLevel.Debug, "Mail to {0} sent.", string.Join(", ", recipients.ToArray()));

              return true;
            }
            catch (Exception exception)
            {
              this.logger.Log(LogLevel.Warning, "Sending mail to {0} failed: {1}", string.Join(", ", recipients.ToArray()), exception.ToString());
              return false;
            }
              }
              else
              {
            this.logger.Log(LogLevel.Warning, "Sending no mail since no acceptable recipient found.");
            return false;
              }
        }
Example #2
0
File: Sheet.cs Project: bg0jr/Maui
        private void Run()
        {
            if ( !Directory.Exists( WorkbookPath ) )
            {
                throw new FileNotFoundException( "No workbook found at: " + WorkbookPath );
            }

            var workbook = new CsvWorkbook( WorkbookPath );

            Body.Open( workbook );

            var resultSheets = new List<CsvWorksheet>();
            if ( Body.ResultSheets != null )
            {
                resultSheets.AddRange( workbook.Worksheets
                    .Where( worksheet => Body.ResultSheets.Contains( worksheet.Name ) )
                    .OfType<CsvWorksheet>() );

                resultSheets.Foreach( worksheet => worksheet.Modified += OnWorksheetModified );
            }

            Body.Calculate();
            Body.Close();

            workbook.Save();

            Console.WriteLine();
            resultSheets.Foreach( worksheet => worksheet.Modified -= OnWorksheetModified );
            resultSheets.Foreach( worksheet => worksheet.Print() );
        }