public AliyunSmsClient() { var aliyunSmsSettting = AliyunSmsHelper.GetSettingsFunc(); if (aliyunSmsSettting == null) { throw new SmsException("请配置短信服务相关配置!"); } if (string.IsNullOrWhiteSpace(aliyunSmsSettting.SignName)) { throw new SmsException("签名不能为空!"); } try { AliyunSmsHelper.LoggerAction("Debug", "SmsSettings:" + JsonConvert.SerializeObject(aliyunSmsSettting)); this.AliyunSmsSettting = aliyunSmsSettting; var product = aliyunSmsSettting.Product; //短信API产品名称(短信产品名固定,无需修改) var domain = aliyunSmsSettting.Domain; //短信API产品域名(接口地址固定,无需修改) var accessKeyId = aliyunSmsSettting.AccessKeyId; //你的accessKeyId,参考本文档步骤2 var accessKeySecret = aliyunSmsSettting.AccessKeySecret; //你的accessKeySecret,参考本文档步骤2 var profile = DefaultProfile.GetProfile(aliyunSmsSettting.RegionId, accessKeyId, accessKeySecret); DefaultProfile.AddEndpoint(aliyunSmsSettting.EndpointName, aliyunSmsSettting.RegionId, product, domain); AcsClient = new DefaultAcsClient(profile); AliyunSmsHelper.LoggerAction("Debug", "已创建AcsClient。"); } catch (Exception ex) { AliyunSmsHelper.LoggerAction("Error", ex.ToString()); throw new SmsException("配置出错,请检查配置!"); } }
public Task <SendResult> SendCodeAsync(string phone, string code) { if (string.IsNullOrWhiteSpace(phone)) { throw new SmsException("手机号码不能为空!"); } if (string.IsNullOrWhiteSpace(code)) { throw new SmsException("验证码不能为空!"); } var client = new AliyunSmsClient(); var acsClient = client.AcsClient; var request = new SendSmsRequest(); var result = new SendResult(); try { //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式 request.PhoneNumbers = phone; //必填:短信签名-可在短信控制台中找到 request.SignName = client.AliyunSmsSettting.SignName; //必填:短信模板-可在短信控制台中找到 request.TemplateCode = client.AliyunSmsSettting.TemplateCode; //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为 request.TemplateParam = string.Format(client.AliyunSmsSettting.TemplateParam, code); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 request.OutId = client.AliyunSmsSettting.OutId; //请求失败这里会抛ClientException异常 var sendSmsResponse = acsClient.GetAcsResponse(request); //发送成功判断 if ("OK".Equals(sendSmsResponse.Code, StringComparison.CurrentCultureIgnoreCase)) { result.Success = true; } else { result.Success = false; result.ErrorMessage = sendSmsResponse.Message; } } catch (ClientException e) { AliyunSmsHelper.LoggerAction("Error", e.ToString()); result.Success = false; result.ErrorMessage = e.ErrorMessage; } catch (Exception e) { AliyunSmsHelper.LoggerAction("Error", e.ToString()); result.Success = false; result.ErrorMessage = e.Message; } return(Task.FromResult(result)); }
/// <summary> /// 批量发送消息模板 /// </summary> /// <param name="input">模板消息</param> /// <returns></returns> public Task <SmsResult> SendBatchTemplateMessageAsync(SendBatchTemplateMessageInput input) { var client = new AliyunSmsClient(); var acsClient = client.AcsClient; var request = new SendBatchSmsRequest(); var result = new SmsResult(); try { //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式 request.PhoneNumberJson = JsonConvert.SerializeObject(input.SendTemplateMessageInputs.Select(a => a.PhoneNumber).ToList()); //必填:短信签名-可在短信控制台中找到 request.SignNameJson = JsonConvert.SerializeObject(input.SendTemplateMessageInputs.Select(a => a.PhoneNumber).ToList()); //必填:短信模板-可在短信控制台中找到 request.TemplateCode = input.TemplateCode; //可选:模板中的变量替换JSON串 if (input.HaveParm) { request.TemplateParamJson = JsonConvert.SerializeObject(input.SendTemplateMessageInputs.Select(a => a.ToString()).ToList()); } //请求失败这里会抛ClientException异常 var sendSmsResponse = acsClient.GetAcsResponse(request); //发送成功判断 if ("OK".Equals(sendSmsResponse.Code, StringComparison.CurrentCultureIgnoreCase)) { result.Success = true; } else { result.Success = false; result.ErrorMessage = sendSmsResponse.Message; } } catch (ClientException e) { AliyunSmsHelper.LoggerAction("Error", e.ToString()); result.Success = false; result.ErrorMessage = e.ErrorMessage; } catch (Exception e) { AliyunSmsHelper.LoggerAction("Error", e.ToString()); result.Success = false; result.ErrorMessage = e.Message; } return(Task.FromResult(result)); }
/// <summary> /// 修改短信模板。 /// </summary> /// <param name="input"></param> /// <returns></returns> public Task <SmsResult> ModifySmsTemplateAsync(ModifySmsTemplateInput input) { var client = new AliyunSmsClient(); var acsClient = client.AcsClient; var request = new ModifySmsTemplateRequest(); var result = new SmsResult(); try { request.TemplateCode = input.TemplateCode; request.TemplateType = input.TemplateType; request.TemplateName = input.TemplateName; request.TemplateContent = input.TemplateContent; request.Remark = input.Remark; //请求失败这里会抛ClientException异常 var ModifySmsTemplateResponse = acsClient.GetAcsResponse(request); //发送成功判断 if ("OK".Equals(ModifySmsTemplateResponse.Code, StringComparison.CurrentCultureIgnoreCase)) { result.Success = true; } else { result.Success = false; result.ErrorMessage = ModifySmsTemplateResponse.Message; } } catch (ClientException e) { AliyunSmsHelper.LoggerAction("Error", e.ToString()); result.Success = false; result.ErrorMessage = e.ErrorMessage; } catch (Exception e) { AliyunSmsHelper.LoggerAction("Error", e.ToString()); result.Success = false; result.ErrorMessage = e.Message; } return(Task.FromResult(result)); }
/// <summary> /// 查看短信发送记录和发送状态。 /// </summary> /// <param name="input"></param> /// <returns></returns> public Task <QuerySendDetailsResult> QuerySendDetailsAsync(QuerySendDetailsInput input) { var client = new AliyunSmsClient(); var acsClient = client.AcsClient; var request = new QuerySendDetailsRequest(); var result = new QuerySendDetailsResult(); try { request.PhoneNumber = input.PhoneNumber; request.SendDate = input.SendDate.ToString("yyyyMMDD"); request.PageSize = input.PageSize; request.CurrentPage = input.CurrentPage; //请求失败这里会抛ClientException异常 var querySendDetailsResponse = acsClient.GetAcsResponse(request); //发送成功判断 if ("OK".Equals(querySendDetailsResponse.Code, StringComparison.CurrentCultureIgnoreCase)) { result.Success = true; result.SmsSendDetailDTOs = querySendDetailsResponse.SmsSendDetailDTOs; } else { result.Success = false; result.ErrorMessage = querySendDetailsResponse.Message; } } catch (ClientException e) { AliyunSmsHelper.LoggerAction("Error", e.ToString()); result.Success = false; result.ErrorMessage = e.ErrorMessage; } catch (Exception e) { AliyunSmsHelper.LoggerAction("Error", e.ToString()); result.Success = false; result.ErrorMessage = e.Message; } return(Task.FromResult(result)); }
/// <summary> /// 新增短信签名。 /// </summary> /// <param name="input"></param> /// <returns></returns> public Task <SmsResult> AddSmsSignAsync(AddSmsSignInput input) { var client = new AliyunSmsClient(); var acsClient = client.AcsClient; var request = new AddSmsSignRequest(); var result = new SmsResult(); try { request.SignName = input.SignName; request.SignSource = input.SignSource; request.Remark = input.Remark; request.SignFileLists = input.SignFileLists; //请求失败这里会抛ClientException异常 var AddSmsSignResponse = acsClient.GetAcsResponse(request); //发送成功判断 if ("OK".Equals(AddSmsSignResponse.Code, StringComparison.CurrentCultureIgnoreCase)) { result.Success = true; } else { result.Success = false; result.ErrorMessage = AddSmsSignResponse.Message; } } catch (ClientException e) { AliyunSmsHelper.LoggerAction("Error", e.ToString()); result.Success = false; result.ErrorMessage = e.ErrorMessage; } catch (Exception e) { AliyunSmsHelper.LoggerAction("Error", e.ToString()); result.Success = false; result.ErrorMessage = e.Message; } return(Task.FromResult(result)); }