Sending SMS Through GSMCOMM library of C#

霸气de小男生 提交于 2019-12-04 19:27:03

Try these guides (it was helpful for me): http://www.codeproject.com/Articles/325731/Bulk-SMS-Sender http://www.codeproject.com/Articles/20420/How-To-Send-and-Receive-SMS-using-GSM-Modem

But it seems that your problem with com-port opening isn't in your code. Try to test your port using something like Teraterm app. And be sure the port isn't open when you start to run your app (it may be still open after previous launching).

when you using gsmcomm .. the first of all , list your comPorts in a comboBox i expert in vb.net .. you can read this code and translate it to C# 1) create a combobox in your form and in form_load , write this code

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each prt In My.Computer.Ports.SerialPortNames
            comboBox1.Items.Add(prt)  
        Next
 End Sub

at the Global scope of your from , write this code

            Public Property mymodem As GsmCommMain

add a sub to your project like below

   Private Sub connect()
    Try
        Cursor.Current = Cursors.WaitCursor
        If comboBox1.Text = "" Then Return
        If IsNothing(mymodem) Then mymodem = New GsmCommMain(comboBox1.Text)
        If Not mymodem.IsOpen Then mymodem.Open()
        Cursor.Current = Cursors.Default
    Catch ex As Exception
        richTextBox1.AppendText(ex.Message & vbCrLf) 'i add a richtextbox to my form for show exceptions and my produced declaration
    End Try
End Sub

after that put a textbox for mobile number .. name it txttel also put a textbox for textMessage .. name it txtMSG put a button to send you message .. name it btnsend the remained code will be like this ..

  Private Sub btnSend_Click(sender As Object, e As EventArgs) Handles btnSend.Click
            If String.IsNullOrEmpty(txtMSG.Text.Trim) Then Return
               SendSMS()

   End Sub


  Private Sub SendSMS()
     Try
          If Not mymodem.IsOpen Then connect()
          Dim pdu As New SmsSubmitPdu(txtMSG.Text.Trim & vbCr, txtTel.Text)
          mymodem.SendMessage(pdu)
          richTextBox1.AppendText("your message sent successfully")
      Catch ex As Exception
          richTextBox1.AppendText(ex.Message)
     End Try
  End Sub

at the end be sure to close your port .. like this

  Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    If Not IsNothing(mymodem) AndAlso mymodem.IsOpen Then
        mymodem.Close()
    End If
  End Sub
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!