Ejemplo n.º 1
0
        private void SendUserEmails()
        {
            AspSettings   settings = AspSettings.Load();
            IConfigurator config   = Configurator.Create();

            #region trial spam
            if (settings.SendSpam)
            {
                // One day after start
                if (settings.SendSpamOneDayAfter)
                {
                    foreach (Guid cid in CManage.GetCompaniesByCategory(CompanyCategory.OneDayNoIM, settings.OneDayAfterPeriod, true))
                    {
                        try
                        {
                            string dbName = config.GetCompanyInfo(cid.ToString()).Database;
                            if (!CManage.NotificationGet(cid, EmailType.UserAfterOneDayIM))
                            {
                                CManage.NotificationUpdate(cid, EmailType.UserAfterOneDayIM, true);

                                if (!CManage.CheckImSessions(dbName))
                                {
                                    // Send e-mails to every active user
                                    TemplateVariables vars = CManage.CompanyGetVariables(cid);
                                    foreach (UserInfo ui in CManage.GetPortalUsers(dbName))
                                    {
                                        vars["Login"] = ui.Login;
                                        vars["Name"]  = ui.Name;
                                        try
                                        {
                                            CManage.SendEmail(ui.Email, EmailType.UserAfterOneDayIM, vars);
                                        }
                                        catch { }
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.WriteError(ex.ToString());
                        }
                    }
                }

                // One week after start
                if (settings.SendSpamOneWeekAfter)
                {
                    foreach (Guid cid in CManage.GetCompaniesByCategory(CompanyCategory.OneWeekAfterStart, settings.OneWeekAfterPeriod, true))
                    {
                        try
                        {
                            if (!CManage.NotificationGet(cid, EmailType.UserAfterOneWeek))
                            {
                                CManage.NotificationUpdate(cid, EmailType.UserAfterOneWeek, true);

                                // Send e-mail
                                TemplateVariables vars = CManage.CompanyGetVariables(cid);
                                if (!string.IsNullOrEmpty(vars["ContactEmail"]))
                                {
                                    CManage.SendEmail(vars["ContactEmail"], EmailType.UserAfterOneWeek, vars);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.WriteError(ex.ToString());
                        }
                    }
                }

                // One week before end
                if (settings.SendSpamOneWeekBefore)
                {
                    foreach (Guid cid in CManage.GetCompaniesByCategory(CompanyCategory.OneWeekBeforeEnd, settings.OneWeekBeforePeriod, true))
                    {
                        try
                        {
                            if (!CManage.NotificationGet(cid, EmailType.UserOneWeekBefore))
                            {
                                CManage.NotificationUpdate(cid, EmailType.UserOneWeekBefore, true);

                                // Send e-mail
                                TemplateVariables vars = CManage.CompanyGetVariables(cid);
                                if (!string.IsNullOrEmpty(vars["ContactEmail"]))
                                {
                                    CManage.SendEmail(vars["ContactEmail"], EmailType.UserOneWeekBefore, vars);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.WriteError(ex.ToString());
                        }
                    }
                }

                // One day before end
                if (settings.SendSpamOneDayBefore)
                {
                    foreach (Guid cid in CManage.GetCompaniesByCategory(CompanyCategory.OneDayBeforeEnd, settings.OneDayBeforePeriod, true))
                    {
                        try
                        {
                            if (!CManage.NotificationGet(cid, EmailType.UserOneDayBefore))
                            {
                                CManage.NotificationUpdate(cid, EmailType.UserOneDayBefore, true);

                                // Send e-mail
                                TemplateVariables vars = CManage.CompanyGetVariables(cid);
                                if (!string.IsNullOrEmpty(vars["ContactEmail"]))
                                {
                                    CManage.SendEmail(vars["ContactEmail"], EmailType.UserOneDayBefore, vars);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.WriteError(ex.ToString());
                        }
                    }
                }
            }
            #endregion

            #region billable spam
            if (settings.SendBillableSpam && settings.UseTariffs)
            {
                // 7 days before
                if (settings.SendBillableSpam7day)
                {
                    foreach (Guid cid in CManage.GetCompaniesByCategory(CompanyCategory.BillableNdaysBefore, 7, true))
                    {
                        // ensure that we send emails only once for a day
                        DateTime lastSpamDate = CManage.GetNotificationClientSpamDate(cid);
                        if (lastSpamDate > DateTime.Now.AddDays(-1))
                        {
                            continue;
                        }

                        // Send e-mail
                        try
                        {
                            TemplateVariables vars = CManage.CompanyGetVariables(cid);
                            if (!string.IsNullOrEmpty(vars["ContactEmail"]))
                            {
                                CManage.SendEmail(vars["ContactEmail"], EmailType.Client7DayZeroBalance, vars);
                                CManage.UpdateNotificationClientSpamDate(cid, DateTime.Now);
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.WriteError(ex.ToString());
                        }
                    }
                }

                // 3 days before
                if (settings.SendBillableSpam3day)
                {
                    foreach (Guid cid in CManage.GetCompaniesByCategory(CompanyCategory.BillableNdaysBefore, 3, true))
                    {
                        // ensure that we send emails only once for a day
                        DateTime lastSpamDate = CManage.GetNotificationClientSpamDate(cid);
                        if (lastSpamDate > DateTime.Now.AddDays(-1))
                        {
                            continue;
                        }

                        // Send e-mail
                        try
                        {
                            TemplateVariables vars = CManage.CompanyGetVariables(cid);
                            if (!string.IsNullOrEmpty(vars["ContactEmail"]))
                            {
                                CManage.SendEmail(vars["ContactEmail"], EmailType.Client3DayZeroBalance, vars);
                                CManage.UpdateNotificationClientSpamDate(cid, DateTime.Now);
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.WriteError(ex.ToString());
                        }
                    }
                }

                // 1 day before
                if (settings.SendBillableSpam1day)
                {
                    foreach (Guid cid in CManage.GetCompaniesByCategory(CompanyCategory.BillableNdaysBefore, 1, true))
                    {
                        // ensure that we send emails only once for a day
                        DateTime lastSpamDate = CManage.GetNotificationClientSpamDate(cid);
                        if (lastSpamDate > DateTime.Now.AddDays(-1))
                        {
                            continue;
                        }

                        // Send e-mail
                        try
                        {
                            TemplateVariables vars = CManage.CompanyGetVariables(cid);
                            if (!string.IsNullOrEmpty(vars["ContactEmail"]))
                            {
                                CManage.SendEmail(vars["ContactEmail"], EmailType.Client1DayZeroBalance, vars);
                                CManage.UpdateNotificationClientSpamDate(cid, DateTime.Now);
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.WriteError(ex.ToString());
                        }
                    }
                }

                // Negative balance
                if (settings.SendBillableSpamNegativeBalance)
                {
                    foreach (Guid cid in CManage.GetCompaniesByCategory(CompanyCategory.BillableNegativeBalance, 0, true))
                    {
                        // ensure that we send emails only once for a day
                        DateTime lastSpamDate = CManage.GetNotificationClientSpamDate(cid);
                        if (lastSpamDate > DateTime.Now.AddDays(-1))
                        {
                            continue;
                        }

                        // Send e-mail
                        try
                        {
                            TemplateVariables vars = CManage.CompanyGetVariables(cid);
                            if (!string.IsNullOrEmpty(vars["ContactEmail"]))
                            {
                                CManage.SendEmail(vars["ContactEmail"], EmailType.ClientZeroBalance, vars);
                                CManage.UpdateNotificationClientSpamDate(cid, DateTime.Now);
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.WriteError(ex.ToString());
                        }
                    }
                }
            }
            #endregion
        }