/// <summary> /// 发送短信消息,参数类型为统一的邮件信息参数 /// </summary> /// <param name="destObj">类型为LibMailParam的List数组</param> public static void SendMsgByMailList(object destObj) { try { List <LibMailParam> sendMailParamList = destObj as List <LibMailParam>; if (sendMailParamList == null || sendMailParamList.Count == 0) { return; } sendMailParamList.ForEach(mailParam => { if (mailParam == null) { return; } SendSMSParam smsParam = GetSendParam(mailParam, false); if (smsParam == null || string.IsNullOrEmpty(smsParam.Message) || string.IsNullOrEmpty(smsParam.Message.TrimEnd()) || smsParam.PhoneList == null || smsParam.PhoneList.Count == 0) { return; } SendMsg(smsParam); }); } catch (Exception) { // to do log } }
/// <summary> /// 发送短信消息 /// </summary> /// <param name="destObj">参数设置为object是为了ThreadPool.QueueUserWorkItem调用,具体类型为SendSMSParam</param> public static void SendMsg(object destObj) { try { if (destObj == null) { return; } //如果有事件处理绑定到了此方法则不再使用AX框架中默认的发送短信方法 if (CustomSendSmsMsg != null) { CustomSendSmsMsg(destObj as SendSMSParam); return; } ILibSMSService svc = new LibSMSService(); SendSMSParam param = destObj as SendSMSParam; if (param != null) { svc.SendMsg(param); } } catch (Exception) { // to do log } }
public static void SendMsg(object destObj) { ILibSMSService svc = new LibHYSMSService(); SendSMSParam param = destObj as SendSMSParam; if (param != null) { svc.SendMsg(param); } }
/// <summary> /// 发送微信消息 /// </summary> /// <param name="destObj">类型为SendSMSParam</param> public static void SendWeiXinMsg(object destObj) { try { ILibSMSService svc = new LibWinXinService(); SendSMSParam param = destObj as SendSMSParam; if (param != null) { svc.SendMsg(param); } } catch (Exception) { // to do log } }
public static Result GenerateCode(string userId, string phoneNo) { Result res = new Result(); try { // 生成四位数的验证码 Random r = new Random(); int i = (int)(r.NextDouble() * 10000); string code = i.ToString().PadLeft(4, '0'); // 查看是否存在该帐号对应的验证码 LibDataAccess access = new LibDataAccess(); string sql = string.Format("SELECT count(*) FROM AXPVERIFYCODE WHERE USERID = {0}", LibStringBuilder.GetQuotString(userId)); int count = LibSysUtils.ToInt32(access.ExecuteScalar(sql)); // 生成或者更新验证码 if (count == 0) { sql = string.Format("INSERT INTO AXPVERIFYCODE(USERID, VERIFYCODE, TIME) VALUES({0}, {1}, {2})", LibStringBuilder.GetQuotString(userId), LibStringBuilder.GetQuotString(code), LibDateUtils.GetCurrentDateTime()); } else if (count == 1) { sql = string.Format("UPDATE AXPVERIFYCODE SET VERIFYCODE={0}, TIME={1} WHERE USERID={2}", LibStringBuilder.GetQuotString(code), LibDateUtils.GetCurrentDateTime(), LibStringBuilder.GetQuotString(userId)); } access.ExecuteNonQuery(sql); // 发送短信 SendSMSParam sendSMSParam = new SendSMSParam(); sendSMSParam.Message = "验证码为:" + code; sendSMSParam.PhoneList.Add(phoneNo); LibSMSHelper.SendMsg(sendSMSParam); res.ReturnValue = true; res.Message = "成功!"; } catch (Exception ex) { res.ReturnValue = false; res.Message = "失败!" + ex.Message; } return(res); }
public void SendMsg(SendSMSParam param) { if (param.PhoneList == null || param.PhoneList.Count == 0) { throw new ArgumentNullException("接收号码为空值"); } try { WeiXinCorp wxmsg = new WeiXinCorp(); StringBuilder builder = new StringBuilder(); foreach (string phone in param.PhoneList) { builder.AppendFormat("{0}|", phone); } builder.Remove(builder.Length - 1, 1); wxmsg.ToUserID = builder.ToString(); string phoneStr = builder.ToString(); string message = param.Message.Trim(); string ret = wxmsg.SendMessage(param.Message); if (ret.Length < 6) { string path = System.IO.Path.Combine(AxCRL.Comm.Runtime.EnvProvider.Default.MainPath, "Output", "Error", "SMS", string.Format("{0}.txt", DateTime.Now.Ticks)); using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create)) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fs)) { sw.Write(string.Format("发送失败,错误码:{0}", ret)); } } } } catch (Exception ex) { string path = System.IO.Path.Combine(AxCRL.Comm.Runtime.EnvProvider.Default.MainPath, "Output", "Error", "SMS", string.Format("{0}.txt", DateTime.Now.Ticks)); using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create)) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fs)) { sw.Write(ex); } } } }
private static SendSMSParam GetSendParam(LibMailParam sendMailParam, bool isWeixin) { if (sendMailParam == null || string.IsNullOrEmpty(sendMailParam.Content) || string.IsNullOrEmpty(sendMailParam.Content.TrimEnd())) { return(null); } List <string> weixinList = new List <string>(); List <string> phoneList = new List <string>(); phoneList = GetSendPhoneList(sendMailParam, out weixinList); SendSMSParam smsParam = new SendSMSParam() { Message = sendMailParam.Content.TrimEnd(), PhoneList = (isWeixin) ? weixinList : phoneList }; return(smsParam); }
/// <summary> /// 功能单据 发送系统处理消息、短信处理信息、微信处理信息 /// </summary> /// <param name="billMasterRow">功能单据 主表行数据</param> /// <param name="billProgId">功能单据 功能标识</param> /// <param name="title">消息标题</param> /// <param name="content">消息内容</param> /// <param name="personIds">额外发送人员,可以为空</param> public void SendBusinessMsg(DataRow billMasterRow, string billProgId, string title, string content, List <string> personIds) { string data = GetMsgData(billMasterRow); //系统消息类,用于发送系统消息,其属性用户列表中的用户不可重复 LibSysNews news = new LibSysNews(); //电话列表,用于发送短信,列表中手机号不可重复 List <string> phoneList = new List <string>(); //微信列表,用于发送微信,列表中微信号不可重复 List <string> weChatList = new List <string>(); #region 为额外需要发送信息的人发短信、发微信、发系统信息 if (personIds != null && personIds.Count > 0) { //获取人员信息【人员代码,微信,电话】 string sql = "SELECT PERSONID,WECHAT,PHONENO FROM COMPERSON WHERE PERSONID in("; foreach (string personId in personIds) { sql = sql + string.Format("{0},", LibStringBuilder.GetQuotString(personId)); } sql = sql.TrimEnd(',') + ")"; using (IDataReader reader = this.DataAccess.ExecuteDataReader(sql)) { while (reader.Read()) { //将人员手机(手机号存在)添加到电话列表中,以便发短信 string phoneNo = LibSysUtils.ToString(reader["PHONENO"]); if (!string.IsNullOrEmpty(phoneNo) && !phoneList.Contains(phoneNo)) { phoneList.Add(phoneNo); } //将人员微信(微信号存在,若不存在则将其手机号作为微信号)添加到微信列表中,以便发微信 string weChat = LibSysUtils.ToString(reader["WECHAT"]); if (!string.IsNullOrEmpty(weChat)) { if (!weChatList.Contains(weChat)) { weChatList.Add(weChat); } } else if (!string.IsNullOrEmpty(phoneNo) && !weChatList.Contains(phoneNo)) { weChatList.Add(phoneNo); } //将人员添加到其用户列表中,以便发系统信息 string personId = LibSysUtils.ToString(reader["PERSONID"]); if (!string.IsNullOrEmpty(personId) && !news.UserList.Contains(personId)) { news.UserList.Add(personId); } } } } #endregion #region 获得单据业务流配置下跟当前功能单据数据相匹配的业务流配置明细并为其接收人(根据需要)发短信/发微信、发系统信息 List <BusinessInfo> curBusinessInfo = GetBusinessInfo(billMasterRow, billProgId); if (curBusinessInfo != null) { foreach (var info in curBusinessInfo) { //将接收人手机(需要接受人接收短信且手机号存在)添加到电话列表中,以便发短信 if (info.NeedSMS && !string.IsNullOrEmpty(info.PhoneNo) && !phoneList.Contains(info.PhoneNo)) { phoneList.Add(info.PhoneNo); } //将接收人微信(需要接收人接受微信且微信号存在,若不存在则将其手机号作为微信号)添加到微信列表中,以便发微信 if (info.SendWeChat) { if (!string.IsNullOrEmpty(info.WeChat)) { if (!weChatList.Contains(info.WeChat)) { weChatList.Add(info.WeChat); } } else if (!string.IsNullOrEmpty(info.PhoneNo)) { if (!weChatList.Contains(info.PhoneNo)) { weChatList.Add(info.PhoneNo); } } } //将接收人添加到其用户列表中,以便发系统信息 if (!news.UserList.Contains(info.PersonId)) { news.UserList.Add(info.PersonId); } } } #endregion #region 发送系统信息 if (news.UserList.Count > 0) { news.Title = title; news.Content = content; news.Data = data; news.PersonId = LibSysUtils.ToString(billMasterRow["CREATORID"]); news.ProgId = billProgId; LibSysNewsHelper.SendNews(news); } #endregion #region 发送短信 if (phoneList.Count > 0) { SendSMSParam param = new SendSMSParam(); param.PhoneList = phoneList; param.Message = news.Content; //ThreadPool.QueueUserWorkItem(LibSMSHelper.SendMsg, param); } #endregion #region 发送微信 if (weChatList.Count > 0) { SendSMSParam param = new SendSMSParam(); param.PhoneList = weChatList; param.Message = news.Content; ThreadPool.QueueUserWorkItem(new WaitCallback(LibSMSHelper.SendWeiXinMsg), param); } #endregion }
void ILibSMSService.SendMsg(SendSMSParam param) { if (param.PhoneList == null || param.PhoneList.Count == 0) { throw new ArgumentNullException("短信接收号码为空值"); } try { StringBuilder builder = new StringBuilder(); foreach (string phone in param.PhoneList) { builder.AppendFormat("{0},", phone); } builder.Remove(builder.Length - 1, 1); string phoneStr = builder.ToString(); SMSProvider provider = EnvProvider.Default.SMSProvider; //SMS.wmgwSoapClient client = new SMS.wmgwSoapClient();//调用的方法 //SMSProvider provider = EnvProvider.Default.SMSProvider;//提供必要参数信息:Host(地址),SMSSys(用户名),SMSPwd,Port(端口) if (provider != null && !string.IsNullOrEmpty(provider.SMSSys)) { string message = "【" + provider.SMSSign + "】" + param.Message.Trim(); //string strparam = "action=send&userid=" + param.UserId + "&account=" + param.Account + "&password="******"&content=" + message + "&mobile=" + phoneStr + "&sendtime="; string strparam = string.Format("action=send&userid={0}&account={1}&password={2}&content={3}&mobile={4}&sendtime=", provider.SMSUserId, provider.SMSSys, provider.SMSPwd, message, phoneStr); byte[] bs = Encoding.GetEncoding("GB2312").GetBytes(strparam); string straddress = "http://" + provider.Host + ":" + provider.Port.ToString() + "/smsGBK.aspx"; HttpWebRequest postSmsInfoReq = (HttpWebRequest)HttpWebRequest.Create(straddress); postSmsInfoReq.Method = "POST"; postSmsInfoReq.ContentType = "application/x-www-form-urlencoded";//头 postSmsInfoReq.ContentLength = bs.Length; using (Stream reqStream = postSmsInfoReq.GetRequestStream()) { reqStream.Write(bs, 0, bs.Length); } //string port = provider.Port == 0 ? "*" : provider.Port.ToString(); //string ret = client.MongateCsSpSendSmsNew(provider.SMSSys, provider.SMSPwd, phoneStr, message, // param.PhoneList.Count, port, string.Empty, string.Empty, string.Empty, // string.Empty, string.Empty, string.Empty, 0, string.Empty, string.Empty, 0);//调用方法,返回参数给ret //string ret = string.Empty; //using (WebResponse wr = postSmsInfoReq.GetResponse()) //{ // StreamReader sr = new StreamReader(wr.GetResponseStream(), System.Text.Encoding.Default); // ret = sr.ReadToEnd().Trim(); //} //if (ret.Length < 6)//txt文件保存错误信息 //{ // string path = System.IO.Path.Combine(AxCRL.Comm.Runtime.EnvProvider.Default.MainPath, "Output", "Error", "SMS", string.Format("{0}.txt", DateTime.Now.Ticks));//将字符串组成一个路径 // using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create)) // { // using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fs)) // { // sw.Write(string.Format("发送失败,错误码:{0}", ret)); // } // } //} SendSmsResult ret = null; using (WebResponse wr = postSmsInfoReq.GetResponse()) { StreamReader sr = new StreamReader(wr.GetResponseStream(), System.Text.Encoding.Default); ret = ReadParseRetXml(sr); } if (ret != null && ret.ReturnStatus == false) { string path = Path.Combine(AxCRL.Comm.Runtime.EnvProvider.Default.MainPath, "Output", "Error", "SMS");//将字符串组成一个路径 if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } path = Path.Combine(path, string.Format("{0}.txt", DateTime.Now.Ticks)); using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create)) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fs)) { sw.Write(string.Format("{0}:发送短信失败,{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ret.ToString())); } } } } } catch (Exception ex) { string path = Path.Combine(AxCRL.Comm.Runtime.EnvProvider.Default.MainPath, "Output", "Error", "SMS");//将字符串组成一个路径 if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } path = Path.Combine(path, string.Format("{0}.txt", DateTime.Now.Ticks)); using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create)) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter(fs)) { sw.Write(string.Format("{0}:发送短信失败,异常:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), ex)); } } } }