public PartialViewResult SendSMS(SMS sms) { if (ModelState.IsValid) { try { bool isSuccess = false; string errMsg = null; SmsService _smsService = new SmsService(); string response = _smsService.Send(sms); string code = _smsService.GetResponseMessage(response, out isSuccess, out errMsg); if (!isSuccess) { ModelState.AddModelError("", errMsg); ViewBag.Message = "Error"; } else { ViewBag.Message = "Message was successfully sent."; } } catch (Exception ex) { ModelState.AddModelError("", ex.Message); } } return PartialView(sms); }
public string Send(SMS sms) { string sessionId = GetSessionId(); //Get the session id string smsUrl = _config.SmsUrl; //Get the sms gatway url from the config file //Form the command for sending message. You can download the API documentation for full list of commands //from http://smslive247.com string smsCmd = String.Format("?cmd=sendmsg&sessionid={0}&message={1}&sender={2}" + "&sendto={3}&msgtype=0", sessionId, sms.Message, sms.SenderId, sms.Numbers); bool isSuccess = false; string errMsg = null; //Send sms message string response = makeHttpRequest(smsUrl + smsCmd); //Process the response from the gateway string code = GetResponseMessage(response, out isSuccess, out errMsg); //401 error code indicate invalid Session ID. If the session id is not valid, then delete it from cache and make a //request to get a new session id from the sms gateway if (code == "401") { _cache.Remove(sessionId_cahe_key);//delete the session id from the cache sessionId = GetSessionId(); //Get the session id smsCmd = String.Format("?cmd=sendmsg&sessionid={0}&message={1}&sender={2}" + "&sendto={3}&msgtype=0", sessionId, sms.Message, sms.SenderId, sms.Numbers); return makeHttpRequest(smsUrl + smsCmd);//resend the sms to the gateway } return response; }