/// <summary>
    /// This function send the sms for the contacts
    /// </summary>
    /// <param name="smsRequest"></param>
    /// <returns></returns>
    // ReSharper disable once FunctionComplexityOverflow
    public PostSmsResponse SendContactSms(PostSmsRequest smsRequest)
    {
        // init the response object
        var response = new PostSmsResponse { IsSuccess = true, ErrorContacts = new List<EmailSmsError>() };

        try
        {
            if (smsRequest.RawMessage != null)
            {
                // removes the htnk from the message and replace the brs with new lines.
                smsRequest.RawMessage = RemoveHtml(smsRequest.RawMessage);
                // Get the sms body by replacing the tags
                Job job = null;
                User cUser = null;
                // Retrieve objects if an id passed
                if (smsRequest.JobId > 0)
                    job = new Jobs().GetJob(smsRequest.JobId);
                //Get the user object
                if (smsRequest.UserId > 0)
                    cUser = new Users().GetUser(smsRequest.UserId);


                // Get contacts for the sent ids 
                var clientContacts = new ClientsAndContacts().GetClientContactsForEmailAndSms(smsRequest.SelectedIds, "");

                //iterate through the ontacts and send the sms
                foreach (var obj in clientContacts)
                {
                    var contact = obj.ClientOrContact as Candidate;
                    if (contact != null)
                    {
                        EmailSmsError errorContact = null;
                        var message = smsRequest.RawMessage;
                        if (!string.IsNullOrEmpty(contact.Mobile))
                        {
                            // get the original message
                            message = new Utils().GetOriginalBody(smsRequest.RawMessage, contact.CandidateId, smsRequest.JobId,
                                smsRequest.UserId, 0, 0, contact, job, cUser);
                            // send sms
                            try
                            {
                                var responseFromServer = SendSms(new Sms { Message = message, Recipient = contact.Mobile, Sender = smsRequest.Sender });
                                if (!responseFromServer.Contains("From"))
                                {
                                    //error from SMS Api server
                                    errorContact = new EmailSmsError { ContactId = contact.CandidateId, Error = responseFromServer };
                                    response.ErrorContacts.Add(errorContact);
                                }
                            }
                            catch (Exception ex)
                            {
                                //error sending the sms 
                                errorContact = new EmailSmsError { ContactId = contact.CandidateId, Error = ex.ToString() };
                                response.ErrorContacts.Add(errorContact);
                            }
                        }
                        else
                        {
                            errorContact = new EmailSmsError { ContactId = contact.CandidateId, Error = "Mobile Number not found for the contact!" };
                            response.ErrorContacts.Add(errorContact);
                        }
                        // On success save sms into the database
                        var context = new dbDataContext();
                        var objSms = new tbl_CandidateSm()
                        {
                            Message = message,
                            Sender = smsRequest.Sender,
                            CandidateId = contact.CandidateId,
                            ClientUserId = smsRequest.UserId,
                            SentDate = DateTime.Now,
                            RawMessage = smsRequest.RawMessage,
                            Error = errorContact != null ? errorContact.Error : "",
                            IsError = errorContact != null,
                            MobileNubmer = contact.Mobile
                        };
                        context.tbl_CandidateSms.InsertOnSubmit(objSms);
                        context.SubmitChanges();
                        // Add History 
                        if (errorContact == null)
                        {
                            new Histories().AddHistory(new History
                            {
                                RefId = contact.CandidateId,
                                RefType = "Contact",
                                ClientUserId = smsRequest.UserId,
                                TypeId = 10,
                                SubRefType = "Sms",
                                SubRefId = objSms.SmsId
                            });
                        }
                    }
                }
            }
        }
        catch (Exception e)
        {
            response.IsSuccess = false;
            response.Error = e.ToString();
        }
        return response;
    }
 partial void Updatetbl_CandidateSm(tbl_CandidateSm instance);
 partial void Deletetbl_CandidateSm(tbl_CandidateSm instance);
 partial void Inserttbl_CandidateSm(tbl_CandidateSm instance);