Пример #1
0
 public virtual void AddDistribution(Distribution distribution)
 {
     distribution.User = this;
     Distributions.Add(distribution);
 }
        private void ProcessDistribution(Distribution distribution)
        {
            var recipients = new List<Contact>(distribution.Contacts);
            foreach (var contact in distribution.Groups.SelectMany(x => x.Contacts))
            {
                if (recipients.FirstOrDefault(x => x.Id == contact.Id) == null)
                {
                    recipients.Add(contact);
                }
            }

            var smtpClient = CreateSmtpClient();
            var allMessages = new List<MailMessage>();
            foreach (var recipient in recipients)
            {
                var email = CreateMail(recipient, distribution.Template);
                allMessages.Add(email);
                try
                {
                    smtpClient.Send(email);
                }
                catch (Exception ex)
                {
                    distribution.AddDeliveryFailed(new FailedDelivery {
                        IsNew = true,
                        Subject = "Error on sending e-mail through SMTP",
                        To = recipient,
                        EmailText = string.Format("<b>{0}</b><br />{1}", ex.GetType().Name, ex.Message)
                    });
                }
            }
        }
 public JsonResult CloseDistribution(Distribution distribution)
 {
     var rds = Repository.GetById<Distribution>(distribution.Id);
     rds.IsClosed = true;
     return Json(true);
 }
        public JsonResult SubmitDistribution(Distribution distribution)
        {
            var distributionToInsert = new Distribution();
            distributionToInsert.Name = distribution.Name;
            distributionToInsert.Contacts = new List<Contact>(distribution.Contacts.Select(x => Repository.GetById<Contact>(x.Id)));
            distributionToInsert.Groups = new List<ContactsGroup>(distribution.Groups.Select(x => Repository.GetById<ContactsGroup>(x.Id)));
            distributionToInsert.Template = Repository.GetById<MailTemplate>(distribution.Template.Id);

            User.AddDistribution(distributionToInsert);
            ProcessDistribution(distributionToInsert);
            return Json(true);
        }