Send SMS via ASP.NET

There is an example page called SendMessage.ASP which you should have copied to a virtual directory. The script to send the Message is shown below.


function processForm()
on error resume next

'Setup the service object
dim sendService
Set sendService = Server.CreateObject("Esendex.SendService.2")

'Error Handling
if err.number <> 0 then
processForm = "Unable to create Esendex.SendService.2 object. Ensure COM object is registered. Error description: " + err.Description
exit function
end if

'Setup the send service object
sendService.Initialise Request.Form("Username"), Request.Form("Password"), Request.Form("Account")
'Attempt to send and handle any errors
dim messageID
messageID = sendService.SendMessage(Request.Form("recipient"), Request.Form("body"), 1)
if err.number <> 0 then
'Get the error code!!
Dim errorCode
errorCode = err.number And &HFFFF&

'Create the message to display
processForm = "Unable to send message: " + err.Description + ". Error Code: " + CStr(errorCode)
exit function
else
processForm = "Message sent. ID is " + messageID
end if
on error goto 0
end function

It looks like quite a lot but it's very simple when broken down.
In order to send a Message you first need to instantiate a Esendex.SendService.2 object.


dim sendService
set sendService = Server.CreateObject("Esendex.SendService.2")

This object has several methods that you can use to send a Message and check on the Message's status and it also has a method called Initialise.


sendService.Initialise Request.Form("Username"), Request.Form("Password"), Request.Form("Account") 

This is where you pass in your Account details and this is required for all of the other methods on the Send Service to work. As you can see the Account details are Username, Password and Account. These are the user name and password that you use to sign in to the website and the Account is your Account Reference, this usually starts with EX00 and is followed by five numbers i.e. EX0012345.

Now that you've instantiated and initialised your sendService object you can send a Message, simply called the SendMessage method.


dim messageID
messageID = sendService.SendMessage(Request.Form("recipient"), Request.Form("body"), 1) 

The SendMessage method returns a Message Id which is a unique identifier in our database for the message that you sent. It's best if you store these Message Ids as you can use them to check on the status of a Message and you will need them if you contact support with a query about a particular Message.

To actually send the Message three parameters are required, Recipient, Body and Type. The first two are self-explanatory, the mobile number to send to and the content of the Message. The third parameter, type, is the type of Message you wish to send. Usually you will set this to 1 which is type Text but you can also set it to Binary (2), SmartMessage (3) or Unicode (4).