我构建了一个使用gsmcomm库和C#发送消息的简单应用程序。当我通过调制解调器发送消息时,我将其存储到oracle数据库 SENTMESSAGE 表中。我在发送消息失败时提供了一个表格,即 FAILEDMESSAGE 表(ID,DATE,TIME,PHONENUMBER,MESSAGE),但我仍然不了解如何实现它。
任何人都可以建议我如何区分在gsmcomm库中发送消息成功和失败?
这是我发送邮件的代码:
private void btnSentSMS_Click(object sender, EventArgs e)
{
var msg = txtMessage.Text;
var phoneNumber = txtNumber.Text;
var pdu = new SmsSubmitPdu(msg, phoneNumber, string.Empty);
comm.SendMessage(pdu);
MessageBox.Show("sms sent");
//STORE SEND SMS TO DATABASE
OracleCommand cmd = new OracleCommand();
cmd.CommandText = @"INSERT INTO SENTMESSAGE (ID, DATE, TIME, PHONENUMBER, MESSAGE) VALUES
(SQ_SENTMESSAGE.NEXTVAL, '" + DateTime.Now + "', TO_DATE('" + DateTime.Now + "', 'dd/MM/yyyy hh24:mi:ss'), '"
+ phoneNumber + "', '" + msg + "')";
cmd.Connection = koneksi_manual.con;
koneksi_manual.con.Open();// <= Open connection before executing the command.
cmd.ExecuteNonQuery();
koneksi_manual.con.Close(); //closing connection
}
答案 0 :(得分:0)
请尝试使用此代码:
private void btnSentSMS_Click(object sender, EventArgs e)
{
var msg = txtMessage.Text;
var phoneNumber = txtNumber.Text;
var pdu = new SmsSubmitPdu(msg, phoneNumber, string.Empty);
if(comm.SendMessage(pdu))
{
MessageBox.Show("sms sent");
//STORE SEND SMS TO DATABASE
OracleCommand cmd = new OracleCommand();
cmd.CommandText = @"INSERT INTO SENTMESSAGE (ID, DATE, TIME, PHONENUMBER, MESSAGE) VALUES
(SQ_SENTMESSAGE.NEXTVAL, '" + DateTime.Now + "', TO_DATE('" + DateTime.Now + "', 'dd/MM/yyyy hh24:mi:ss'), '"
+ phoneNumber + "', '" + msg + "')";
cmd.Connection = koneksi_manual.con;
koneksi_manual.con.Open();// <= Open connection before executing the command.
cmd.ExecuteNonQuery();
koneksi_manual.con.Close(); //closing connection
}
else
{
MessageBox.Show("sms not sent");
OracleCommand cmd = new OracleCommand();
cmd.CommandText = @"INSERT INTO FAILEDMESSAGE (ID, DATE, TIME, PHONENUMBER, MESSAGE) VALUES
(SQ_SENTMESSAGE.NEXTVAL, '" + DateTime.Now + "', TO_DATE('" + DateTime.Now + "', 'dd/MM/yyyy hh24:mi:ss'), '"
+ phoneNumber + "', '" + msg + "')";
cmd.Connection = koneksi_manual.con;
koneksi_manual.con.Open();// <= Open connection before executing the command.
cmd.ExecuteNonQuery();
koneksi_manual.con.Close(); //closing connection
}
}
答案 1 :(得分:-1)
SemdMessage方法如:
public bool SendMessage(SerialPort port, string phoneNo, string message)
{
bool isSend = false;
try
{
string recievedData = SendATCommand(port,"AT", 300, "No phone connected");
string command = "AT+CMGF=1" + char.ConvertFromUtf32(13);
recievedData = SendATCommand(port,command, 300, "Failed to set message format.");
// AT Command Syntax - http://www.smssolutions.net/tutorials/gsm/sendsmsat/
command = "AT+CMGS=\"" + phoneNo + "\"" + char.ConvertFromUtf32(13);
recievedData = SendATCommand(port, command, 300,
"Failed to accept phoneNo");
command = message + char.ConvertFromUtf32(26);
recievedData = SendATCommand(port, command, 3000,
"Failed to send message"); //3 seconds
if (recievedData.EndsWith("\r\nOK\r\n"))
isSend = true;
else if (recievedData.Contains("ERROR"))
isSend = false;
return isSend;
}
catch (Exception ex)
{
throw ex;
}
}