public JsonResult ImportContacts(Attachment attachment)
        {
            var text = Encoding.UTF8.GetString(attachment.Content);
            var contacts = text.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            if (contacts.Count < 2)
            {
                return Json(false);
            }

            var indexes = Indexes.IndexString(contacts[0]);
            contacts.RemoveAt(0);
            foreach (var contactText in contacts)
            {
                var parts = contactText.Split(new [] { "," }, StringSplitOptions.None);
                var contact = new Contact
                {
                    Email = parts[indexes.Email],
                    FirstName = parts[indexes.FirstName],
                    LastName = parts[indexes.LastName],
                    MiddleName = parts[indexes.MiddleName],
                    Organization = parts[indexes.Organization]
                };
                if (User.Contacts.Any(x => x.Email == contact.Email))
                {
                    continue;
                }

                var groups = parts[indexes.Grouops].Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (var groupName in groups)
                {
                    var group = User.ContactsGroups.FirstOrDefault(x => x.Name.ToLower() == groupName.ToLower());
                    if (group == null)
                    {
                        group = new ContactsGroup { Name = groupName };
                        User.AddContactsGroup(group);
                    }

                    contact.AddGroup(group);
                }

                User.AddContact(contact);
            }

            return Json(true);
        }
        private void UpdateGroups(ref Contact contact)
        {
            if (contact.Groups == null)
            {
                return;
            }

            for (int i = 0; i < contact.Groups.Count; i++)
            {
                var id = contact.Groups[i].Id;
                if (id > 0)
                {
                    contact.Groups[i] = Repository.GetById<ContactsGroup>(id);
                }
                else
                {
                    contact.Groups[i].Id = 0;
                    User.AddContactsGroup(contact.Groups[i]);
                }
            }
        }
 public JsonResult DeleteContact(Contact contact)
 {
     var temp = Repository.GetById<Contact>(contact.Id);
     Repository.Delete(temp);
     return Json(true);
 }
        public JsonResult UpdateOrCreateContact(Contact contact)
        {
            if (contact.Id < 1)
            {
                if (User.Contacts.Any(x => x.Email.ToLower() == contact.Email.ToLower()))
                {
                    return Json(false);
                }

                contact.Id = 0;
                UpdateGroups(ref contact);
                User.AddContact(contact);
            }
            else
            {
                var realContact = Repository.GetById<Contact>(contact.Id);
                realContact.Email = contact.Email;
                realContact.FirstName = contact.FirstName;
                realContact.LastName = contact.LastName;
                realContact.MiddleName = contact.MiddleName;
                realContact.Organization = contact.Organization;
                realContact.Groups.Clear();
                foreach (var item in contact.Groups)
                {
                    TryAddGroupToContact(realContact, item);
                }
            }

            return Json(true);
        }
 private void TryAddGroupToContact(Contact contact, ContactsGroup group)
 {
     if (contact.Groups.FirstOrDefault(x => x.Id == group.Id) == null)
     {
         if (group.Id < 1)
         {
             group.Id = 0;
             User.AddContactsGroup(group);
             contact.AddGroup(group);
         }
         else
         {
             var realGroup = User.ContactsGroups.First(x => x.Id == group.Id);
             contact.AddGroup(realGroup);
         }
     }
 }
Beispiel #6
0
 public virtual void AddContact(Contact contact)
 {
     contact.User = this;
     Contacts.Add(contact);
 }
 private string GetCompleteText(Contact contact, string template)
 {
     return template.Replace("†qte†", "\"")
                    .Replace("†rvn†", "=")
                    .Replace(Settings.FirstNameKeyword, contact.FirstName)
                    .Replace(Settings.LastNameKeyword, contact.LastName)
                    .Replace(Settings.MiddleNameKeyword, contact.MiddleName)
                    .Replace(Settings.OrganizationKeyword, contact.Organization)
                    .Replace(Settings.FullNameKeyword,
                             string.IsNullOrEmpty(contact.MiddleName) ?
                                 string.Format("{0} {1}", contact.LastName, contact.FirstName) :
                                 string.Format("{0} {1} {2}", contact.LastName, contact.FirstName, contact.MiddleName))
                    .Trim();
 }
        private Reply CreateReplyFromMail(System.Net.Mail.MailMessage message, Contact from)
        {
            var reply = new Reply
            {
                From = from,
                EmailText = message.Body,
                RecievedOn = DateTime.Now,
                Subject = message.Subject,
                IsNew = true
            };
            foreach (var mailAttachment in message.Attachments)
            {
                reply.AddAttachment(CreateAttachment(mailAttachment));
            }

            return reply;
        }
        private MailMessage CreateMail(Contact contact, MailTemplate template)
        {
            var message = new MailMessage();

            message.Subject = GetCompleteText(contact, template.Subject);
            message.SubjectEncoding = Encoding.UTF8;

            message.Body = GetCompleteText(contact, template.Text);
            message.BodyEncoding = Encoding.UTF8;
            message.IsBodyHtml = true;

            foreach (var attachment in template.Attachments)
            {
                var stream = new MemoryStream(attachment.Content);
                var mailAttachment = new System.Net.Mail.Attachment(stream, attachment.Name, attachment.ContentType);
                message.Attachments.Add(mailAttachment);
            }

            message.From = new MailAddress(User.Settings.EmailAddressFrom);
            message.To.Add(new MailAddress(contact.Email));
            return message;
        }
 private FailedDelivery CreateFailedDeliveryFromMail(System.Net.Mail.MailMessage message, Contact to)
 {
     return new FailedDelivery
     {
         To = to,
         EmailText = message.Body,
         Subject = message.Subject,
         IsNew = true
     };
 }