public static void ProductAvailable(string productName, string[] emails)
        {
            var emailMessage = StaticContents.GetContentByName("ProductAvailable");

            if (emailMessage != null && emailMessage != "نا مشخص")
            {
                emailMessage = emailMessage.Replace("{{Name}}", productName);

                List <EmailSend> list = new List <EmailSend>();

                foreach (var item in emails)
                {
                    var emailSend = new EmailSend
                    {
                        EmailSendStatus = EmailSendStatus.NotChecked,
                        FromID          = SaleMail.ID,
                        LastUpdate      = DateTime.Now,
                        Priority        = Priority.Medium,
                        Subject         = "دعوت به خرید",
                        Text            = emailMessage,
                        To = item
                    };

                    list.Add(emailSend);
                }

                EmailSends.InsertGroup(list);
            }
        }
        public JsonResult Get(int pageIndex, int pageSize, string pageOrder)
        {
            var list = EmailSends.Get(pageIndex, pageSize, pageOrder);

            int total     = EmailSends.Count();
            int totalPage = (int)Math.Ceiling((decimal)total / pageSize);

            if (pageSize > total)
            {
                pageSize = total;
            }

            if (list.Count < pageSize)
            {
                pageSize = list.Count;
            }

            JsonResult result = new JsonResult()
            {
                Data = new
                {
                    TotalPages = totalPage,
                    PageIndex  = pageIndex,
                    PageSize   = pageSize,
                    Rows       = list
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };

            return(result);
        }
        public ActionResult Edit(EmailSend email, string emailsList)
        {
            try
            {
                email.LastUpdate = DateTime.Now;

                ViewBag.Success = true;

                if (email.ID == -1)
                {
                    var list = new List <EmailSend>();

                    var emails = emailsList.Split('\n');

                    foreach (var item in emails)
                    {
                        if (!String.IsNullOrWhiteSpace(item))
                        {
                            var emailSend = new EmailSend
                            {
                                FromID          = email.FromID,
                                To              = item,
                                EmailSendStatus = email.EmailSendStatus,
                                Priority        = email.Priority,
                                LastUpdate      = DateTime.Now,
                                Subject         = email.Subject,
                                Text            = email.Text
                            };

                            list.Add(emailSend);
                        }
                    }

                    EmailSends.InsertGroup(list);
                    email = new EmailSend();
                }
                else
                {
                    EmailSends.Update(email);
                    email.Text = HttpUtility.HtmlDecode(email.Text);
                }
            }
            catch (Exception ex)
            {
                SetErrors(ex);
            }

            return(ClearView(email));
        }
        public ActionResult Edit(int?id)
        {
            EmailSend email;

            if (id.HasValue)
            {
                email      = EmailSends.GetByID(id.Value);
                email.Text = HttpUtility.HtmlDecode(email.Text);
            }
            else
            {
                email = new EmailSend();
            }

            return(View(email));
        }
        public JsonResult Delete(int id)
        {
            var jsonSuccessResult = new JsonSuccessResult();

            try
            {
                EmailSends.Delete(id);
                jsonSuccessResult.Success = true;
            }
            catch (Exception ex)
            {
                jsonSuccessResult.Errors  = new string[] { ex.Message };
                jsonSuccessResult.Success = false;
            }

            return(new JsonResult()
            {
                Data = jsonSuccessResult
            });
        }
        public static void SendEmails(int count)
        {
            var emailSends = EmailSends.GetEmailsList(count);

            foreach (var item in emailSends)
            {
                #region Before Send

                var notCheckeds = new EmailSend();
                notCheckeds.ID              = item.ID;
                notCheckeds.LastUpdate      = DateTime.Now;
                notCheckeds.EmailSendStatus = EmailSendStatus.Sending;
                EmailSends.UpdateStatus(notCheckeds);

                #endregion Before Send

                var status = SendEmail(item.From, item.To, item.Subject, item.Text, null);

                #region After Send

                var afterSend = new EmailSend();
                afterSend.ID         = item.ID;
                afterSend.LastUpdate = DateTime.Now;
                if (status == EmailStatus.Failed)
                {
                    afterSend.EmailSendStatus = EmailSendStatus.Failed;
                }
                else
                {
                    afterSend.EmailSendStatus = EmailSendStatus.Sent;
                }
                EmailSends.UpdateStatus(afterSend);

                #endregion After Send
            }
        }