dotMobimobiThinkingmobiForgemobiReadyDeviceAtlasgoMobi
background image

Sending SMS from Apps and Webapps

Old mail box outside house
Posted by mokil - 10 Aug 2010
Twitter share icon Facebook share icon Google Plus share icon

It’s easy to send SMS messages from any Web site or application, but it’s surprising how few sites/applications actually do this. Integrating SMS into your system gives it something extra, and allows your system to enhance the mobile experience for the user. In this article, we’ll look at how an SMS message can be sent from any system, and at a few providers who offer this functionality.

Choosing an SMS Provider

There are essentially three types of SMS provider:

  • Mobile Network (e.g. Vodafone, Three)
  • Aggregator
  • SMS Messaging Company

Mobile Network

To send messages through a mobile network, you need to set up a Short Message Service Centre (SMSC). These are being replaced with SMS routers, which are more efficient. It’s very unlikely you’ll set up an account directly with a network, as the costs are prohibitive. You need to be sending a few million messages per day before a network will even consider talking to you.

Aggregator

Think of an aggregator as a go-between, sitting between you and the network. Aggregators set up an SMSC with one or more networks and allow users that want to send SMS messages to send those messages through their connection. By gathering up a large number of small users, the message numbers are aggregated (you can see where the name comes from), giving the aggregator enough messages per day to maintain their SMSC connection with the network.
Depending upon the aggregator you pick, costs will vary. Generally you’ll pay anywhere between 2p – 7p per message, and maybe a monthly fee. The API you’ll use will involve either a SOAP Web service, an XML-RPC Web service, or HTTP POST/GET calls.
There are lots of aggregators, here’s a sample:

  • Esendex
  • 2ergo
  • iTagg (supports MMS)
  • Tanla
  • Reach Data

SMS Messaging Company

SMS messaging companies are third party companies who publish an API that allows you to send SMS messages. To all intents and purposes these companies are much the same as an aggregator. Examples of such companies are txtlocal and txtnation. These companies often offer installable software which allows you to upload lists of mobile numbers to which SMS messages can be sent.
When it comes to choosing a provider, you need to think about what you are trying to do. Some common requirements to consider are:

  • Do I need to send SMS messages?
  • Do I need to send bulk SMS messages?
  • Do I need to send MMS messages?
  • Do I need to receive messages?
  • Do I need to send long messages (messages greater than 160 characters in length)
  • How much will each message cost me?

If you only aim to send SMS messages, you can choose pretty much any provider you want. If you want to send SMS messages via your site or application, you’ll need to choose a provider with an API.
Remember that you’ll be charged for each message sent (2p to 7p). You can either pre-pay or arrange a monthly direct debit with most providers; it depends on how many messages you think you’re likely to send.

Adding SMS Functionality to Your System.

As you have a mobile system, it’s a natural progression to add mobile-related technologies to it. It enhances the mobile experience and allows the user to better interact with the system. The BBC Mobile site, for example, displays a small blue SMS icon at the bottom of each article. Clicking on this displays a screen that allows you to send an SMS to a friend:


Figure 1: BBC mobile site SMS interface

It’s this kind of use that can really take your mobile system to another level.
To show you how to send an SMS message, I’m going to use the iTagg API. The first thing to do once you’ve selected an SMS provider is to download or request their API documentation (you can download the iTagg API documentation from here). The level of implementation difficulty changes from API to API, but none of them are particularly challenging for a competent developer. Most APIs also offer error codes, so you’re informed if something goes wrong when sending a message.

Once you have the API document, request a trial. This will allow you to send some SMS messages. You’ll be given a user name and password, which you’ll need to follow the example below.
The iTagg API supports a HTTP POST-based mechanism. Here are the parameters we need to specify:

  • Usr - The user name for your iTagg account.
  • password - The password for your iTagg account.
  • from - The number or alias you want the message to appear to have been sent from. If you specify a text-based alias, you can specify up to 11 characters. If you specify a valid UK mobile number you can specify up to 12 characters, as a mobile number with a country code is 12 characters long. Note: if you intend to allow users to reply to SMS messages, you must specify a mobile number.
  • to - The mobile number you want to send the message to.
  • Txt - The message body you want to send. Remember this can only be up to 160 characters in length. You can send longer messages using contatenation if you require – but not all the provider APIs support this.
  • type - Although I’ve only discussed SMS messages so far, it’s usually also possible to send binary or WAP Push messages. The iTagg API allows you to specify the message type to send using this parameter.
  • send - This optional parameter allows you to specify a date and time to send the message, so you can schedule it. If no scheduled date/time is specified, the message is sent immediately.
  • route - Some providers ask you to specify a route – iTagg is one such provider. This often determines how fast the message will arrive once you’ve sent it, and how much you will be charged for sending the message.

We’ll create a simple Web site using Visual Studio 2008 to show how to send a message.
1. Open Visual Studio and create a new ASP.NET Web site.
2. Open the Default.aspx page in design view and add the following controls:

  • A label called lblTo, with the text value set to To:
  • A text box called txtTo
  • A label called lblFrom, with the text value set to From:
  • A text box called txtFrom
  • A label called lblBody, with the text value set to SMS Message:
  • A text box called txtBody, with the TextMode set to MultiLine
  • A button called btnSend, with the text value set to Send
  • A label called lblResult, with the text value set to nothing (i.e. an empty string)

Once you’ve done this, rearrange the controls so they are displayed on individual lines, as in the image below.


Figure 2: Visual Studio - form controls layout

3. Now we can add some code to actually send an SMS message. Bear in mind that, for easier demonstration, I’m going to hard-code everything into the btnSend_Click event. In reality, things like the user name and password would be stored in the web.config file.

Double-click the Send button to create the button’s click event handler:


Figure 3: Visual Studio - send button event handler code

4. Enter the following code (make sure you replace username and password with the appropriate values):

    protected void btnSend_Click(object sender, EventArgs e)
    {
        // Initialise the Web request
        System.Net.WebClient webClient = new System.Net.WebClient();
 
        // Prepare the HTTP POST data
        // Change the user name as appropriate
        // Change the password as appropriate
        // Type is set to text for SMS - could also be set to binary
        // Route is set to 4 - this is the iTagg budget route. Other routes are 7 (for UK) and 8 (for Global)
        string postData = "usr=username&pwd=password&from= " + 
   		txtFrom.Text + "&to=" + txtTo.Text +
                          "&type=text&route=4&txt=" + txtBody.Text;
 
        webClient.Headers.Add("Content-Type", 
"application/x-www-form-urlencoded");
 
        // Make the request to iTagg
        lblResult.Text = 
webClient.UploadString("https://secure.itagg.com/smsg/sms.mes", 
postData);
    }


Figure 4: Visual Studio - send button event handler code

5. Now press F5 to run the Web site. Type in some details, ensuring you specify a valid mobile number. Then click Send. You should see a response appear below the Send button, and the message should be delivered to your phone.


Figure 5: iTagg SMS API Message Demo - browser output


Figure 6: Receiving SMS - phone display

6. It really is that easy to send an SMS message – four lines of code! But what’s the response string that was displayed after we clicked the Send button? Let’s take a closer look.

error code|error text|submission reference 0|sms submitted|2891717007c4bf0ebf2c81cb4995cd2d-3

iTagg returns a pipe-separated list of return values. The first two are self-explanatory – if an error occurs, an error code and error text are displayed. In a fully implemented solution, your program would handle the various error codes returned by iTagg and could respond appropriately. No error has occurred here, so the error code is set to 0 (denoting success), and the error text is set to sms submitted.
The final value is the submission reference. This is an identifier for the message, which can be stored in your system to identify the message. This is useful because it allows us to process delivery receipts for this message – which we’ll discuss in a follow-up article.

Summary

We’ve just shown that SMS sending capabilities can, with relatively little effort, be added to any system. There are a large number of SMS providers out there so it’s important to think about what you need before choosing one. Most of the providers offer some type of HTTP-based API, which can be quickly integrated into your chosen system.
SMS providers can do more than just send SMS messages; they can also help you receive them and can also process delivery receipts too, so you can demonstrate that an SMS message arrived at its intended target. We’ll discuss these features in a separate article – happy sending!


Posted by mokil - 10 Aug 2010

mokil's picture

Mike McQuillan began developing software professionally after graduating from university with a 1st class Honours degree in Computer Studies in 1999. Since then he has worked for small companies (five employees) and large PLCs, such as Experian. He has developed just about every type of software there is – Windows client/server, Web systems, services, Unix systems, and now mobile systems. He is the CTO at Mdev Technology, a mobile software development company, and he also runs his own consultancy firm, McQTech.
He's been working on mobile since 2006 and has built up a comprehensive knowledge of what works and what doesn’t, especially on the mobile Web. He mainly concentrates on using .NET and SQL Server but has developed systems for Apple's mobile devices, Android, and Windows Phone 7 too.

Posted by burstsms 4 years ago

Thanks Mike, good to see more detailed looks into API functionality.

Burst SMS has a really easy to use PHP SMS API, our system is based on our SMS Marketing platform so it also has list functionality and the ability to have multiple clients built right into the API.

Brad Down
Head of Innovation
Burst SMS
http://www.burstsms.com

Brad Down Head of Innovation Burst SMS http://www.burstsms.com

Posted by mobidick700 4 years ago

Hi, interesting and very informative! Would it be possible to do the same thing, but let the sms send from provider/operator of the user of the mobile phone? (I am totally new to mobile design so don't blame me if this is a stupid question!) And how is this done?
Greetings David

Posted by mokil 4 years ago

Hi David

Unfortunately you can't do this (easily). Some app programming environments let you do this but generally SMS functionality etc is sandboxed. There's no way you can send an SMS to my knowledge directly from the handset itself via a Web site.

Posted by pbsjr 3 years ago

I have written a SMS Windows form application which send a SMS message from user's desktop directly to receipient's phone. NO CHARGE. Sender needs to know desitnation carrier and cell number. I provide a lookup for carriers from which to choose.

The message and contact info is saved in an embedded SQL Server Compact database for later reference. The sender receives a confirming email containing a copy of message.

I use a smtp account of mine on host server. The user and password are encrypted.

I am willing to share code with interested developers.

Posted by Vibe Active Media 3 years ago

Hi Mike

Great article, and nicely explained. Vibe Active Media has a full Developers API with code laid out ready to use, just copy and paste in to any website and away you go. there is code for PHP SMS ASP SMS and XML SMS

With our platform you can send the SMS directly from any email client using our Email 2 SMS Gateway, simply create the email using the recipients mobile number and send.

Any Web Developers looking to integrate SMS in to their e-commerce or CMS we can provide you with test credits to help.

I think a lot of people think its very complex to build SMS in to their website but your article simplifies it very well.

Trevor

Vibe Active Communications

http://www.VibeActiveMedia.com
http://www.VibeActive.com

Trevor Vibe Active Communications http://www.VibeActiveMedia.com http://www.VibeActive.com

Posted by aeroporika 2 years ago

In our site (http://www.petas.gr/gr/aeroporika-eisitiria.html) we already send sms using mVolution platform provided by Atcom, do you know any other sms platform to suggest me? Thank you in advance.