dotnet-Snippets.com
Add Snippet
Snippets: 137 | Registered User: 138
Main Menu

Home
Random Snippet
Contact Us
Imprint
RSS Feeds

All languages
C#
VB.NET
C++
ASP.NET
Google Ads
Send SMS

Author: Michael List
Programming Language: C# Rating:
not yet rated

Views: 2557

Description:

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




C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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; }
    }
}

This Snippets could be interesting for you:

Poor Excellent
1 2 3 4 5 6 7 8 9 10
Sign in to vote for this snippet.

Comments:
(Please log in to write an comment.)