4 changed files with 106 additions and 0 deletions
-
6src/Shentun.Sms.HttpApi.Host/appsettings.json
-
1src/Shentun.Sms.Service/Shentun.Sms.Service.csproj
-
98src/Shentun.Sms.Service/Sms/AliyunSms.cs
-
1src/Shentun.Sms.Service/Sms/SmsBase.cs
@ -0,0 +1,98 @@ |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.Logging; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using TencentCloud.Common.Profile; |
|||
using TencentCloud.Common; |
|||
using Tea; |
|||
using Tea.Utils; |
|||
|
|||
namespace Shentun.Sms.Service.Sms |
|||
{ |
|||
public class AliyunSms : SmsBase |
|||
{ |
|||
private readonly IConfiguration _aliyunSmsConfig; |
|||
private readonly ILogger<AliyunSms> _logger; |
|||
|
|||
public AliyunSms( |
|||
IConfiguration aliyunSmsConfig, |
|||
ILogger<AliyunSms> logger) |
|||
{ |
|||
_aliyunSmsConfig = aliyunSmsConfig; |
|||
_logger = logger; |
|||
} |
|||
|
|||
/// <summary>
|
|||
///
|
|||
/// </summary>
|
|||
/// <param name="phoneNumbers">手机号前+86</param>
|
|||
/// <param name="templateParam"></param>
|
|||
public override void Send(string[] phoneNumbers, string[] templateParam) |
|||
{ |
|||
AlibabaCloud.SDK.Dysmsapi20170525.Client client = CreateClient(); |
|||
AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest |
|||
{ |
|||
PhoneNumbers = phoneNumbers[0], |
|||
SignName = _aliyunSmsConfig["AliyunSms:SignName"], |
|||
TemplateCode = _aliyunSmsConfig["AliyunSms:TemplateCode"], |
|||
TemplateParam = "{\"codestr\":\"2222\"}" |
|||
}; |
|||
try |
|||
{ |
|||
// 复制代码运行请自行打印 API 的返回值
|
|||
var msg = client.SendSmsWithOptions(sendSmsRequest, new AlibabaCloud.TeaUtil.Models.RuntimeOptions()); |
|||
|
|||
_logger.LogInformation($"------短信发送记录=>{AlibabaCloud.TeaUtil.Common.AssertAsString(msg)}------当前时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}"); |
|||
} |
|||
catch (TeaException error) |
|||
{ |
|||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|||
// 错误 message
|
|||
Console.WriteLine(error.Message); |
|||
// 诊断地址
|
|||
Console.WriteLine(error.Data["Recommend"]); |
|||
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message); |
|||
} |
|||
catch (Exception _error) |
|||
{ |
|||
TeaException error = new TeaException(new Dictionary<string, object> |
|||
{ |
|||
{ "message", _error.Message } |
|||
}); |
|||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|||
// 错误 message
|
|||
Console.WriteLine(error.Message); |
|||
// 诊断地址
|
|||
Console.WriteLine(error.Data["Recommend"]); |
|||
AlibabaCloud.TeaUtil.Common.AssertAsString(error.Message); |
|||
} |
|||
|
|||
} |
|||
|
|||
/** |
|||
* 使用AK&SK初始化账号Client |
|||
* @return Client |
|||
* @throws Exception |
|||
*/ |
|||
public AlibabaCloud.SDK.Dysmsapi20170525.Client CreateClient() |
|||
{ |
|||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
|||
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378671.html。
|
|||
AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config |
|||
{ |
|||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
|||
AccessKeyId = _aliyunSmsConfig["AliyunSms:AccessKeyId"], |
|||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
|||
AccessKeySecret = _aliyunSmsConfig["AliyunSms:AccessKeySecret"] |
|||
}; |
|||
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
|
|||
config.Endpoint = "dysmsapi.aliyuncs.com"; |
|||
return new AlibabaCloud.SDK.Dysmsapi20170525.Client(config); |
|||
} |
|||
|
|||
|
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue