Example #1
0
        /// <summary>
        /// Send an email with a job result according to the given environment parameters. If enough
        /// parameters are missing (i.e. recipient, SMTP host, or text), no email is sent.
        /// </summary>
        /// <param name="title">Email title</param>
        /// <param name="text">Body of the email - usually a job result</param>
        /// <param name="env">The environment for this email</param>
        public static void SendMail(string title, string text, Settings env)
        {
            if (string.IsNullOrWhiteSpace(env.MailTo) || string.IsNullOrWhiteSpace(text) || string.IsNullOrWhiteSpace(env.SmtpHost))
            return;

              // Whatever exception happens in here, just catch it and log it
              Logger.Catch(() =>
              {
            Logger.Debug("Sending results to " + env.MailTo);

            var msg = new MailMessage
            {
              Subject = title,
              Body = text,
              From = new MailAddress(string.IsNullOrWhiteSpace(env.MailFrom) ? env.MailTo : env.MailFrom)
            };

            foreach(var email in (env.MailTo ?? "").Split(',').Where(x => !string.IsNullOrWhiteSpace(x)))
              msg.To.Add(email.Trim());
            foreach (var email in (env.MailCc ?? "").Split(',').Where(x => !string.IsNullOrWhiteSpace(x)))
              msg.CC.Add(email);
            foreach (var email in (env.MailBcc ?? "").Split(',').Where(x => !string.IsNullOrWhiteSpace(x)))
              msg.Bcc.Add(email);

            if (env.SmtpSSL)
              Logger.Debug("Using SSL connection");

            var credentials = string.IsNullOrEmpty(env.SmtpUser) && string.IsNullOrEmpty(env.SmtpPass)
              ? null
              : new NetworkCredential(env.SmtpUser, env.SmtpPass);

            Program.MailSender.Send(msg, env.SmtpHost, env.SmtpSSL, credentials);
              });
        }
Example #2
0
        public static CronJob Parse(ConfigReader.JobArgs jobArgs, Settings settings = null)
        {
            if (jobArgs == null)
            return null;

              var job = new CronJob { Settings = settings != null ? settings.Clone() : new Settings() };

              if (jobArgs.Reboot)
              {
            job.Reboot = true;
            job.Command = jobArgs.Command;
            return job;
              }

              try
              {
            ParseValue(ref job.Minutes, jobArgs.Minute, 0, 59, false);
            ParseValue(ref job.Hours, jobArgs.Hour, 0, 23, false);
            ParseValue(ref job.Days, jobArgs.Day, 1, 31, false);
            ParseValue(ref job.Months, jobArgs.Month, 1, 12, false);
            ParseValue(ref job.Weekdays, jobArgs.Weekday, 0, 6, true);

            job.Command = jobArgs.Command;
            job.RecalcNextExecTime();

            return job;
              }
              catch (Exception e)
              {
            Logger.Error(e.Message);
            return null;
              }
        }
Example #3
0
        public void TestExists()
        {
            var env = new Settings();

              Assert.IsTrue(env.Exists("home"));
              Assert.IsTrue(env.Exists("MAILTO"));
              Assert.IsTrue(env.Exists("SmtpUser"));
              Assert.IsFalse(env.Exists("NotExists"));
        }
Example #4
0
        public void TestSet()
        {
            var env = new Settings();

              env.Set("home", "bork");
              env.Set("MAILTO", "xxx");

              Assert.AreEqual("bork", env.Home);
              Assert.AreEqual("xxx", env.MailTo);
        }
Example #5
0
        public static ServiceJob Parse(ConfigReader.JobArgs jobArgs, Settings settings = null)
        {
            if (jobArgs == null)
            return null;

              return new ServiceJob
              {
            Settings = settings != null ? settings.Clone() : new Settings(),
            Command = jobArgs.Command
              };
        }
Example #6
0
        public void TestClone()
        {
            var env = new Settings
              {
            Home = "home",
            MailTo = "to-email",
            MailFrom = "from-email",
            SmtpHost = "server",
            SmtpPass = "******",
            SmtpUser = "******"
              };

              var env2 = env.Clone();

              Assert.AreNotSame(env, env2);
              Assert.AreEqual(env.Home, env2.Home);
              Assert.AreEqual(env.MailFrom, env2.MailFrom);
              Assert.AreEqual(env.MailTo, env2.MailTo);
              Assert.AreEqual(env.SmtpHost, env2.SmtpHost);
              Assert.AreEqual(env.SmtpPass, env2.SmtpPass);
              Assert.AreEqual(env.SmtpUser, env2.SmtpUser);
        }
Example #7
0
 public void TestSetInvalid()
 {
     var env = new Settings();
       env.Set("xxx", "bork");
 }