要想发送短信通知,必须要申请通用的签名,不能使用验证码签名,申请通用签名需要三证合一证件。
签名申请通过之后,还要去申请通知模板。
这两样东西申请通过了接下来看代码(阿里云申请的具体操作流程请看上章短信验证码操作):
1、在View页面给个按钮点击发送短信通知,输入接收人的手机号发送到对方手里。
<div class="weui-cells weui-cells_form">
<form id="NoteLogin" method="post">
<div class="weui-cell">
<div class="weui-cell__hd">
<label class="weui-label">手机号</label>
</div>
<div class="weui-cell__bd">
<input class="weui-input f15" placeholder="请输入手机号" type="text" name="PhoneNum" id="phone" autocomplete="off">
</div>
</div>
</form>
</div>
<div class="weui-btn-area" style="margin-top: 8px;">
<a class="weui-btn" href="javascript:" style="font-size:16px;background:#206edf;" onclick="OpenStudentCode($('#phone').val())">发送短信通知</a>
</div>
jQuery:
//学生短信催费
function OpenStudentCode(phone) {
layer.confirm('确定给学生发送短信催费吗?', { icon: 3, title: '提示' }, function () {
$.ajax({
type: "POST", //用POST方式传输
dataType: "text", //数据格式:JSON
url: 'DXCF', //请求路径
data: { PerID: PerID, phone: phoneNumber },
error: function (XMLHttpRequest, textStatus, errorThrown) {
layer.msg("发送短信失败!", { icon: 0, skin: "layui-layer-molv" });
},
success: function (msg) {
if (msg == "ok") {
layer.msg("发送短信成功!", { icon: 1, skin: "layui-layer-molv" });
} else {
layer.msg("发送短信失败!", { icon: 0, skin: "layui-layer-molv" });
}
}
})
});
}
2、控制器:
public void JZDXCF()
{
using (var scope = new TransactionScope())
{
String product = "Dysmsapi";//短信API产品名称(短信产品名固定,无需修改)
String domain = "dysmsapi.aliyuncs.com";//短信API产品域名(接口地址固定,无需修改)
String accessKeyId = "";//你的accessKeyId,参考本文档步骤2
String accessKeySecret = "";//你的accessKeySecret,参考本文档步骤2
IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
string mobile = Request["phone"];
//必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式,发送国际/港澳台消息时,接收号码格式为00+国际区号+号码,如“0085200000000”
request.PhoneNumbers = mobile;
//必填:短信签名-可在短信控制台中找到
request.SignName = "";
//必填:短信模板-可在短信控制台中找到,发送国际/港澳台消息时,请使用国际/港澳台短信模版
request.TemplateCode = "";
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},........."时,此处的值为
request.TemplateParam = "{\"name\":\"阿里云\"}";
//请求失败这里会抛ClientException异常
try
{
SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);
Response.Write("ok");
//提交事务
scope.Complete();
}
catch (ServerException er)
{
Response.Write("er");
Console.WriteLine(er);
}
}
}
效果图: