/// <summary> /// Sends the email to recipients. /// </summary> /// <param name="site">The site.</param> /// <param name="documents">The updated documents.</param> public void SendEmailToRecipients(string site, int batchId) { try { List <Subscription> subscribers = SubscriptionService.GetCurrentSubscribers(site); if (null == subscribers || subscribers.Count == 0) { log.Info("No Subscribers - email will not be sent"); return; } string emailTemplateURL = null; string emailSubject = null; using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { SiteConfiguration siteConfiguration = dataContext.SiteConfigurations.FirstOrDefault(siteName => siteName.site == site); emailTemplateURL = siteConfiguration.emailTemplateURL; emailSubject = siteConfiguration.siteName + " " + (ConfigurationManager.AppSettings["Email.Subject"] as String); } string messageBody = GetEmailBody(emailTemplateURL + "?batchId=" + batchId); SmartThreadPool smartThreadPool = new SmartThreadPool(); IWorkItemsGroup workItemsGroup = smartThreadPool.CreateWorkItemsGroup(maxThreads); foreach (Subscription subscription in subscribers) { Email email = new Email { to = subscription.email, from = FromAddress, body = messageBody, subject = emailSubject }; PostExecuteWorkItemCallback afterEmailSend = delegate { SaveSentMailInformation(site, batchId, email); }; WorkItemInfo workItem = new WorkItemInfo(); workItem.PostExecuteWorkItemCallback = afterEmailSend; workItemsGroup.QueueWorkItem(workItem, SendMail, email); } workItemsGroup.WaitForIdle(); smartThreadPool.Shutdown(); using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { Batch batch = dataContext.Batches.FirstOrDefault(b => b.site == site && b.batchId == batchId); batch.finishDate = DateTime.Now; batch.status = "Successful"; dataContext.SubmitChanges(); } } catch (Exception e) { log.Error("Unable to Send Email", e); using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext()) { Batch batch = dataContext.Batches.FirstOrDefault(b => b.site == site && b.batchId == batchId); batch.status = "Unsuccessful"; dataContext.SubmitChanges(); } throw e; } }
partial void UpdateBatch(Batch instance);
partial void DeleteBatch(Batch instance);
partial void InsertBatch(Batch instance);