Text Message Sender
This article is provided to help you make your own text message sending application that uses smtp.gmail.com to send text messages from a valid gmail account to a cell phone.
Start by adding the following controls to your windows forms application:
Now as shown in the picture you may want to place Labels (“Enter phone number:”,”Carrier:”,”Subject:”,”Body:”,”Number of times to send:”) next to the appropriate controls to know which field you are entering data to.
Now here is the source code:
Imports System.Net.Mail Public Class Form1 Dim EMAIL="YOUR_GMAIL_ADDRESS@gmail.com" Dim PASS="YOUR_GMAIL_PASSWORD" Private Sub cliky(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Doit.Click Doit.Enabled = False For i = 0 To counter.Value Dim mail As New MailMessage mail.From = New MailAddress(EMAIL) If svc.Text = "Verizon" Then mail.To.Add("" & address.Text & "@vtext.com") ElseIf svc.Text = "AT&T" Then mail.To.Add("" & address.Text & "@mobile.att.net") ElseIf svc.Text = "Cingular" Then mail.To.Add("" & address.Text & "@mycingular.com") ElseIf svc.Text = "Nextel" Then mail.To.Add("" & address.Text & "@messaging.nextel.com") ElseIf svc.Text = "T-Mobile" Then mail.To.Add("" & address.Text & "@tmomail.net") ElseIf svc.Text = "Sprint" Then mail.To.Add("" & address.Text & "@messing.sprintpcs.com") ElseIf svc.Text = "Tracfone" Then mail.To.Add("" & address.Text & "@cingularme.com") End If mail.Subject = subject.Text mail.Body = body.Text Dim smtp As New SmtpClient("smtp.gmail.com") smtp.Port = 587 smtp.EnableSsl = True smtp.Credentials = New System.Net.NetworkCredential(EMAIL, PASS) smtp.Send(mail) Next Doit.Enabled = True End Sub End Class
Please correct the data of the two variables EMAIL and PASS at the top of the code.
The Code Explanation
This is a namespace that we are referring to so that we don’t have to specify it when accessing the members of this namespace.
Line 03 & 04:
Dim EMAIL="YOUR_GMAIL_ADDRESS@gmail.com" Dim PASS="YOUR_GMAIL_PASSWORD"
In order to send email to a cell phone we need to use an email account with the proper credentials to do that. So here we are going to use a GMAIL account. Specify your gmail username with the extension ‘@gmail.com’ for the ‘EMAIL’ variable and use your gmail account password for the variable ‘PASS’
Line 05 & 34:
Everything that happens between these lines is an event that is triggered when the user presses the button.
Doit.Enabled = False
This disables the button until all the text messages were sent, so the user won’t screw anything up.
Line 07 & 32:
For i = 0 to counter.Value
This introduces a loop that increments the variable ‘i’ from 0 by 1 until it reaches the same value as counter.value; that is, the number of times the user specified the text message to send.
Dim mail As New MailMessage
Creates a new mail message that needs its fields (to, from, subject, body, etc.) filled out before being sent as email.
mail.From = EMAIL
This means that the sender of the mail is you; the gmail address you specified at the beginning of the code.
Line 10 – 24:
Branches to add a certain extension to the specified phone number as an email address to send to depending on the carrier.
Line 25 & 26:
Takes what the user input into the subject and body fields and uses that as the input for mail.subject and mail.body.
Dim smtp As New SmtpClient("smtp.gmail.com")
Creates a new SmtpClient to use to send the mail. This client is ‘smtp.gmail.com’ and the name of this object is ‘smtp’.
smtp.Port = 587
Self Explanatory, uses port 587 for the transfer of this data.
EnableSsl = Use Secure Socket Layer to encrypt the connection.
Line 30: Here’s where we place the credentials (username and password) so that we have a valid right to use this email address as a sender of this email.
Line 31: Send the mail
Line 33: Re-enable the button.