private static MessageResult SendSMS(long passportId, string phone, string content) { phone.AssertNotNull("phone"); content.AssertNotNull("content"); if (false == new Regex(@"1\d{10}").IsMatch(phone)) { return(MessageResult.FailedResult("-1", "手机号不正确")); } const string urlpattern = "http://sdk.entinfo.cn/webservice.asmx/mdSmsSend?sn=SDK-BBX-010-23630&pwd=AF01B559B6B6CD0D1D259CC41B255347&mobile={0}&content={1}&ext=1&stime=&rrid=&msgfmt="; var url = string.Format(urlpattern, phone.Trim(), HttpUtility.UrlEncode(content, Encoding.GetEncoding("gb2312"))); using (var client = new WebClient()) { var stream = client.OpenRead(new Uri(url)); if (stream != null) { var reader = new StreamReader(stream); var readResult = reader.ReadToEnd(); var re = new Regex(@"\-?\d{5,32}").Match(readResult).Value; if (!string.IsNullOrEmpty(re) && re.Length > 6) { return(MessageResult.SuccessResult()); } else { return(MessageResult.FailedResult("-9", readResult)); } } return(MessageResult.FailedResult("-9")); } }
public static MessageResult SendTempleteSMS(string phone, string templeteId, string[] arsg) { phone.AssertNotNull("phone"); templeteId.AssertNotNull("templeteId"); arsg.AssertNotNull("arsg"); if (false == new Regex(@"1\d{10}").IsMatch(phone)) { return(MessageResult.FailedResult("-1", "手机号不正确")); } var apiClient = new CCPRestSDK.CCPRestSDK(); bool isInit = apiClient.init("app.cloopen.com", "8883"); apiClient.setAccount(SmsYTX_AccountSId, SmsYTX_AccountToken); apiClient.setAppId(SmsYTX_AppId); try { if (isInit) { Dictionary <string, object> retData = apiClient.SendTemplateSMS(phone, templeteId, arsg); if (null != retData && retData.ContainsKey("statusCode") && retData["statusCode"].ToString() == "000000") { return(MessageResult.SuccessResult()); } else { if (null != retData && retData.ContainsKey("statusCode")) { return(MessageResult.FailedResult(retData["statusCode"].ToString(), retData["statusMsg"].ToString())); } else { return(MessageResult.FailedResult("-9", "发送失败")); } } } else { return(MessageResult.FailedResult("-1", "初始化失败")); } } catch (Exception exc) { return(MessageResult.FailedResult("-" + exc.GetType().Name, exc.Message)); } finally { //Response.Write(ret); } }
/// <summary> /// /// </summary> /// <param name="account"></param> /// <param name="clientIP"></param> /// <param name="phone"></param> /// <returns></returns> public static MessageResult SendSMSValidationCode(AnonymousAccount account, string clientIP, string phone) { var validationCode = GenerateAuthCode(6); if (false == CheckSMSSecurityStrategy(account, clientIP, phone)) { return(MessageResult.FailedResult("-1", "短信发送受限")); } var cache = CacheManager.GetCache(ModuleEnvironment.SMSCacheName); cache.Set(phone, validationCode); MessageResult result = null; if (SmsRegister.Contains("{0}")) { var smsContent = string.Format(SmsRegister, validationCode); result = SendSMS(account.PassportId, phone, smsContent); var smsMessage = new SmsMessage() { PassportId = account.PassportId, MobilePhone = phone, Content = smsContent, SendStatus = result.Success ? "Success" : "Failed", FailedReason = null == result.ErrorMessage ? result.ErrorCode : result.ErrorMessage }; smsMessage.Save(); } else { result = SendTempleteSMS(phone, SmsRegister, new string[] { validationCode, SmsIntervalMinutes.ToString() }); var smsMessage = new SmsMessage() { PassportId = account.PassportId, MobilePhone = phone, Content = string.Format("验证码:{0}, 短信模板:{1}", validationCode, SmsRegister), SendStatus = result.Success ? "Success" : "Failed", FailedReason = null == result.ErrorMessage ? result.ErrorCode : result.ErrorMessage }; smsMessage.Save(); } LogManager.GetLogger().Info("SendSMSValidationCode[{0}] => {1} : {2}", result.Success, phone, validationCode); return(result); }