C# - Send SMS

By , 10/25/2012
(0 ratings)
The provider Nexmo provides a REST based API that can be used to send text messages worldwide.
Sending an SMS currently costs 5.5 euro-cents in Germany and 0.7 dollar-cents in the United States. Anyone wishing to use the service can register for free and receives a starting balance to give the service a try.

Following code shows how to use the REST API. Error handling etc. is not implemented.
The code uses the JavaScriptSerializer class, which requires .NET Framework 4.5!

This class can be called as follows:
SmsResponse response = new SmsSender().SendSMS("491725555555", "Your Name", "USERNAME", "PASSWORD", "Test SMS");


The SmsResponse - the object that the method SendSMS returns - contains:

- The price the sent SMS costs
- A status (0 = sent successful)
- The message ID
- Your remaining credit at Nexmo


Provider-URL: http://www.nexmo.com

Author: Jan Welker, translation by Michael List
Translate to VB
using System.Collections.Generic;
using System.Net;
using System.Web.Script.Serialization;

namespace SMS
{
    class SmsSender
    {
        public SmsResponse SendSMS(string number, string from, string username, string pasword, string text)
        {
            string uri = string.Format("http://rest.nexmo.com/sms/json?username={0}&password={1}&from={2}&to={3}&text={4}", username, pasword, from, number, text);
            var json = new WebClient().DownloadString(uri);
            return ParseSmsResponseJson(json);
        }

        private SmsResponse ParseSmsResponseJson(string json)
        {
            // hyphens are not allowed in in .NET var names
            json = json.Replace("message-count", "MessageCount");
            json = json.Replace("message-price", "MessagePrice");
            json = json.Replace("message-id", "MessageId");
            json = json.Replace("remaining-balance", "RemainingBalance");
            return new JavaScriptSerializer().Deserialize<SmsResponse>(json);
        }
    }

    public class Message
    {
        public string To { get; set; }
        public string Messageprice { get; set; }
        public string Status { get; set; }
        public string MessageId { get; set; }
        public string RemainingBalance { get; set; }
    }

    public class SmsResponse
    {
        public string Messagecount { get; set; }
        public List<Message> Messages { get; set; }
    }
}
Tagged with SMS.

2 Comments

By markusr, 6/13/2013
the above uri appears to be incorrect.

http://rest.nexmo.com/sms/json?api_key={0}&api_secret={1}&from={2}&to={3}&text={4}

By csophatna, 8/26/2016
After i followed your instruction when it not work. it show message error like this :
- Statement error:
var json = new WebClient().DownloadString(uri);

- A first chance exception of type 'System.Net.WebException' occurred in System.dll
- Additional information: The remote server returned an error: (404) Not Found".
Not: i'm already add reference "System.Web" but it still not work.
Please kindly to help me.
 

Log in, to comment!