發簡訊C#代碼

來源:互聯網
上載者:User
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace smsxx
{
/// <summary>
/// Form1 的摘要說明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private string msg=""; //簡訊內容
private string[] nums={"138********","137*******"};//要發送到的手機號
private int[] sendTimes; //記錄重試次數
private AxSMSocx.AxSMS axSMS1; //他們提供的那個sms.ocx控制項
private System.Timers.Timer timer1;
private TextWriter tw =null; //日誌寫入器
/// <summary>
/// 必需的設計器變數。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
... //都是預設產生的程式碼
}

/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose( bool disposing )
{
... //都是預設產生的程式碼
}

#region Windows 表單設計器產生的程式碼
/// <summary>
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內容。
/// </summary>
private void InitializeComponent()
{... //都是預設產生的程式碼
}
#endregion

/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void axSMS1_OnComm(object sender, System.EventArgs e)
{
int ret=Int32.Parse(axSMS1.CommEvent.ToString());
switch(ret)
{
case 4444: addLog("[錯誤] 串口開啟");
break;
case 6666: addLog("[錯誤] 發送逾時");
break;
case 7777: addLog("[錯誤] 發送失敗");
break;
case 8880: addLog("[成功] "+axSMS1.LastSentSMS);
break;
case 8884: string lastsms=axSMS1.LastSentSMS;
addLog("[失敗] "+lastsms); //這裡發送的第一個手機的情況是不會返回的,估計是他們控制項的bug。
int i=lastsms.LastIndexOf("|")+1;
if(i>0)
{

i=Int32.Parse(lastsms.Substring(i));
if(sendTimes[ i]<4)
{
axSMS1.SendSMSWithoutRet(nums,msg,i);
sendTimes[ i]+=1;
}
}
break;
case 8888: addLog("[成功] "+axSMS1.NewSMS);
break;
case 2000: addLog("[失敗] 沒有入網");
axSMS1.Refresh();
break;
case 2003: addLog("[失敗] 拒絕登入");
axSMS1.Refresh();
break;
case 5555: addLog(axSMS1.InputInfo);
break;
case 4000: addLog(axSMS1.Read_SMS(4,1).ToString());
break;

}
}

private void addLog(string log)
{
tw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")+"  "+log);
}

private void Form1_Load(object sender, System.EventArgs e)
{
tw=new StreamWriter("smslogs/"+DateTime.Now.ToString("yyyy-MM-dd")+".log",true);
axSMS1.EndComm();
axSMS1.CommPort=1;
if(!axSMS1.IsOpen)
{
axSMS1.InitComm();
}
if(axSMS1.IsOpen)
{
axSMS1.SMSnotSaveInSIM();
}
else
{
addLog("[失敗]裝置第一次初始化");
}
timer1.Enabled=true;
}

private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
timer1.Enabled=false;

if(!axSMS1.IsOpen)
{
axSMS1.InitComm();

}
if(axSMS1.IsOpen)
{
StreamReader sr=new StreamReader("smsconf",System.Text.Encoding.GetEncoding("GB18030"));
msg=sr.ReadToEnd();
sr.Close();

sendTimes=new int[nums.Length];
for(int i=0;i<nums.Length;i++)
{
sendTimes=1;
}
for(int i=0;i<nums.Length;i++)
{
while(axSMS1.SendingBusyState())
{
Application.DoEvents();
}
if(axSMS1.SendSMSWithoutRet(nums,msg,i))
{
addLog("[成功] " +nums+"->"+msg);
}
else
{
addLog("[失敗] "+nums+"->"+msg);
}

}
for(int i=0;i<1000;i++)
{
Application.DoEvents();
System.Threading.Thread.Sleep(50);
}
axSMS1.EndComm();
}
else
{
addLog("[失敗] 第二次初始化");
}

tw.Close();

Close();

}

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.