发短信C#代码

来源:互联网 发布:人工智能ppt模板 编辑:程序博客网 时间:2024/04/20 04:14
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();


}


}