ASP.NET: Send SMS using asp.net MVC using ViaNett

Standard

Sending SMS from asp.net mvc application using ViaNett service is pretty easy.

Benefits

  • 15 years’ experience
  • 13 APIs for easy integration
  • SMS wholesale worldwide
  • Dispatch of about 700,000 SMS per day
  • Online monitoring of all events worldwide 24/7
  • Business customers in more than 100 countries

Get Free Trial Account:

  • Free Registration
  • Send 5 SMS free of charge
  • Fill form at Sign up for free
  • Create trial account signing in to website using Email and Password.

Snippet:

        public String TestViaNett(SmsModel obj)
        {
            const string url = "http://smsc.vianett.no/v3/cpa/cpawebservice.asmx";
            const string action = "http://smsc.vianett.no/CPA/SendSMS_Simple1";
 
            XmlDocument soapEnvelopeXml = CreateSoapEnvelopeForViaNett(obj);
            HttpWebRequest webRequest = CreateWebRequest(url, action);
 
            InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);
 
            string result;
            using (WebResponse response = webRequest.GetResponse())
            {
                using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                {
                    result = rd.ReadToEnd();
                }
            }
            return result;
        }
 
        private static HttpWebRequest CreateWebRequest(string url, string action)
        {
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.Headers.Add("SOAPAction", action);
            webRequest.ContentType = "text/xml;charset=\"utf-8\"";
            webRequest.Accept = "text/xml";
            webRequest.Method = "POST";
            return webRequest;
        }
 
        private static XmlDocument CreateSoapEnvelopeForViaNett(SmsModel obj)
        {
            XmlDocument soapEnvelopeXml = new XmlDocument();
            soapEnvelopeXml.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?>
                    <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
                    <soap:Body>
                    <SendSMS_Simple1 xmlns=""http://smsc.vianett.no/CPA/"">
                    <UserName>username</UserName>
                    <PassWord>password</PassWord>
                    <Tel>" + obj.Reciever + "</Tel>" +
                    "<Msg>" + obj.Message + "</Msg>" +
                    "</SendSMS_Simple1>" +
                    "</soap:Body>" +
                    "</soap:Envelope>");
            return soapEnvelopeXml;
        }
 
        private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s