目前腾讯云短信为客户提供国内短信和国际/港澳台短信服务,腾讯云短信 SDK 支持以下操作:
国内短信 | 国际/港澳台短信 |
---|---|
|
|
.
在使用 SDK 前,您需要准备以下信息:
1、获取 SDKAppID 和 AppKey
3、申请签名并确认审核通过
4、申请模板并确认审核通过
代码实现
1、Install-Package qcloud.qcloudsms_csharp -Version 0.1.5
2、
/// <summary>
/// 发短信
/// </summary>
public class SMSHelper
{
protected static Logger _logger = LogManager.GetCurrentClassLogger();
// 短信应用SDK AppID
private static int appid = xxxx;
// 短信应用SDK AppKey
private static string appkey = "xxxxx";
// 签名
private static string smsSign = "xxxxx"; // NOTE: 这里的签名只是示例,请使用真实的已申请的签名, 签名参数使用的是`签名内容`,而不是`签名ID`
/// <summary>
/// 指定模板ID单发短信
/// </summary>
/// <param name="phoneNumber">手机号码</param>
/// <param name="templateId">模板id</param>
/// <param name="parameters">参数</param>
public static void SendToSingle(string phoneNumber, int templateId, string[] parameters)
{
//https://cloud.tencent.com/document/product/382/3785
//https://github.com/qcloudsms/qcloudsms_csharp 参考文档
try
{
SmsSingleSender ssender = new SmsSingleSender(appid, appkey);
var result = ssender.sendWithParam("86", phoneNumber,
templateId, parameters, smsSign, "", ""); // 签名参数不能为空串
Console.WriteLine(result);
}
catch (JSONException e)
{
_logger.Error(e);
}
catch (HTTPException e)
{
_logger.Error(e);
}
catch (Exception e)
{
_logger.Error(e);
}
}
}
3、单元测试
/// <summary>
/// 指定模板ID单发短信
/// </summary>
[TestMethod]
public void SendToSingleTest()
{
// 短信模板ID,需要在短信应用中申请
int templateId = xxxx;
//参数
string[] parameters = new[] { "1111", "2222", "3333", "4444", "5555" };
//手机号
string phoneNumber = "xxxx";
////SMSHelper.SendToSingle(phoneNumber, templateId, parameters);
}
}
}