注意: 签名不要写名字,具体参考签名参考规范
记录里面的值
public class SendSMS
{
private static string domain="dysmsapi.aliyuncs.com" ;
//产品域名
private static string product ="Dysmsapi";
private static string endpointName = "cn-beijing";
private static string regionId = "cn-beijing";
///
/// 发送短信
///
/// 开发者秘钥对,通过阿里云控制台的秘钥管理页面创建与管理
/// 开发者秘钥对,通过阿里云控制台的秘钥管理页面创建与管理
/// 待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
/// 短信签名,可在短信控制台中找到,例如 云通信
/// 短信模板,可在短信控制台中找到,例如 SMS_1000000
/// 模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为,例如 {\"name\":\"小李\",\"code\":\"546523\"}
/// 提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
/// 端点名称
/// 地区名称
///
public static ResultObject
{
ResultObject
Dictionary
try
{
//templateParam = "{\"code\":\"小李\"}";
var Id = Guid.NewGuid();
var outId = Id.ToString();
IClientProfile profile = DefaultProfile.GetProfile(regionId, AccessKeyId, AccessKeySecret);
DefaultProfile.AddEndpoint(endpointName, regionId, product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//该类为了记录发送日志(可不写)
SendInformation Information = new SendInformation()
{
Id = Guid.NewGuid(),
accessKeyId = AccessKeyId,
accessKeySecret = AccessKeySecret,
endpointName = endpointName,
outId = outId,
phoneNumber = phoneNumber,
regionId = regionId,
signName = signName,
State = 1,
templateCode = templateCode,
templateParam = templateParam,
Message = "",
CreateTime=DateTime.Now
};
// 组装请求对象
SendSmsRequest request = new SendSmsRequest
{
PhoneNumbers = phoneNumber,
SignName = signName,
TemplateCode = templateCode,
TemplateParam = templateParam,
OutId = outId
};
SendSmsResponse response = null;
try
{
// 请求失败这里会抛ClientException异常
response = acsClient.GetAcsResponse(request);
if (response.Code == "OK")
{
result.Success = true;
Information.State = 0;
}
else
{
result.Message = response.Message;
Information.Message = response.Message;
}
}
catch (ServerException e)
{
// LogHelper.LogException
Information.Message = e.Message;
Information.State = 1;
result.Message = e.Message;
}
catch (ClientException e)
{
// LogHelper.LogException
Information.Message = e.Message;
Information.State = 1;
result.Message = e.Message;
}
Info.Add(Information, response);
result.Data = Info;
}
catch (Exception e)
{
result.Message = e.Message;
}
return result;
}
}
该类需要引用
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Dysmsapi.Model.V20170525;
dll 文件获取方式
1https://help.aliyun.com/document_detail/112145.html?spm=a2c4g.11186623.2.16.4bcd19d9sgWFau
Code = "{\"code\":\""+ Code + "\"}";
var Result = SendSMS.SendSms(Tel, Code, AccessKeyId, AccessKeySecret, signName, templateCode);