private void SmsReceiver_MessageCallbackEvent(String number, String body) { if (helper == null) { return; } var config = helper.GetConfig(); if (config.Count == 0) { return; } helper.AddAppLog("[event]:" + (number + "->" + body)); #region try { #region --代发-- var controlphone = config["controlphone"] ?? "";//该手机号不转发 if (!String.IsNullOrEmpty(controlphone) && controlphone.Length > 7 && number.Trim('+').EndsWith(controlphone.Trim('+'))) { //if (body.StartsWith("#") && body.EndsWith("#")) //{//指令区 var cmd = body.Trim().Trim('#'); var param = cmd.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries); if (param.Length > 1) { SMSHelper.SendPhone(param[0].Trim(), param[1].Trim()); helper.UpdateSendStatistics("sendsuccess"); helper.AddAppLog("[controlcomplete]:" + body); } else { helper.AddAppLog("[controlerror]接收控制指令:" + cmd); } //} return; } #endregion #region --转发-- var contactList = helper.GetContact(); var content = config["messagetemplate"].Replace("#from#", number).Replace("#body#", body); foreach (var contact in contactList) { if (contact.Contains("@")) { #region email EmailHelper.Init(config["smtpserver"], config["smtpaccount"], config["smtppassword"], config["smtpaccount"], Convert.ToInt32(config["smtpport"] ?? "0")); var thread = new System.Threading.Thread(new System.Threading.ThreadStart(() => { try { EmailHelper.Send(string.Format("sms {0}", number), contact, content, null, false); helper.UpdateSendStatistics("sendsuccess"); helper.AddAppLog("[emailcomplete]:" + (contact + "->" + content)); } catch (Exception ex) { helper.UpdateSendStatistics("sendfailure"); helper.AddAppLog("[emailerror]:" + (contact + "->" + content + "->" + ex.Message)); } })); thread.Name = "sendmessagethread"; thread.IsBackground = true; thread.Start(); #endregion } else { SMSHelper.SendPhone(contact, content); helper.UpdateSendStatistics("sendsuccess"); helper.AddAppLog("[smscomplete]:" + (contact + "->" + content)); } System.Threading.Thread.Sleep(100); } #endregion } catch (Exception ex) { helper.UpdateSendStatistics("sendfailure"); helper.AddAppLog("[error]发送失败:" + ex.Message + "(" + body + ")"); } #endregion }
private void SendMessage(string to, string body) { SMSHelper.SendPhone(to, body); helper.UpdateSendStatistics("sendsuccess"); }