public NotificationMail GetNotificationMail(string notificationType) { try { var notifications = new XmlDocument(); notifications.Load(Config.ConfigurationFile); var settings = notifications.SelectSingleNode("//global"); var node = notifications.SelectSingleNode(string.Format("//instant//notification [@name = '{0}']", notificationType)); var details = new XmlDocument(); var cont = details.CreateElement("details"); cont.AppendChild(details.ImportNode(settings, true)); cont.AppendChild(details.ImportNode(node, true)); var detailsChild = details.AppendChild(cont); var notificationMail = new NotificationMail { FromMail = detailsChild.SelectSingleNode("//email").InnerText, FromName = detailsChild.SelectSingleNode("//name").InnerText, Subject = detailsChild.SelectSingleNode("//subject").InnerText, Domain = detailsChild.SelectSingleNode("//domain").InnerText, Body = detailsChild.SelectSingleNode("//body").InnerText }; return notificationMail; } catch (Exception e) { LogHelper.Error<MarkAsSolutionReminder>(string.Format("Couldn't get settings for {0}", notificationType), e); throw; } }
public NotificationMail GetNotificationMail(string notificationType) { try { var notifications = new XmlDocument(); notifications.Load(Config.ConfigurationFile); var settings = notifications.SelectSingleNode("//global"); var node = notifications.SelectSingleNode(string.Format("//instant//notification [@name = '{0}']", notificationType)); var details = new XmlDocument(); var cont = details.CreateElement("details"); cont.AppendChild(details.ImportNode(settings, true)); cont.AppendChild(details.ImportNode(node, true)); var detailsChild = details.AppendChild(cont); var notificationMail = new NotificationMail { FromMail = detailsChild.SelectSingleNode("//email").InnerText, FromName = detailsChild.SelectSingleNode("//name").InnerText, Subject = detailsChild.SelectSingleNode("//subject").InnerText, Domain = detailsChild.SelectSingleNode("//domain").InnerText, Body = detailsChild.SelectSingleNode("//body").InnerText }; return(notificationMail); } catch (Exception e) { LogHelper.Error <MarkAsSolutionReminder>(string.Format("Couldn't get settings for {0}", notificationType), e); throw; } }
public void SendNotification(int topicId, int memberId, NotificationMail notificationMail) { try { var topicService = new TopicService(ApplicationContext.Current.DatabaseContext); var topic = topicService.GetById(topicId); using (ContextHelper.EnsureHttpContext()) { var memberShipHelper = new MembershipHelper(UmbracoContext.Current); var member = memberShipHelper.GetById(memberId); using (var smtpClient = new SmtpClient()) { var fromMailAddress = new MailAddress(notificationMail.FromMail, notificationMail.FromName); var subject = string.Format("{0} - '{1}'", notificationMail.Subject, topic.Title); var domain = notificationMail.Domain; var body = notificationMail.Body; body = string.Format(body, topic.Title, "https://" + domain + topic.GetUrl()); if (member.GetPropertyValue <bool>("bugMeNot") == false) { const string notificationTestFolder = "~/App_Data/NotificationTest"; if (Directory.Exists(HostingEnvironment.MapPath(notificationTestFolder)) == false) { Directory.CreateDirectory(HostingEnvironment.MapPath(notificationTestFolder)); } File.AppendAllText(string.Format("{0}/{1}.txt", HostingEnvironment.MapPath(notificationTestFolder), topic.Id), string.Format("To: {0}{3}Subject: {1}{3}Body: {3}{2}", member.GetPropertyValue <string>("Email"), subject, body, Environment.NewLine)); using (var mailMessage = new MailMessage()) { mailMessage.Subject = subject; mailMessage.Body = body; mailMessage.To.Add(member.GetPropertyValue <string>("Email")); mailMessage.From = fromMailAddress; smtpClient.Send(mailMessage); } } } } using (var db = ApplicationContext.Current.DatabaseContext.Database) { var sql = new Sql("UPDATE forumTopics SET markAsSolutionReminderSent = 1 WHERE id = @topicId", new { topicId = topic.Id }); var result = db.Execute(sql); } } catch (Exception ex) { LogHelper.Error <MarkAsSolutionReminder>("Error processing notification", ex); throw; } }
public void ScheduleTopics() { using (var db = ApplicationContext.Current.DatabaseContext.Database) { var sql = new Sql("SELECT id, memberId FROM forumTopics WHERE answer = 0 AND (markAsSolutionReminderSent IS NULL OR markAsSolutionReminderSent = 0) AND replies > 0 AND updated < getdate() - 7 AND created > '2016-10-01 00:00:00' AND id NOT IN (SELECT topicId FROM notificationMarkAsSolution) ORDER BY created DESC"); var results = db.Query <ReminderTopic>(sql); var reminder = new MarkAsSolutionReminder(); var notification = new NotificationMail(); var reminderMail = notification.GetNotificationMail("MarkAsSolutionReminderSingle"); foreach (var reminderTopic in results) { var jobId = BackgroundJob.Schedule(() => reminder.SendNotification(reminderTopic.Id, reminderTopic.MemberId, reminderMail), TimeSpan.FromMinutes(10)); } } }