protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.SMSTransferConfigLayout);
            Title = "发送配置";
            // Create your application here
            var helper = new ExtSQLiteHelper(this);
            Dictionary <string, string> dic;
            var templatetext     = FindViewById <EditText>(Resource.Id.text_template);
            var smtpservertext   = FindViewById <EditText>(Resource.Id.text_smtpserver);
            var smtpporttext     = FindViewById <EditText>(Resource.Id.text_smtpport);
            var smtpaccounttext  = FindViewById <EditText>(Resource.Id.text_smtpaccount);
            var smtppasswordtext = FindViewById <EditText>(Resource.Id.text_smtppassword);
            var controlphonetext = FindViewById <EditText>(Resource.Id.text_controlphone);

            #region
            dic = helper.GetConfig();
            templatetext.Text     = dic.ContainsKey("messagetemplate") ? dic["messagetemplate"] : "";
            smtpservertext.Text   = dic.ContainsKey("smtpserver") ? dic["smtpserver"] : "";
            smtpporttext.Text     = dic.ContainsKey("smtpport") ? dic["smtpport"] : "25";
            smtpaccounttext.Text  = dic.ContainsKey("smtpaccount") ? dic["smtpaccount"] : "";
            smtppasswordtext.Text = dic.ContainsKey("smtppassword") ? dic["smtppassword"] : "";
            controlphonetext.Text = dic.ContainsKey("controlphone") ? dic["controlphone"] : "";
            #endregion

            #region
            FindViewById <Button>(Resource.Id.save_config_button).Click += (sender, e) =>
            {
                dic = new Dictionary <string, string>();
                dic.Add("messagetemplate", templatetext.Text);
                dic.Add("smtpserver", smtpservertext.Text);
                dic.Add("smtpport", smtpporttext.Text);
                dic.Add("smtpaccount", smtpaccounttext.Text);
                dic.Add("smtppassword", smtppasswordtext.Text);
                dic.Add("controlphone", controlphonetext.Text.Trim().TrimStart('+'));
                helper.SaveConfig(dic);

                Toast.MakeText(this, "完成", ToastLength.Short).Show();
            };
            FindViewById <Button>(Resource.Id.cancel_config_button).Click += (sender, e) =>
            {
                this.Finish();
            };
            #endregion
        }
Example #2
0
        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
        }