万商超信
阿里云短信群发平台怎么用(net core 教程)
2021-11-20 14:04
[阿里云短信群发平台怎么用(net core 教程)]。
一、引用阿里云短信官方的包
二、调用下面的方法
public bool AliMessageCode(string phone, string val)
{
var msg = "";
MessageCodeDto strJson = new MessageCodeDto();
//注意刚刚下载的AccessKey的excel中的accessKeyId和accessSecret填入
IClientProfile profile = DefaultProfile.GetProfile("default", "accesskey", "accesssecret");
DefaultAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.Method = MethodType.POST;
request.Domain = "dysmsapi.aliyuncs.com";
request.Version = "2017-05-25";
request.Action = "SendSms";
// request.Protocol = ProtocolType.HTTP;
request.AddQueryParameters("PhoneNumbers", phone);
request.AddQueryParameters("SignName", "你的模板名称");
request.AddQueryParameters("TemplateCode", "模板id");
MessageCodeRequest messageCode = new MessageCodeRequest()
{
code = val
};
var code = JsonHelper.SerializeObject(messageCode);
request.AddQueryParameters("TemplateParam", code); //需为json格式
// request.Protocol = ProtocolType.HTTP;
try
{
CommonResponse response = client.GetCommonResponse(request);
msg = Encoding.Default.GetString(response.HttpResponse.Content);
strJson = JsonHelper.DeserializeJsonToObject<MessageCodeDto>(msg);
}
catch (ServerException e)
{
msg = e.ErrorMessage;
}
catch (ClientException e)
{
msg = e.ErrorMessage;
}
if (strJson.Code == "OK")
{
return true;
}
else
{
return false;
}
}
三、调用
/// <summary>
/// 短信推送
/// </summary>
/// <param name="phone">手机号/支持多个(阿里云文档写最多1000条,用","隔开)</param>
/// <param name="code">验证码(自定义)</param>
/// <returns></returns>
if (!_msg.AliMessageCode(phone, code)
{
/*获取验证码失败*/
sysCode = SysCode.GetCodeErr;
}