コード例 #1
0
ファイル: EmailService.cs プロジェクト: zdavid99/wstest
        private static void LogException(string site, int batchId, Email email, Exception e)
        {
            log.Error("Failed to insert the sent info item for: " + email.to, e);
            using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext())
            {
                SentInfo sentTo = dataContext.SentInfos.FirstOrDefault(sent => sent.batchId == batchId &&
                                                                       sent.subscriptionEmail == email.to &&
                                                                       sent.site == site);

                if (sentTo == null)
                {
                    sentTo = new SentInfo
                    {
                        batchId           = batchId,
                        lastSendDate      = DateTime.Now,
                        site              = site,
                        status            = "failed",
                        subscriptionEmail = email.to
                    };
                    dataContext.SentInfos.InsertOnSubmit(sentTo);
                }
                else
                {
                    sentTo.status = "failed";
                }

                dataContext.SubmitChanges();
            }
        }
コード例 #2
0
 /// <summary>
 /// Unsubscribes the specified email from the specified site's notifications.
 /// </summary>
 /// <param name="email">The email.</param>
 /// <param name="site">The site.</param>
 /// <returns></returns>
 public static Boolean Unsubscribe(string email, string site)
 {
     try
     {
         using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext())
         {
             Subscription unSubscription = dataContext.Subscriptions.FirstOrDefault(sub => sub.email == email && sub.site == site);
             if (unSubscription != null)
             {
                 unSubscription.optedIn = false;
                 dataContext.SubmitChanges();
             }
             else
             {
                 return(false);
             }
         }
     }
     catch (Exception e)
     {
         log.Error("Not able to unsubscribe " + email, e);
         return(false);
     }
     return(true);
 }
コード例 #3
0
        private Batch CreateBatchForItems(List <UpdatedDoc> itemsForBatch, DocumentProcessOptions options)
        {
            //TODO: Transaction!
            using (NotificationTablesDataContext notificationDataContext = new NotificationTablesDataContext())
            {
                Batch batch = new Batch();
                batch.startDate = options.StartDate;
                batch.endDate   = options.EndDate;
                batch.site      = options.Site;

                notificationDataContext.Batches.InsertOnSubmit(batch);
                notificationDataContext.SubmitChanges();

                // Add the updated docs for this batch
                foreach (UpdatedDoc docToAddToBatch in itemsForBatch)
                {
                    //TODO: why not use UpdatedDocument here instead of Updated Doc
                    notificationDataContext.UpdatedDocuments.InsertOnSubmit
                    (
                        new UpdatedDocument
                    {
                        batch        = batch.batchId,
                        dateUpdated  = docToAddToBatch.dateUpdated,
                        documentType = docToAddToBatch.documentType,
                        documentName = docToAddToBatch.documentName,
                        fileSize     = docToAddToBatch.fileSize,
                        url          = docToAddToBatch.url
                    }
                    );
                }
                ;
                batch.finishDate = DateTime.Now;
                notificationDataContext.SubmitChanges();
                return(batch);
            }
        }
コード例 #4
0
ファイル: EmailService.cs プロジェクト: zdavid99/wstest
        /// <summary>
        /// Resends the email.
        /// </summary>
        /// <param name="site">The site.</param>
        /// <param name="email">The email.</param>
        /// <param name="batchId">The batch id.</param>
        /// <returns>true if successful / false if not</returns>
        public Boolean ResendEmail(string site, string email, int batchId)
        {
            try
            {
                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);

                EmailSender.Instance.SendEmail(email, FromAddress, messageBody, emailSubject, false, true);

                using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext())
                {
                    SentInfo sentInfo = dataContext.SentInfos.FirstOrDefault(info => info.site == site && info.subscriptionEmail == email && info.batchId == batchId);

                    sentInfo.lastSendDate = DateTime.Now;
                    sentInfo.status       = "resent";

                    dataContext.SubmitChanges();
                }

                return(true);
            }
            catch (Exception e)
            {
                log.Error("Unable to resend email from batch " + batchId + " for site " + site + " to email " + email, e);

                using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext())
                {
                    SentInfo sentInfo = dataContext.SentInfos.FirstOrDefault(info => info.site == site && info.subscriptionEmail == email && info.batchId == batchId);

                    sentInfo.status = "resend failed";

                    dataContext.SubmitChanges();
                }

                return(false);
            }
        }
コード例 #5
0
        /// <summary>
        /// Subscribes the specified email from the specified site's notifications.
        /// </summary>
        /// <param name="email">The email.</param>
        /// <param name="site">The site.</param>
        /// <param name="firstName">The first name.</param>
        /// <param name="lastName">The last name.</param>
        /// <returns>
        /// true if subscribed / false if errors occured
        /// </returns>
        public static Boolean Subscribe(string email, string site, string firstName, string lastName)
        {
            try
            {
                using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext())
                {
                    Subscription newSubscription = dataContext.Subscriptions.FirstOrDefault(sub => sub.email == email && sub.site == site);
                    //Has opted in before, may be opted out or not.
                    if (newSubscription != null)
                    {
                        if (firstName != null)
                        {
                            newSubscription.firstName = firstName;
                        }
                        if (lastName != null)
                        {
                            newSubscription.lastName = lastName;
                        }

                        newSubscription.optedIn = true;
                    }
                    //Is a new Subscription
                    else
                    {
                        newSubscription           = new Subscription();
                        newSubscription.email     = email;
                        newSubscription.site      = site;
                        newSubscription.firstName = firstName;
                        newSubscription.lastName  = lastName;
                        newSubscription.optedIn   = true;

                        dataContext.Subscriptions.InsertOnSubmit(newSubscription);
                    }
                    dataContext.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                log.Error("Not able to create subscription for email: " + email, e);
                return(false);
            }
            return(true);
        }
コード例 #6
0
        ///// <summary>
        ///// Gets the previous documents list by site.
        ///// </summary>
        ///// <param name="site">The site.</param>
        ///// <returns>A list of documents that were in the last run batch</returns>
        //public List<UpdatedDoc> GetDocumentsList(string site)
        //{
        //    try
        //    {
        //        int latestBatch = GetLastBatchRun(site).batchId;
        //        return GetDocumentsList(site, latestBatch);
        //    }
        //    catch (Exception e)
        //    {
        //        throw e;
        //    }
        //}

        /// <summary>
        /// Creates the new batch.
        /// </summary>
        /// <param name="site">The site.</param>
        /// <returns>The new batch.</returns>
        private Batch CreateNewBatch(string site)
        {
            try
            {
                using (NotificationTablesDataContext notificationDataContext = new NotificationTablesDataContext())
                {
                    Batch batch = new Batch();
                    batch.startDate = DateTime.Now;
                    batch.site      = site;

                    notificationDataContext.Batches.InsertOnSubmit(batch);
                    notificationDataContext.SubmitChanges();

                    return(batch);
                }
            }
            catch (Exception e)
            {
                log.Error("Unable to create a new batch", e);
                throw e;
            }
        }
コード例 #7
0
ファイル: EmailService.cs プロジェクト: zdavid99/wstest
        private static void SaveSentMailInformation(string site, int batchId, Email email)
        {
            try
            {
                using (NotificationTablesDataContext dataContext = new NotificationTablesDataContext())
                {
                    SentInfo sentTo = new SentInfo
                    {
                        batchId           = batchId,
                        lastSendDate      = DateTime.Now,
                        site              = site,
                        status            = "sent",
                        subscriptionEmail = email.to
                    };

                    dataContext.SentInfos.InsertOnSubmit(sentTo);
                    dataContext.SubmitChanges();
                }
            }
            catch (Exception e)
            {
                LogException(site, batchId, email, e);
            }
        }
コード例 #8
0
ファイル: EmailService.cs プロジェクト: zdavid99/wstest
        /// <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;
            }
        }