public void AddNewMail(NewMailBm bm, int currentUserId)
        {
            Email mail = Mapper.Map <NewMailBm, Email>(bm);

            mail.SentDate = DateTime.Now;
            User currentuser = Context.Users.Find(currentUserId);

            mail.Flag   = Flag.Sent;
            mail.Sender = currentuser;
            if (mail.Attachment != null && mail.Attachment.Length == 0)
            {
                mail.Attachment = null;
            }

            var users = bm.Users.Split(';').Select(u => u.Trim()).ToList();

            foreach (var user in users)
            {
                var username = user.Split('@')[0];
                if (Context.Users.Any(u => u.Username == username))
                {
                    var reciever = Context.Users.FirstOrDefault(u => u.Username == username);
                    mail.Recipients.Add(reciever);
                }
            }

            Context.Emails.Add(mail);
            Context.SaveChanges();
        }
        public IActionResult New(HttpSession session, HttpResponse response, NewMailBm bm)
        {
            if (!AuthenticationManager.IsUserAuthenticated(session.Id))
            {
                this.Redirect(response, "/users/login");

                return(null);
            }

            if (!this.service.NewMailBmIsValid(bm))
            {
                this.Redirect(response, "/mail/new");

                return(null);
            }

            User currentUser = AuthenticationManager.GetAuthenticatedUser(session.Id);

            if (bm.Action == "Send")
            {
                this.service.AddNewMail(bm, currentUser.Id);
            }
            else if (bm.Action == "Save")
            {
                this.service.PutMailInDraft(bm, currentUser.Id);
            }


            this.Redirect(response, "mail/sent");

            return(null);
        }
        public bool NewMailBmIsValid(NewMailBm bm)
        {
            var  users   = bm.Users.Split(';').ToList();
            bool isValid = true;

            if (users.Count == 0 || string.IsNullOrEmpty(string.Join("", users)))
            {
                ErrorBag.Errors.Add(new Error()
                {
                    Message = "Recipients are required"
                });

                isValid = false;
            }

            foreach (var user in users)
            {
                string pattern = @"^[a-zA-Z0-9\.][email protected]\s*?$";
                Regex  regex   = new Regex(pattern);

                bool isMatch = regex.IsMatch(user);
                if (!isMatch)
                {
                    ErrorBag.Errors.Add(new Error()
                    {
                        Message = "Recipients should be in format: \"[email protected]\""
                    });

                    isValid = false;
                    break;
                }
            }

            if (bm.Subject.Length < 3 || bm.Subject.Length > 50)
            {
                ErrorBag.Errors.Add(new Error()
                {
                    Message = "Subject must be between 3 and 50 symbols"
                });

                isValid = false;
            }

            if (bm.Message.Length > 300)
            {
                ErrorBag.Errors.Add(new Error()
                {
                    Message = "Message must less than 300 symbols"
                });

                isValid = false;
            }

            if (bm.Attachment.Length > 250)
            {
                ErrorBag.Errors.Add(new Error()
                {
                    Message = "Attachment must be less than 250 symbols"
                });

                isValid = false;
            }

            return(isValid);
        }