Click here to Skip to main content
11,707,470 members (76,558 online)
Click here to Skip to main content

Send a Text Message to a Cell Phone from a VB.NET Application

, 23 Aug 2006 324.2K 15.1K 124
Rate this:
Please Sign up or sign in to vote.
This article describes a simple way to send text messages to a cellular phone from within a VB.NET desktop application.

Sample Image - screenshot.jpg

Introduction

This article describes a simple way to send text messages to a cellular phone from within a VB.NET desktop application. The source code provided includes a relatively good list of carriers to simplify the task of connecting with a cell phone, and the task itself is really no more difficult than sending an email message through a desktop or web based application.

Getting Started

In order to begin, unzip the downloaded files and open the project provided. Within the project, you will find one main class: frmMain.vb. The main form is a Windows application form, and it contains a few controls necessary to capture the fields needed to properly form the message. These fields include:

  • Recipient’s phone number: Captures the recipient’s cellular telephone number (10 digit).
  • Recipient’s carrier: Captures the recipient’s carrier.
  • Sender’s email address: Captures the sender’s email address.
  • Sender’s email server: Captures the name of the sender’s email server.
  • Message subject line: Captures the message’s title or subject.
  • Message body: Captures the sender’s message content.

The application is simple, but could easily be improved by validating each of the required fields through the use of regular expressions or by at least validating that the text associated with each of the text boxes is not an empty string. To maintain the simplicity of the project, little in the way of error handling has been included.

The following figure (Figure 1) shows a properly configured collection of input fields in use:

Sample Image - 1.jpg

Figure 1: The Demonstration Application in Use

A quick review of the code will reveal that there is little going on there. The following imports were added to the top of the class:

Imports System
Imports System.Net.Mail

The System.Net.Mail import brings in the support necessary to transmit the messages generated using the application. Following the imports and the class declaration, there is a Declarations region identified, and within that region is a collection of private member variables; these private member variables are created in order to supply each of the required elements of the message.

#Region "Declarations"

    ' message elements
    Private mMailServer As String
    Private mTo As String
    Private mFrom As String
    Private mMsg As String
    Private mSubject As String

#End Region

At this point, the only thing left to do in code is to write the following three methods:

#Region "Methods"

Private Sub frmMain_Load(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

    ' set up the carriers list - this is a fair list,
    ' you may wish to research the topic and add others,
    ' it took a while to generate this list...
    cboCarrier.Items.Add("@itelemigcelular.com.br")
    cboCarrier.Items.Add("@message.alltel.com")
    cboCarrier.Items.Add("@message.pioneerenidcellular.com")
    cboCarrier.Items.Add("@messaging.cellone-sf.com")
    cboCarrier.Items.Add("@messaging.centurytel.net")
    cboCarrier.Items.Add("@messaging.sprintpcs.com")
    cboCarrier.Items.Add("@mobile.att.net")
    cboCarrier.Items.Add("@mobile.cell1se.com")
    cboCarrier.Items.Add("@mobile.celloneusa.com")
    cboCarrier.Items.Add("@mobile.dobson.net")
    cboCarrier.Items.Add("@mobile.mycingular.com")
    cboCarrier.Items.Add("@mobile.mycingular.net")
    cboCarrier.Items.Add("@mobile.surewest.com")
    cboCarrier.Items.Add("@msg.acsalaska.com")
    cboCarrier.Items.Add("@msg.clearnet.com")
    cboCarrier.Items.Add("@msg.mactel.com")
    cboCarrier.Items.Add("@msg.myvzw.com")
    cboCarrier.Items.Add("@msg.telus.com")
    cboCarrier.Items.Add("@mycellular.com")
    cboCarrier.Items.Add("@mycingular.com")
    cboCarrier.Items.Add("@mycingular.net")
    cboCarrier.Items.Add("@mycingular.textmsg.com")
    cboCarrier.Items.Add("@o2.net.br")
    cboCarrier.Items.Add("@ondefor.com")
    cboCarrier.Items.Add("@pcs.rogers.com")
    cboCarrier.Items.Add("@personal-net.com.ar")
    cboCarrier.Items.Add("@personal.net.py")
    cboCarrier.Items.Add("@portafree.com")
    cboCarrier.Items.Add("@qwest.com")
    cboCarrier.Items.Add("@qwestmp.com")
    cboCarrier.Items.Add("@sbcemail.com")
    cboCarrier.Items.Add("@sms.bluecell.com")
    cboCarrier.Items.Add("@sms.cwjamaica.com")
    cboCarrier.Items.Add("@sms.edgewireless.com")
    cboCarrier.Items.Add("@sms.hickorytech.com")
    cboCarrier.Items.Add("@sms.net.nz")
    cboCarrier.Items.Add("@sms.pscel.com")
    cboCarrier.Items.Add("@smsc.vzpacifica.net")
    cboCarrier.Items.Add("@speedmemo.com")
    cboCarrier.Items.Add("@suncom1.com")
    cboCarrier.Items.Add("@sungram.com")
    cboCarrier.Items.Add("@telesurf.com.py")
    cboCarrier.Items.Add("@teletexto.rcp.net.pe")
    cboCarrier.Items.Add("@text.houstoncellular.net")
    cboCarrier.Items.Add("@text.telus.com")
    cboCarrier.Items.Add("@timnet.com")
    cboCarrier.Items.Add("@timnet.com.br")
    cboCarrier.Items.Add("@tms.suncom.com")
    cboCarrier.Items.Add("@tmomail.net")
    cboCarrier.Items.Add("@tsttmobile.co.tt")
    cboCarrier.Items.Add("@txt.bellmobility.ca")
    cboCarrier.Items.Add("@typetalk.ruralcellular.com")
    cboCarrier.Items.Add("@unistar.unifon.com.ar")
    cboCarrier.Items.Add("@uscc.textmsg.com")
    cboCarrier.Items.Add("@voicestream.net")
    cboCarrier.Items.Add("@vtext.com")
    cboCarrier.Items.Add("@wireless.bellsouth.com")

End Sub

Private Sub btnSend_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnSend.Click

' Collect user input from the form and stow content into
' the objects member variables

    mTo = Trim(txtPhoneNumber.Text) & _
        Trim(cboCarrier.SelectedItem.ToString())
    mFrom = Trim(txtSender.Text)
    mSubject = Trim(txtSubject.Text)
    mMailServer = Trim(txtMailServer.Text)
    mMsg = Trim(txtMessage.Text)

' Within a try catch, format and send the message to
' the recipient. Catch and handle any errors.
    Try

        Dim message As New MailMessage(mFrom, mTo, mSubject, mMsg)
        Dim mySmtpClient As New SmtpClient(mMailServer)
        mySmtpClient.UseDefaultCredentials = True
        mySmtpClient.Send(message)

        MessageBox.Show("The mail message has been sent to " & _
                        message.To.ToString(), "Mail", _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Information)

    Catch ex As FormatException

        MessageBox.Show(ex.StackTrace, ex.Message, _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)

    Catch ex As SmtpException

        MessageBox.Show(ex.StackTrace, ex.Message, _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)

    Catch ex As Exception

        MessageBox.Show(ex.StackTrace, ex.Message, _
                        MessageBoxButtons.OK, _
                        MessageBoxIcon.Error)
    End Try
End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnExit.Click

' Upon user’s request, close the application
    Application.Exit()

End Sub
#End Region

At this point, the application should be complete. You may wish to build the solution and test it. Even though this example was intended to be simple, the overall concept may be used within an application to do some seemingly complex jobs. For example, if you were tasked with writing an application that monitored some sort of trend information such as a daily stock price, and were to alert a group of end users whenever the stock price exceeded some predetermined, agreed upon value, you could do something such as looping through a collection of users subscribing to the stock price monitoring service and direct a text message to each of these users indicating that the watched stock had surpassed the threshold value.

Also, please note that, whilst it does cost you a dime to send a message to a cell phone in this manner, it may well cost the recipient something to receive it. Bearing that in mind, as you test your version of the code, be mindful of any expenses you may be generating for yourself (if, for example, you are sending messages to yourself) or another person.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

salysle
Software Developer (Senior)
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionQuestion Pin
Member 1176133312-Jun-15 2:18
memberMember 1176133312-Jun-15 2:18 
GeneralMy vote of 5 Pin
ali_habbobi9-Apr-15 6:28
memberali_habbobi9-Apr-15 6:28 
NewsHi Pin
Member 110639976-Sep-14 18:04
memberMember 110639976-Sep-14 18:04 
Questionhow to add idea or airtel subscriber Pin
Member 1054576527-Jun-14 23:34
memberMember 1054576527-Jun-14 23:34 
GeneralMy vote of 4 Pin
Agyemang18-Jun-14 6:37
memberAgyemang18-Jun-14 6:37 
GeneralMy vote of 1 Pin
Rixterz12330-May-14 10:27
memberRixterz12330-May-14 10:27 
Questionmail server and carrier Pin
muhannadcs22-Mar-14 13:00
membermuhannadcs22-Mar-14 13:00 
GeneralMy vote of 1 Pin
Rixterz1237-Mar-14 5:39
memberRixterz1237-Mar-14 5:39 
GeneralRe: My vote of 1 Pin
gggustafson30-May-14 6:05
professionalgggustafson30-May-14 6:05 
Questionneed this for my thesis.. Pin
Member 1042107723-Nov-13 5:08
memberMember 1042107723-Nov-13 5:08 
QuestionMObile carrier in Tonga Pin
backblaze21318-Aug-13 19:12
memberbackblaze21318-Aug-13 19:12 
GeneralThanks... Pin
Creeper_Stache3-Jun-13 4:50
memberCreeper_Stache3-Jun-13 4:50 
GeneralRe: Thanks... Pin
heemanshu bhalla8-Aug-13 8:22
memberheemanshu bhalla8-Aug-13 8:22 
QuestionPDA code for sending text message Pin
justoriko13-May-13 19:40
memberjustoriko13-May-13 19:40 
QuestionNice job. Pin
Sir Mari05-May-13 5:30
memberSir Mari05-May-13 5:30 
QuestionSending mail Pin
DOZDOZ16-Apr-13 5:06
memberDOZDOZ16-Apr-13 5:06 
QuestionSendng mail Pin
DOZDOZ16-Apr-13 4:59
memberDOZDOZ16-Apr-13 4:59 
Questionsend message via mobile though desktop application c# .net Pin
Aditya Malviya17-Dec-12 1:13
memberAditya Malviya17-Dec-12 1:13 
GeneralMy vote of 4 Pin
Parthi Bun17-Oct-12 0:40
memberParthi Bun17-Oct-12 0:40 
GeneralRe: My vote of 4 Pin
murti abdi20-Nov-13 7:01
membermurti abdi20-Nov-13 7:01 
QuestionAre there changes that need to be made in the code Pin
epodemik28-Jun-12 15:36
memberepodemik28-Jun-12 15:36 
QuestionDoubt in program Pin
Arunmozjhidevan19-Jun-12 6:18
memberArunmozjhidevan19-Jun-12 6:18 
Questionhelp Pin
Member 87011386-Mar-12 3:59
memberMember 87011386-Mar-12 3:59 
QuestionHi Pin
omo711724-Feb-12 8:30
memberomo711724-Feb-12 8:30 
Questionmobile carrier list Pin
Gerald Musoke15-Nov-11 4:26
memberGerald Musoke15-Nov-11 4:26 
Questionpls answer this question.. Pin
shieake9-Jan-11 18:20
membershieake9-Jan-11 18:20 
Questionfailure sending Pin
anupama9620105-Jan-11 3:01
memberanupama9620105-Jan-11 3:01 
AnswerRe: failure sending Pin
basheer.j12-Mar-11 16:02
memberbasheer.j12-Mar-11 16:02 
GeneralRe: failure sending Pin
Member 1061081519-Feb-14 19:25
memberMember 1061081519-Feb-14 19:25 
AnswerRe: failure sending Pin
gggustafson30-May-14 6:07
professionalgggustafson30-May-14 6:07 
GeneralRegarding free web services Pin
AbhijeetCSL21-Apr-10 0:21
memberAbhijeetCSL21-Apr-10 0:21 
GeneralRe: Regarding free web services Pin
PatilVL7-Sep-10 17:12
memberPatilVL7-Sep-10 17:12 
GeneralGood Article Pin
Sunil Scaria8-Apr-10 1:40
memberSunil Scaria8-Apr-10 1:40 
GeneralNice Concept Pin
Arun Jacob6-Apr-10 1:30
memberArun Jacob6-Apr-10 1:30 
GeneralI WANT TO MOBILE CARRIER IN INDIA Pin
NETKANNNAN6-Mar-10 18:56
memberNETKANNNAN6-Mar-10 18:56 
GeneralRe: I WANT TO MOBILE CARRIER IN INDIA Pin
PatilVL7-Sep-10 17:12
memberPatilVL7-Sep-10 17:12 
Generalnot receiving msg to mob Pin
CoolV16-Feb-10 23:17
memberCoolV16-Feb-10 23:17 
GeneralExtracting email id from web page Pin
rd21216-Feb-10 20:24
memberrd21216-Feb-10 20:24 
GeneralThis is it... Pin
owen1025057-Jun-09 19:26
memberowen1025057-Jun-09 19:26 
GeneralC# Pin
jammmie9999-Apr-09 0:21
memberjammmie9999-Apr-09 0:21 
Generalhey Pin
farmalik27-Mar-09 8:03
memberfarmalik27-Mar-09 8:03 
QuestionMessage sent successfully,but i am not receive in my mobile Pin
datatechprojects24-Mar-09 22:39
memberdatatechprojects24-Mar-09 22:39 
GeneralFailure sending mail Pin
datatechprojects16-Mar-09 19:51
memberdatatechprojects16-Mar-09 19:51 
GeneralRe: Failure sending mail Pin
obicauka27-Apr-09 7:33
memberobicauka27-Apr-09 7:33 
GeneralRe: Failure sending mail Pin
gggustafson30-May-14 6:09
professionalgggustafson30-May-14 6:09 
QuestionHow to programming on mobile phone ? Pin
Anatha15-Feb-09 16:38
memberAnatha15-Feb-09 16:38 
Generalmobile carriers of Pakistan Pin
Osama10015-Oct-08 22:10
memberOsama10015-Oct-08 22:10 
GeneralI want the mobile carrier for Nigeria Pin
chikezie25-Nov-07 23:40
memberchikezie25-Nov-07 23:40 
Generalnonetrying Pin
colliebee12-Oct-07 15:13
membercolliebee12-Oct-07 15:13 
GeneralGood List Pin
Norberto Olazabal9-Oct-07 3:35
memberNorberto Olazabal9-Oct-07 3:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150819.1 | Last Updated 23 Aug 2006
Article Copyright 2006 by salysle
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid