public ActionResult Create(Guid to)
        {
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                var viewModel = new CreatePrivateMessageViewModel
                {
                    To = to
                };

                try
                {
                    var permissions = RoleService.GetPermissions(null, UsersRole);
                    var settings = SettingsService.GetSettings();
                    // Check if private messages are enabled
                    if (!settings.EnablePrivateMessages || LoggedOnReadOnlyUser.DisablePrivateMessages == true)
                    {
                        return Content(LocalizationService.GetResourceString("Errors.GenericMessage"));
                    }

                    // Check outbox size of logged in user
                    var senderCount = _privateMessageService.GetAllSentByUser(LoggedOnReadOnlyUser.Id).Count;
                    if (senderCount > settings.MaxPrivateMessagesPerMember)
                    {
                        return Content(LocalizationService.GetResourceString("PM.SentItemsOverCapcity"));
                    }
                    if (senderCount > (settings.MaxPrivateMessagesPerMember - SiteConstants.Instance.PrivateMessageWarningAmountLessThanAllowedSize))
                    {
                        // Send user a warning they are about to exceed 
                        var sb = new StringBuilder();
                        sb.AppendFormat("<p>{0}</p>", LocalizationService.GetResourceString("PM.AboutToExceedInboxSizeBody"));
                        var email = new Email
                        {
                            EmailTo = LoggedOnReadOnlyUser.Email,
                            NameTo = LoggedOnReadOnlyUser.UserName,
                            Subject = LocalizationService.GetResourceString("PM.AboutToExceedInboxSizeSubject")
                        };
                        email.Body = _emailService.EmailTemplate(email.NameTo, sb.ToString());
                        _emailService.SendMail(email);
                    }

                    // Set editor permissions
                    ViewBag.ImageUploadType = permissions[SiteConstants.Instance.PermissionInsertEditorImages].IsTicked ? "forumimageinsert" : "image";

                    unitOfWork.Commit();

                }
                catch (Exception ex)
                {
                    unitOfWork.Rollback();
                    LoggingService.Error(ex);
                }

                return PartialView(viewModel);
            }

        }
        public ActionResult Create(Guid? id, Guid? to)
        {
            // Check if private messages are enabled
            if (!SettingsService.GetSettings().EnablePrivateMessages || LoggedOnUser.DisablePrivateMessages == true)
            {
                return ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage"));
            }

            // Check flood control
            var lastMessage = _privateMessageService.GetLastSentPrivateMessage(LoggedOnUser.Id);
            if (lastMessage != null && DateUtils.TimeDifferenceInMinutes(DateTime.UtcNow, lastMessage.DateSent) < SettingsService.GetSettings().PrivateMessageFloodControl)
            {
                return ErrorToInbox(LocalizationService.GetResourceString("PM.SendingToQuickly"));
            }

            // Check outbox size
            var senderCount = _privateMessageService.GetAllSentByUser(LoggedOnUser.Id).Count;
            if (senderCount > SettingsService.GetSettings().MaxPrivateMessagesPerMember)
            {
                return ErrorToInbox(LocalizationService.GetResourceString("PM.SentItemsOverCapcity"));
            }

            var viewModel = new CreatePrivateMessageViewModel();

            // add the username to the to box if available
            if (to != null)
            {
                var userTo = MembershipService.GetUser((Guid)to);
                viewModel.UserToUsername = userTo.UserName;
            }

            // See if this is a reply or not
            if (id != null)
            {
                var previousMessage = _privateMessageService.Get((Guid)id);
                // Its a reply, get the details
                viewModel.UserToUsername = previousMessage.UserFrom.UserName;
                viewModel.Subject = previousMessage.Subject;
                viewModel.PreviousMessage = previousMessage.Message;
            }
            return View(viewModel);
        }
        public ActionResult Create(CreatePrivateMessageViewModel createPrivateMessageViewModel)
        {
            if (!SettingsService.GetSettings().EnablePrivateMessages || LoggedOnUser.DisablePrivateMessages == true)
            {
                return ErrorToHomePage(LocalizationService.GetResourceString("Errors.GenericMessage"));
            }
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                if (ModelState.IsValid)
                {
                    var userTo = createPrivateMessageViewModel.UserToUsername;

                    // first check they are not trying to message themself!
                    if (userTo.ToLower() != LoggedOnUser.UserName.ToLower())
                    {
                        // Map the view model to message
                        var privateMessage = new PrivateMessage
                                                 {
                                                     UserFrom = LoggedOnUser,
                                                     Subject = createPrivateMessageViewModel.Subject,
                                                     Message = createPrivateMessageViewModel.Message,
                                                 };
                        // now get the user its being sent to
                        var memberTo = MembershipService.GetUser(userTo);

                        // check the member
                        if (memberTo != null)
                        {

                            // Check in box size
                            // First check sender
                            var receiverCount = _privateMessageService.GetAllReceivedByUser(memberTo.Id).Count;
                            if (receiverCount > SettingsService.GetSettings().MaxPrivateMessagesPerMember)
                            {
                                ModelState.AddModelError(string.Empty, string.Format(LocalizationService.GetResourceString("PM.ReceivedItemsOverCapcity"), memberTo.UserName));
                            }
                            else
                            {
                                // Good to go send the message!
                                privateMessage.UserTo = memberTo;
                                _privateMessageService.Add(privateMessage);

                                try
                                {
                                    TempData[AppConstants.MessageViewBagName] = new GenericMessageViewModel
                                    {
                                        Message = LocalizationService.GetResourceString("PM.MessageSent"),
                                        MessageType = GenericMessages.success
                                    };

                                    unitOfWork.Commit();

                                    // Finally send an email to the user so they know they have a new private message
                                    // As long as they have not had notifications disabled
                                    if (memberTo.DisableEmailNotifications != true)
                                    {
                                        var email = new Email
                                        {
                                            EmailFrom = SettingsService.GetSettings().NotificationReplyEmail,
                                            EmailTo = memberTo.Email,
                                            Subject = LocalizationService.GetResourceString("PM.NewPrivateMessageSubject"),
                                            NameTo = memberTo.UserName
                                        };

                                        var sb = new StringBuilder();
                                        sb.AppendFormat("<p>{0}</p>", string.Format(LocalizationService.GetResourceString("PM.NewPrivateMessageBody"), LoggedOnUser.UserName));
                                        email.Body = _emailService.EmailTemplate(email.NameTo, sb.ToString());
                                        _emailService.SendMail(email);
                                    }

                                    return RedirectToAction("Index");
                                }
                                catch (Exception ex)
                                {
                                    unitOfWork.Rollback();
                                    LoggingService.Error(ex);
                                    ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("Errors.GenericMessage"));
                                }
                            }
                        }
                        else
                        {
                            // Error send back to user
                            ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("PM.UnableFindMember"));
                        }
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, LocalizationService.GetResourceString("PM.TalkToSelf"));
                    }
                }
                TempData[AppConstants.MessageViewBagName] = null;
                return View(createPrivateMessageViewModel);
            }
        }
        public ActionResult Create(CreatePrivateMessageViewModel createPrivateMessageViewModel)
        {
            if (!SettingsService.GetSettings().EnablePrivateMessages || LoggedOnUser.DisablePrivateMessages == true)
            {
                throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage"));
            }
            using (var unitOfWork = UnitOfWorkManager.NewUnitOfWork())
            {
                if (ModelState.IsValid)
                {
                    // Check flood control
                    var lastMessage = _privateMessageService.GetLastSentPrivateMessage(LoggedOnUser.Id);
                    if (lastMessage != null && DateUtils.TimeDifferenceInMinutes(DateTime.UtcNow, lastMessage.DateSent) < SettingsService.GetSettings().PrivateMessageFloodControl)
                    {
                        throw new Exception(LocalizationService.GetResourceString("PM.SendingToQuickly"));
                    }

                    var memberTo = MembershipService.GetUser(createPrivateMessageViewModel.To);

                    // first check they are not trying to message themself!
                    if (memberTo != null)
                    {
                        // Map the view model to message
                        var privateMessage = new PrivateMessage
                        {
                            UserFrom = LoggedOnUser,
                            Message = createPrivateMessageViewModel.Message,
                        };

                        // check the member
                        if (!String.Equals(memberTo.UserName, LoggedOnUser.UserName, StringComparison.CurrentCultureIgnoreCase))
                        {
                            // Check in box size for both
                            var receiverCount = _privateMessageService.GetAllReceivedByUser(memberTo.Id).Count;
                            if (receiverCount > SettingsService.GetSettings().MaxPrivateMessagesPerMember)
                            {
                                throw new Exception(string.Format(LocalizationService.GetResourceString("PM.ReceivedItemsOverCapcity"), memberTo.UserName));
                            }

                            // If the receiver is about to go over the allowance them let then know too
                            if (receiverCount > (SettingsService.GetSettings().MaxPrivateMessagesPerMember - SiteConstants.PrivateMessageWarningAmountLessThanAllowedSize))
                            {
                                // Send user a warning they are about to exceed
                                var sb = new StringBuilder();
                                sb.AppendFormat("<p>{0}</p>", LocalizationService.GetResourceString("PM.AboutToExceedInboxSizeBody"));
                                var email = new Email
                                {
                                    EmailTo = memberTo.Email,
                                    NameTo = memberTo.UserName,
                                    Subject = LocalizationService.GetResourceString("PM.AboutToExceedInboxSizeSubject")
                                };
                                email.Body = _emailService.EmailTemplate(email.NameTo, sb.ToString());
                                _emailService.SendMail(email);
                            }

                            // Good to go send the message!
                            privateMessage.UserTo = memberTo;
                            _privateMessageService.Add(privateMessage);

                            try
                            {
                                // Finally send an email to the user so they know they have a new private message
                                // As long as they have not had notifications disabled
                                if (memberTo.DisableEmailNotifications != true)
                                {
                                    var email = new Email
                                    {
                                        EmailTo = memberTo.Email,
                                        Subject = LocalizationService.GetResourceString("PM.NewPrivateMessageSubject"),
                                        NameTo = memberTo.UserName
                                    };

                                    var sb = new StringBuilder();
                                    sb.AppendFormat("<p>{0}</p>", string.Format(LocalizationService.GetResourceString("PM.NewPrivateMessageBody"), LoggedOnUser.UserName));
                                    email.Body = _emailService.EmailTemplate(email.NameTo, sb.ToString());
                                    _emailService.SendMail(email);
                                }

                                unitOfWork.Commit();

                                return PartialView("_PrivateMessage", privateMessage);
                            }
                            catch (Exception ex)
                            {
                                unitOfWork.Rollback();
                                LoggingService.Error(ex);
                                throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage"));
                            }
                        }
                        else
                        {
                            throw new Exception(LocalizationService.GetResourceString("PM.TalkToSelf"));
                        }
                    }
                    else
                    {
                        // Error send back to user
                        throw new Exception(LocalizationService.GetResourceString("PM.UnableFindMember"));
                    }
                }
                throw new Exception(LocalizationService.GetResourceString("Errors.GenericMessage"));
            }
        }