I have beaten out the bugs that people said they were getting.
Originally Posted by Link
There's nothing named "SMSmobile"...
You need to name the module SMSmobile.
now the following code only has one error.
Code:
Imports System.IO
Imports System.Text.RegularExpressions
Module SMSMobile
Public Class SMS
Private _numberLength As Integer
Private _ListOfNumbers As New Dictionary(Of String, String)
Private _ListOfProviders As New Dictionary(Of String, String)
Private _phonenumber As String
Private _FilePath As String
Private __defaultprovider As String
#Region "Properties"
Private Property PhoneNumber() As String
Get
Return _phonenumber
End Get
Set(ByVal value As String)
_phonenumber = value
End Set
End Property
Private Property Length() As Integer
Get
Return _numberLength
End Get
Set(ByVal value As Integer)
_numberLength = value
End Set
End Property
Private Property ListOfProviders() As Dictionary(Of String, String)
Get
Return _ListOfProviders
End Get
Set(ByVal value As Dictionary(Of String, String))
_ListOfProviders = value
End Set
End Property
Private Property ListOfNumbers() As Dictionary(Of String, String)
Get
Return _ListOfNumbers
End Get
Set(ByVal value As Dictionary(Of String, String))
_ListOfNumbers = value
End Set
End Property
Private Property DefaultProvidor() As String
Get
Return __defaultprovider
End Get
Set(ByVal value As String)
__defaultprovider = value
End Set
End Property
Public Property FilePath() As String
Get
Return _FilePath
End Get
Set(ByVal value As String)
_FilePath = value
End Set
End Property
#End Region
#Region "Constructors"
Public Sub New(ByVal df As String, ByVal fp As String)
If Not String.IsNullOrEmpty(fp) Or Not String.IsNullOrEmpty(fp) Then
DefaultProvidor = df
Length = 10
FilePath = fp
LoadProviders()
Else
Throw New Exception("Your filepath/Defaultprovider is invalid")
End If
End Sub
Public Sub New(ByVal df As String, ByVal l As Integer, ByVal fp As String)
Try
If Not String.IsNullOrEmpty(df) Or Integer.Parse(l) Then
DefaultProvidor = df
Length = l
FilePath = fp
LoadProviders()
Else
Throw New Exception("Default Provider must be a string")
End If
Catch ex As Exception
'handle your exception here.
Console.WriteLine(ex.Message)
End Try
End Sub
#End Region
#Region "Public Functions"
'public functions.
Public Sub AddSingle(ByVal number As String, ByVal provider As String)
PhoneNumber = number
If CheckNumber() Then
If DoesProviderExist(provider) Then
ListOfNumbers.Add(PhoneNumber, provider)
End If
End If
End Sub
Public Sub AddSingle(ByVal number As String) 'uses the default provider
PhoneNumber = number
If CheckNumber() Then
ListOfNumbers.Add(PhoneNumber, DefaultProvidor)
End If
End Sub
Public Sub Delete(ByVal number As String)
ListOfNumbers.Remove(number)
End Sub
Public Sub SendAll(ByVal temp As String)
Dim fullEmail As String = String.Empty
For Each num In ListOfNumbers
Dim pro As String = String.Empty
If ListOfProviders.TryGetValue(num.Value, pro) Then
Dim temp As String '= pro.Replace("number", num.Key)
SendEmail(temp)
Else
Console.WriteLine("Sendall failed")
End If
Next
End Sub
#End Region
#Region "Private functions"
Private Sub SendEmail(ByVal email As String)
'send your email here.
Console.WriteLine(email)
End Sub
Private Sub LoadProviders()
Dim reader As New IO.StreamReader(FilePath) ' create new streamreader
Do Until reader.EndOfStream
Dim line As String = reader.ReadLine 'read single line
Dim proArray() As String = line.Split(",") 'split the line
ListOfProviders.Add(proArray(0).Trim, proArray(1).Trim) 'store both parts into dictionary
Loop
reader.Close()
End Sub
'this method parses the number, and removes any dashses.
Private Function CheckNumber() As Boolean
Dim ph As String = PhoneNumber
'remove one or zero.
If ph.StartsWith(0) Or ph.StartsWith(1) Then
ph = ph.Remove(0, 1)
End If
'remove all dashes and check for length
ph = ph.Replace("-", String.Empty)
If ph.Length <> Length Or ph.Length + 1 <> Length + 1 Then
Console.WriteLine("Length didnt match")
Return False
End If
' Check a (USA style) telephone number
'1-800-201-1929 (true)
'800-201-1929 (true)
'201-1929 (false)
Dim myRegex As New Regex( _
"^1?\s*-?\s*(\d{3}|\(\s*\d{3}\s*\))\s*-?\s*\d{3}\s*-?\s*\d{4}$")
If Not myRegex.IsMatch(ph) Then
Console.WriteLine("not in right format")
Return False
End If
PhoneNumber = ph
Console.WriteLine(PhoneNumber)
Return True
End Function
Private Function DoesProviderExist(ByVal name As String) As Boolean
If ListOfProviders.ContainsKey(name.Trim) Then
Console.WriteLine("provider exists: " & name)
Return True
Else
Console.WriteLine("provider don't exist")
Return False
End If
End Function
#End Region
End Class
End Module
the error is in here highlighted in lime. there error code is Name 'num' is not declared.
Code:
Public Sub SendAll(ByVal temp As String)
Dim fullEmail As String = String.Empty
For Each num In ListOfNumbers
Dim pro As String = String.Empty
If ListOfProviders.TryGetValue(num.Value, pro) Then
Dim temp As String '= pro.Replace("number", num.Key)
SendEmail(temp)
Else
Console.WriteLine("Sendall failed")
End If
Next
End Sub
If one spends a few minutes scanning thru the provided code and tries to figure out what it does, he/she will find that the code will never work. Why? The heart of the SMS class where it actually sends an SMS is the SendEmail sub, and all this sub does is to write the message back to the console.
No wonder why nobody can get it to send any messages
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it. - Abraham Lincoln -
you are right. maybe masfenix could work a little on that. it would be great i have always wanted to send SMS with my computer and i know it is possible cause aim and yim can send and receive sms messages.
If one spends a few minutes scanning thru the provided code and tries to figure out what it does, he/she will find that the code will never work. Why? The heart of the SMS class where it actually sends an SMS is the SendEmail sub, and all this sub does is to write the message back to the console.
No wonder why nobody can get it to send any messages
Right then, why is it in codebank at all ? Admins !!!!!!!!!!!
haha i m sorry guys. the reason i left out send email is that because i assumed most of you have classes made for emailing through dot net. so it's be obselete if i added email functinality.
however, I will add the proper email handling code tonight and reupdate the whole post with a PROPER working one!
Hey sorry, I had two midterms this week, and two next week! So i dont know when I'll be able to update!
However, it's very easy to do this.
Some mobile phone carriers provide a mechanism to send emails to mobile phones on their networks using SMS (Wikipedia).
And the file is there with easy to use formatting. basically you just take your number, and send a message to that number as email!
its really just basic File IO and sending an email!
What you would theoretically like to do is get the number, get the providor, and then search through the textfile for the provider line by line. Once you find the provider, split up the line with teh "," and you'll have an array of two strings. the second element in that array is the email. Simple just replace "number" with your number and send the email off!
i have worked with file IO and i can probably work up something that will send an email. would you mind if i took a shot at this? i might be able to get it done before you .
ok well i think i have finished. it worked fine for me when i sent a text to my friend. there are a couple of controls that need to be on the form. i will upload a pic and lable the control names. the form1 code is as follows:
Code:
Imports System.Net
Imports System.Net.Mail
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'provider,file path to providers.txt
Dim a As New SMSMobile.SMS("Sprint (Nextel)", "c:\providors.txt")
'phone number,provider
a.AddSingle("555-555-5555", "Sprint (Nextel)")
'first step in sending the email
a.SendAll()
Console.ReadLine()
End Sub
'sends the email
Public Sub sendmail(ByVal from As String, ByVal reciever As String, ByVal text As String, ByVal subject As String)
Dim message As New MailMessage(from, reciever, subject, text)
' this is just for sprint (nextel) but can be changed around to use
'other providers
Dim emailClient As New SmtpClient("page.nextel.com")
emailClient.Send(message)
End Sub
End Class
the modules code is almost the same as the original.
Code:
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Net.Mail
Module SMSMobile
Public Class SMS
Private _numberLength As Integer
Private _ListOfNumbers As New Dictionary(Of String, String)
Private _ListOfProviders As New Dictionary(Of String, String)
Private _phonenumber As String
Private _FilePath As String
Private __defaultprovider As String
#Region "Properties"
Private Property PhoneNumber() As String
Get
Return _phonenumber
End Get
Set(ByVal value As String)
_phonenumber = value
End Set
End Property
Private Property Length() As Integer
Get
Return _numberLength
End Get
Set(ByVal value As Integer)
_numberLength = value
End Set
End Property
Private Property ListOfProviders() As Dictionary(Of String, String)
Get
Return _ListOfProviders
End Get
Set(ByVal value As Dictionary(Of String, String))
_ListOfProviders = value
End Set
End Property
Private Property ListOfNumbers() As Dictionary(Of String, String)
Get
Return _ListOfNumbers
End Get
Set(ByVal value As Dictionary(Of String, String))
_ListOfNumbers = value
End Set
End Property
Private Property DefaultProvidor() As String
Get
Return __defaultprovider
End Get
Set(ByVal value As String)
__defaultprovider = value
End Set
End Property
Public Property FilePath() As String
Get
Return _FilePath
End Get
Set(ByVal value As String)
_FilePath = value
End Set
End Property
#End Region
#Region "Constructors"
Public Sub New(ByVal df As String, ByVal fp As String)
If Not String.IsNullOrEmpty(fp) Or Not String.IsNullOrEmpty(fp) Then
DefaultProvidor = df
Length = 10
FilePath = fp
LoadProviders()
Else
Throw New Exception("Your filepath/Defaultprovider is invalid")
End If
End Sub
Public Sub New(ByVal df As String, ByVal l As Integer, ByVal fp As String)
Try
If Not String.IsNullOrEmpty(df) Or Integer.Parse(l) Then
DefaultProvidor = df
Length = l
FilePath = fp
LoadProviders()
Else
Throw New Exception("Default Provider must be a string")
End If
Catch ex As Exception
'handle your exception here.
Console.WriteLine(ex.Message)
End Try
End Sub
#End Region
#Region "Public Functions"
'public functions.
Public Sub AddSingle(ByVal number As String, ByVal provider As String)
PhoneNumber = number
If CheckNumber() Then
If DoesProviderExist(provider) Then
ListOfNumbers.Add(PhoneNumber, provider)
End If
End If
End Sub
Public Sub AddSingle(ByVal number As String) 'uses the default provider
PhoneNumber = number
If CheckNumber() Then
ListOfNumbers.Add(PhoneNumber, DefaultProvidor)
End If
End Sub
Public Sub Delete(ByVal number As String)
ListOfNumbers.Remove(number)
End Sub
'first step in sending mail. when finished it calls the send email sub
Public Sub SendAll()
Dim fullEmail As String = String.Empty
For Each num In ListOfNumbers
Dim pro As String = String.Empty
If ListOfProviders.TryGetValue(num.Value, pro) Then
Dim temp As String = pro.Replace("number", num.Key)
'send the email. this calls a sub on form1. i imagine that you could
'put the sub in this module this is the only thing i changed.
Form1.sendmail(Form1.TextBox2.Text, temp, Form1.TextBox1.Text, Form1.TextBox3.Text)
Else
Console.WriteLine("Sendall failed")
End If
Next
End Sub
#End Region
#Region "Private functions"
Private Sub LoadProviders()
Dim reader As New IO.StreamReader(FilePath) ' create new streamreader
Do Until reader.EndOfStream
Dim line As String = reader.ReadLine 'read single line
Dim proArray() As String = line.Split(",") 'split the line
ListOfProviders.Add(proArray(0).Trim, proArray(1).Trim) 'store both parts into dictionary
Loop
reader.Close()
End Sub
'this method parses the number, and removes any dashses.
Private Function CheckNumber() As Boolean
Dim ph As String = PhoneNumber
'remove one or zero.
If ph.StartsWith(0) Or ph.StartsWith(1) Then
ph = ph.Remove(0, 1)
End If
'remove all dashes and check for length
ph = ph.Replace("-", String.Empty)
If ph.Length <> Length Or ph.Length + 1 <> Length + 1 Then
Console.WriteLine("Length didnt match")
Return False
End If
' Check a (USA style) telephone number
'1-800-201-1929 (true)
'800-201-1929 (true)
'201-1929 (false)
Dim myRegex As New Regex( _
"^1?\s*-?\s*(\d{3}|\(\s*\d{3}\s*\))\s*-?\s*\d{3}\s*-?\s*\d{4}$")
If Not myRegex.IsMatch(ph) Then
Console.WriteLine("not in right format")
Return False
End If
PhoneNumber = ph
Console.WriteLine(PhoneNumber)
Return True
End Function
Private Function DoesProviderExist(ByVal name As String) As Boolean
If ListOfProviders.ContainsKey(name.Trim) Then
Console.WriteLine("provider exists: " & name)
Return True
Else
Console.WriteLine("provider don't exist")
Return False
End If
End Function
#End Region
End Class
End Module
@Bagstoper: your code/design has major flaws and it doesn't conform to the principles of OPP. These are the flaws I spotted:
1. You put the SMS class inside a module, which is unneccessary. This also limit the reusability of the class.
2. The SMS class should be an object itself and unrelated to any other objects. That is, it should not need to know anything about Form1. However, if you look in the SMS.SendAll sub, you're accessing Form1's members. What if I don't have a Form1 in my project?
3. The use of Console.WriteLine is useless. You don't have a console in windows applications.
4. There's no error handling in Form1.sendmail sub. Your program will crash if it fails to send the email. Besides, you hardcoded the smtp server name which should have been obtained from the SMS instance.
Happy coding
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it. - Abraham Lincoln -
I know my code isn't perfect. I don't expect it to be. I know I could have coded it differently but I am working on several other projects at this time. If I wanted to make this into a fully functional application I would have to play around with it some more. But it is easy enough to make it into a class and un-hardcode the SMTP server. Everything I did was in a hurry. I just wanted to get it done.
The regex I have in there is for US number style only. However I only put that there because the UK carriers i tested were not relaying the text message through email so I didn't bother with them. However its up to you if you want to support UK
You might try googling for more providers. If you are looking for a specific provider then you can do this. On a phone that is on an unknown provider's network go to where ever you send messages from (ie the message app or what ever the phone uses) then create a new MMS message. this kind of message is mainly used for emailing. Type a random message and then send it to an email account you use. When you open the message through the email account the sender should be something like Phone#@vzwpix.com (that is an example from a Verizon phone) that is how you find out the provider for an unknown network.
Last edited by bagstoper; Jun 9th, 2009 at 09:34 PM.