public ActionResult SendMessage(OutgoingMessageViewModel msg)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    // The To list can be separated by comma or semicolon
                    string[] toList = msg.Recipient.Split(MessageHelper.SupportedSeparators, StringSplitOptions.RemoveEmptyEntries);      
                    string groupId = MessageHelper.GenerateUniqueIdentifier();

                    GatewayConfig gwConfig = repository.First<GatewayConfig>(gc => gc.Id == msg.Channel);

                    repository.UnitOfWork.BeginTransaction();
                    foreach (string to in toList)
                    {
                        OutgoingMessage message = new OutgoingMessage();
                        message.Id = MessageHelper.GenerateUniqueIdentifier();
                        message.GatewayId = msg.Channel;
                        message.Recipient = to.Trim();
                        if (gwConfig != null)
                        {
                            message.Originator = gwConfig.OwnNumber;
                        }
                        else
                        {
                            message.Originator = string.Empty;
                        }
                        message.MessageType = msg.MessageType;
                        message.MessageFormat = msg.MessageFormat;
                        message.LastUpdate = DateTime.Now;
                        message.CreateDate = message.LastUpdate;
                        message.SrcPort = Convert.ToInt32(msg.SrcPort);
                        message.DestPort = Convert.ToInt32(msg.DestPort);
                        message.Status = "Pending";
                        message.MessageClass = msg.MessageClass;
                        message.Priority = msg.Priority;
                        message.StatusReport = msg.StatusReport;
                        message.Quantity = 1;
                        message.GroupId = groupId;
                        message.ScheduledDate = msg.ScheduledDate;
                        message.Message = msg.Message;
                        
                        if (msg.MessageType.Equals("WAP Push", StringComparison.OrdinalIgnoreCase))
                        {
                            message.WapUrl = msg.WapUrl;
                            message.WapSignal = msg.WapSignal;
                            message.WapCreateDate = msg.WapCreateDate;
                            message.WapExpiryDate = msg.WapExpiryDate;
                        }
                        repository.Add<OutgoingMessage>(message);
                    }
                    repository.UnitOfWork.CommitTransaction();                    
                    return RedirectToAction("Index");
                }
            }
            catch (DataException)
            {
                if (repository.UnitOfWork.IsInTransaction)
                {
                    // Rollback the transaction
                    repository.UnitOfWork.RollBackTransaction();
                }

                // Log the error (add a variable name after DataException)
                ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
            }

            return View(msg);
        }
 /// <summary>
 /// Sends the message.
 /// </summary>
 /// <returns></returns>
 public ActionResult SendMessage()
 {
     OutgoingMessageViewModel msg = new OutgoingMessageViewModel();            
     return View(msg);
 }