private void Send(EmailInformation information) { var context = new IPTV2Entities(); context.Database.Connection.ConnectionString = RoleEnvironment.GetConfigurationSettingValue("Iptv2Entities"); Trace.WriteLine(context.Database.Connection.ConnectionString); try { if (information.isRecurring) { var usersWithRecurring = EmailReminder.GetRecurringUsers(context, information.numberOfDays, offeringId); if (usersWithRecurring != null) { var emailTemplateHTML = information.Template; foreach (var item in usersWithRecurring) { var template = emailTemplateHTML; var textBody = template.Replace("[firstname]", item.User.FirstName); textBody = textBody.Replace("[productname]", item.Product.Description); if (item.Product is SubscriptionProduct) { var sproduct = (SubscriptionProduct)item.Product; textBody = textBody.Replace("[newexpiry]", GetEntitlementEndDate(sproduct.Duration, sproduct.DurationType, item.EndDate.Value).ToString("MMMM dd, yyyy")); } Trace.TraceInformation("Trying to send email to " + item.User.EMail); EmailReminder.SendEmailViaSendGrid(item.User.EMail, fromEmail, information.Subject, String.Empty, TFCtv_EMail_Reminder.MailType.TextOnly, textBody, SendGridUsername, SendGridPassword, SendGridSmtpHost, SendGridSmtpPort); } } } else { var users = EmailReminder.GetUsers(context, information.numberOfDays, offeringId); if (users != null) { var emailTemplateHTML = information.Template; foreach (var user in users) { var template = emailTemplateHTML; var htmlBody = template.Replace("[firstname]", user.FirstName); var dealers = EmailReminder.GetDealerNearUser(context, user, offeringId); htmlBody = htmlBody.Replace("[dealers]", dealers); Trace.TraceInformation("Trying to send email to " + user.EMail); //Trace.WriteLine(htmlBody); EmailReminder.SendEmailViaSendGrid(user.EMail, fromEmail, information.Subject, htmlBody, TFCtv_EMail_Reminder.MailType.HtmlOnly, String.Empty, SendGridUsername, SendGridPassword, SendGridSmtpHost, SendGridSmtpPort); } } } } catch (Exception e) { Trace.TraceInformation("Error: " + e.Message); Trace.TraceInformation("Inner Exception: " + e.InnerException.Message); } }
public override void Run() { ISchedulerFactory schedFact = new StdSchedulerFactory(); sched = schedFact.GetScheduler(); sched.Start(); //Test if (Convert.ToBoolean(RoleEnvironment.GetConfigurationSettingValue("IsTestMode"))) cronExpression = RoleEnvironment.GetConfigurationSettingValue("cronExpressionTest"); // This is a sample worker implementation. Replace with your logic. Trace.TraceInformation("TFCtv EMail Reminder entry point called", "Information"); Trace.TraceInformation("Time now is " + DateTime.UtcNow); bool sendEmail = Convert.ToBoolean(RoleEnvironment.GetConfigurationSettingValue("sendEmail")); bool sendEmail3DaysBefore = Convert.ToBoolean(RoleEnvironment.GetConfigurationSettingValue("sendEmail3DaysBefore")); bool sendEmail1DayAfter = Convert.ToBoolean(RoleEnvironment.GetConfigurationSettingValue("sendEmail1DayAfter")); bool sendEmail14DaysAfter = Convert.ToBoolean(RoleEnvironment.GetConfigurationSettingValue("sendEmail14DaysAfter")); bool sendEmail30DaysAfter = Convert.ToBoolean(RoleEnvironment.GetConfigurationSettingValue("sendEmail30DaysAfter")); bool sendEmail4DaysBeforeRecurringBilling = Convert.ToBoolean(RoleEnvironment.GetConfigurationSettingValue("sendEmail4DaysBeforeRecurring")); if (sendEmail) { if (sendEmail3DaysBefore) { Trace.TraceInformation("Sending Emails (3 days before)"); Trace.TraceInformation("Template: " + subscription3DaysBeforeTemplate); var emailTemplateHTML = EmailReminder.GetEmailTemplate(subscription3DaysBeforeTemplate); EmailInformation information = new EmailInformation() { numberOfDays = -3, Subject = subscription3DaysBeforeSubject, Template = emailTemplateHTML, isRecurring = false, jobName = "3DaysBefore" }; CreateScheduledJob(information); } if (sendEmail1DayAfter) { Trace.TraceInformation("Sending Emails (1 day after)"); Trace.TraceInformation("Template: " + subscription1DayAfterTemplate); var emailTemplateHTML = EmailReminder.GetEmailTemplate(subscription1DayAfterTemplate); EmailInformation information = new EmailInformation() { numberOfDays = 1, Subject = subscription1DayAfterSubject, Template = emailTemplateHTML, isRecurring = false, jobName = "1DayAfter" }; CreateScheduledJob(information); } if (sendEmail14DaysAfter) { Trace.TraceInformation("Sending Emails (14 days after)"); Trace.TraceInformation("Template: " + subscription14DaysAfterTemplate); var emailTemplateHTML = EmailReminder.GetEmailTemplate(subscription14DaysAfterTemplate); EmailInformation information = new EmailInformation() { numberOfDays = 14, Subject = subscription14DaysAfterSubject, Template = emailTemplateHTML, isRecurring = false, jobName = "4DaysAfter" }; CreateScheduledJob(information); } if (sendEmail30DaysAfter) { Trace.TraceInformation("Sending Emails (30 days after)"); Trace.TraceInformation("Template: " + subscription30DaysAfterTemplate); var emailTemplateHTML = EmailReminder.GetEmailTemplate(subscription30DaysAfterTemplate); EmailInformation information = new EmailInformation() { numberOfDays = 30, Subject = subscription30DaysAfterSubject, Template = emailTemplateHTML, isRecurring = false, jobName = "14DaysAfter" }; CreateScheduledJob(information); } if (sendEmail4DaysBeforeRecurringBilling) { Trace.TraceInformation("Sending Emails (Recurring Billings 4 days before)"); Trace.TraceInformation("Template: " + recurring4DaysBeforeTemplate); var emailTemplateHTML = EmailReminder.GetEmailTemplate(recurring4DaysBeforeTemplate); EmailInformation information = new EmailInformation() { numberOfDays = -4, Subject = recurring4DaysBeforeSubject, Template = emailTemplateHTML, isRecurring = true, jobName = "4DaysBeforeRecurring" }; CreateScheduledJob(information); } //if (sendEmail3DaysBefore) // SendMail(context, subscription3DaysBeforeTemplate, -3, subscription3DaysBeforeSubject); //if (sendEmail1DayAfter) // SendMail(context, subscription1DayAfterTemplate, 1, subscription1DayAfterSubject); //if (sendEmail7DaysBefore) // SendMail(context, subscription14DaysAfterTemplate, 14, subscription14DaysAfterSubject); } Trace.TraceInformation("TFCtv Recurring Billing entry point called", "Information"); bool processRecurring = Convert.ToBoolean(RoleEnvironment.GetConfigurationSettingValue("processRecurring")); if (processRecurring) { Trace.TraceInformation("Processing Recurring Billing"); EmailInformation information = new EmailInformation() { jobName = "ProcessRecurring" }; //CreateRecurringScheduledJob(information); CreateScheduledJob(information); } while (true) { Thread.Sleep(10000); Trace.WriteLine("Thread is sleeping. DateTime: " + DateTime.UtcNow); //Trace.WriteLine("Working", "Information"); } }
public static void CreateScheduledJob(EmailInformation information) { //EmailReminder reminder = new EmailReminder(offeringId, fromEmail, SendGridUsername, SendGridPassword, SendGridSmtpHost, SendGridSmtpPort); //EmailReminder reminder = new EmailReminder(); //JobDetailImpl jobDetail = new JobDetailImpl(String.Format("myJob-{0}", numberOfDays), null, reminder.GetType()); //CronTriggerImpl trigger = new CronTriggerImpl(String.Format("DailyTrigger-{0}", numberOfDays), "Group1", cronExpression); IJobDetail jobDetail = null; var jobCronExpression = cronExpression; if (String.Compare(information.jobName, "ProcessRecurring", true) == 0) { jobDetail = JobBuilder.Create<RecurringProcessor>() .WithIdentity(String.Format("myJob-{0}", information.jobName), "Group1") .Build(); jobCronExpression = cronExpressionRecurring; } else { jobDetail = JobBuilder.Create<EmailReminder>() .WithIdentity(String.Format("myJob-{0}", information.jobName), "Group1") .Build(); jobCronExpression = cronExpression; } //IJobDetail jobDetail = JobBuilder.Create<EmailReminder>() // .WithIdentity(String.Format("myJob-{0}", information.jobName), "Group1") // .Build(); jobDetail.JobDataMap["Information"] = information; ITrigger trigger = TriggerBuilder.Create() .WithIdentity(String.Format("DailyTrigger-{0}", information.jobName), "Group1") .StartNow() .WithCronSchedule(jobCronExpression) .Build(); sched.ScheduleJob(jobDetail, trigger); var nextFireTime = trigger.GetNextFireTimeUtc(); Trace.TraceInformation("Next Fire Time:" + nextFireTime.Value); }