通過編程方式實現簡訊息的發送對很多人來說是一件比較煩雜的事情,目前一般的解決方案是通過電腦和手機的連線,通過可對手機編程的語言編寫相關的手機簡訊息程式來實現,而這種方法對於一般人來說是很難達到的,因為此種方法不僅要有很多必備的硬體裝置,也還需懂得手機編程的相關知識。本文就來探討一下另外一種通過Visual C#傳送簡訊息的簡單實現方法,這種方法在實現過程中比我們想象的要簡單許多,只需擁有Visual C#一般編程基礎,並確定您的電腦接入互連網即可。下面就來詳細介紹一下Visual C#傳送簡訊息的具體實現過程。
一. Visual C#傳送簡訊息的原理:
我想當很多讀者一看到本文的題目一定會想本文內容一定非常深奧,並且作者一定知道了電信的傳送簡訊的網關地址,其實並非如此。本文其實是利用一個現成的資源,一個可傳送簡訊的Web Serviec。這個Web Service就是新浪網就提供的一個,可供使用者直接調用的發送短訊息的Web Service。這個Service的地址是http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl。在這個Service中提供了一個發送短訊息的方法"sendXml"。此方法的文法格式如下:
string sendXml (carrier , userid , password , mobilenumber , content , msgtype )
sendXml方法中的六個參數均為string類型,並且sendXml方法的傳回值也是string類型。
以下是sendXml方法中的六個參數的具體說明:
·carrier:電訊廠商名稱,具體使用時此參數並沒有什麼具體要求,即這裡面可以隨便輸,輸入的字串也不會在對方手機中有任何顯示。
·userid:在新浪網上註冊的手機號,註冊手機所用的地址是:http://sms.sina.com.cn,具體註冊方法下面會具體介紹。
·password:您在新浪網成功註冊手機後,新浪網所反饋來的密碼。
·mobilenumber:要傳送簡訊到對方的手機號碼。
·content:所要發送短訊息的內容。
·msgtype:發送短訊息的類型,由於本文發送的不是多媒體訊息,所以輸入"Text"。
調用此Web Service只需在Visual C#開發環境中直接添加Web引用,把該地址輸入即可,就可以使用此Web Service中的sendXml方法發送短訊息了。當然發送短訊息的機器必須接入互連網。
二.在新浪網上註冊手機:
按照如下步驟就可在新浪網上註冊手機:
1. 開啟瀏覽器,並在地址欄中輸入新浪無線地址:http://sms.sina.com.cn.。在瀏覽器德左上方,可見01介面:
輸入手機號後,單擊圖01中的"登陸",如果你的手機沒有在新浪,則提示02所示資訊。
2. 按照圖02中的選項輸入相應的資訊後,單擊"登陸移動夢網"按鈕,如果註冊成功,移動夢網則會向你註冊的手機上發送一個8位長度的口令就可以得到圖03所示介面。
這個口令要保留,下面傳送簡訊就要使用這個口令。
三.本文程式設計、調試和啟動並執行環境:
(1).微軟公司視窗2000伺服器版。
(2).Visual Studio .Net 2003企業構建版,.Net FrameWork SDK版本號碼4322。
四.Visual C#實現簡訊息發送的具體實現步驟:
Visual C#傳送簡訊息的關鍵就是通過Web引用新浪網提供的傳送簡訊息的Web Service,並在引用完成後。調用此Service的sendXml方法即可。以下就是Visual C#引用Web Service傳送簡訊息的具體實現步驟:
1. 啟動Visual Studio .Net。
2. 選擇菜單【檔案】|【建立】|【項目】後,彈出【建立項目】對話方塊。
3. 將【項目類型】設定為【Visual Basic項目】。
4. 將【模板】設定為【Windows應用程式】。
5. 在【名稱】文字框中輸入【簡訊】。
6. 在【位置】的文字框中輸入【E:\VS.NET項目】,然後單擊【確定】按鈕,這樣在"E:\VS.NET項目"目錄中就產生了名稱為"簡訊"的檔案夾,並在裡面建立了名稱為"簡訊"的專案檔。
7. 把Visual Studio .Net的當前視窗切換到【Form1.cs(設計)】視窗,並從【工具箱】中的【Windows表單組件】選項卡中往Form1表單中拖入下列組件,並執行相應的操作:
四個Label組件。
四個TextBox組件。
一個Button組件,其作用是傳送簡訊息。並在這個Button組件拖入Form1的設計表單後,雙擊它,則系統會在Form1.cs檔案分別產生這個組件的Click事件對應的處理代碼。
8. 把Visual Studio .Net的當前視窗切換到Form1.vb的代碼編輯視窗,並用下列代碼替換Form1.cs中的InitializeComponent過程對應的代碼,下列代碼作用是初始化表單中加入的組件:
private void InitializeComponent ( ) { this.textBox1 = new System.Windows.Forms.TextBox ( ) ; this.textBox2 = new System.Windows.Forms.TextBox ( ) ; this.textBox3 = new System.Windows.Forms.TextBox ( ) ; this.button1 = new System.Windows.Forms.Button ( ) ; this.label1 = new System.Windows.Forms.Label ( ) ; this.label2 = new System.Windows.Forms.Label ( ) ; this.label3 = new System.Windows.Forms.Label ( ) ; this.label4 = new System.Windows.Forms.Label ( ) ; this.textBox4 = new System.Windows.Forms.TextBox ( ) ; this.SuspendLayout ( ) ; this.textBox1.Location = new System.Drawing.Point ( 144 , 16 ) ; this.textBox1.Name = "textBox1" ; this.textBox1.Size = new System.Drawing.Size ( 184 , 21 ) ; this.textBox1.TabIndex = 0 ; this.textBox1.Text = "" ; this.textBox2.Location = new System.Drawing.Point ( 144 , 69 ) ; this.textBox2.Name = "textBox2" ; this.textBox2.PasswordChar = ''''''''*'''''''' ; this.textBox2.Size = new System.Drawing.Size ( 184 , 21 ) ; this.textBox2.TabIndex = 1 ; this.textBox2.Text = "" ; this.textBox3.Location = new System.Drawing.Point ( 144 , 122 ) ; this.textBox3.Name = "textBox3" ; this.textBox3.Size = new System.Drawing.Size ( 184 , 21 ) ; this.textBox3.TabIndex = 2 ; this.textBox3.Text = "" ; this.button1.Location = new System.Drawing.Point ( 152 , 256 ) ; this.button1.Name = "button1" ; this.button1.Size = new System.Drawing.Size ( 80 , 32 ) ; this.button1.TabIndex = 4 ; this.button1.Text = "發送" ; this.button1.Click += new System.EventHandler ( this.button1_Click ) ; this.label1.Location = new System.Drawing.Point ( 56 , 24 ) ; this.label1.Name = "label1" ; this.label1.Size = new System.Drawing.Size ( 88 , 16 ) ; this.label1.TabIndex = 5 ; this.label1.Text = "註冊手機號:" ; this.label2.Location = new System.Drawing.Point ( 88 , 77 ) ; this.label2.Name = "label2" ; this.label2.Size = new System.Drawing.Size ( 72 , 16 ) ; this.label2.TabIndex = 6 ; this.label2.Text = "口令:" ; this.label3.Location = new System.Drawing.Point ( 56 , 128 ) ; this.label3.Name = "label3" ; this.label3.Size = new System.Drawing.Size ( 96 , 16 ) ; this.label3.TabIndex = 7 ; this.label3.Text = "目標手機號:" ; this.label4.Location = new System.Drawing.Point ( 96 , 176 ) ; this.label4.Name = "label4" ; this.label4.Size = new System.Drawing.Size ( 72 , 16 ) ; this.label4.TabIndex = 8 ; this.label4.Text = "內容:" ; this.textBox4.Location = new System.Drawing.Point ( 144 , 175 ) ; this.textBox4.Multiline = true ; this.textBox4.Name = "textBox4" ; this.textBox4.Size = new System.Drawing.Size ( 184 , 48 ) ; this.textBox4.TabIndex = 3 ; this.textBox4.Text = "" ; this.AutoScaleBaseSize = new System.Drawing.Size ( 6 , 14 ) ; this.ClientSize = new System.Drawing.Size ( 410 , 303 ) ; this.Controls.Add ( this.button1 ) ; this.Controls.Add ( this.textBox4 ) ; this.Controls.Add ( this.textBox3 ) ; this.Controls.Add ( this.textBox2 ) ; this.Controls.Add ( this.textBox1 ) ; this.Controls.Add ( this.label4 ) ; this.Controls.Add ( this.label3 ) ; this.Controls.Add ( this.label2 ) ; this.Controls.Add ( this.label1 ) ; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle ; this.MaximizeBox = false ; this.Name = "Form1" ; this.Text = "Visual C#實現簡訊發送" ; this.ResumeLayout ( false ) ; } |
至此【簡訊】項目的介面設計和功能實現的準備工作就完成了,具體04所示:
9. 選中【簡訊】的【解決方案資源管理員】,右擊【引用】,彈出【添加Web引用】對話方塊。在此對話方塊中的【URL】文字框中輸入"http://smsinter.sina.com.cn/ws/smswebservice0101.wsdl"後,單擊【轉到】按鈕,則會得到圖05所示介面:
此時單擊圖04中的【添加引用】按鈕,則成功實現在【簡訊】項目中添加可傳送簡訊息的新浪網提供的Web Service。
10. 把Visual Stuido .Net的當前視窗切換到Form1.cs的代碼編輯視窗,並用下列代碼替換From1.cs中的button1的Click事件對應的代碼,下列代碼的作用是調用引入的Web Service中提供的sendXml方法向指定手機傳送簡訊息:
private void button1_Click ( object sender , System.EventArgs e ) { 簡訊.cn.com.sina.smsinter.SMSWS ws = new 簡訊.cn.com.sina.smsinter.SMSWS ( ) ; string result = ws.sendXml ( "Sina" ,textBox1.Text ,textBox2.Text ,textBox3.Text ,textBox4.Text ,"new" ) ; MessageBox.Show ( result ) ; } |
11. 至此,在上述步驟都正確執行後,【簡訊】項目的全部工作就完成了。單擊快速鍵F5運行程式,在輸入【註冊手機號】、【口令】、【目標手機號】和【內容】後,單擊【發送】按鈕後,程式就會向指定的手機號傳送簡訊息了。
五.總結:
本文介紹的這種Visual C#傳送簡訊息方法,其解決關鍵是引用Web Service,調用此Web Service中的方法。通觀全文,此種方法是不是很簡單。最後還要提醒諸位,利用這種傳送簡訊息並不是免費的午餐,其資費標準可參看新浪無線網站上的相關說明,每發送一條收費一角錢,從在新浪網註冊的手機上收費。在使用本文介紹的方法傳送簡訊息時,在發送完成後,一般會有一個延遲。這是因為後台採用了了訊息佇列機制,不過這種延遲一般只會有幾秒鐘的時間。