1:创建一个项目用来调用第三方的类,右键Nuget添加第三方的引用类库 qcloudsms_csharp
2:把第三方的公共类放入到我们的项目里
using qcloudsms;
using qcloudsms_csharp.httpclient;
using qcloudsms_csharp.json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MiShop.Remote
{
public class TenXunYunSMS
{
//appId
public int appId;
//appKey
public string appKey = "";
//短信模板ID
private int tmplateId = 379257;
//签名内容
private string smsSign = "7hhhcn";
/// <summary>
/// 验证码
/// </summary>
public int Code { get; set; }
/// <summary>
/// 发送验证码
/// </summary>
/// <param name="phone"></param>
/// <returns></returns>
public void SetSMS(string phone)
{
Random random = new Random();
int code = random.Next(100000, 999999);
try
{
SmsSingleSender ssender = new SmsSingleSender(appId, appKey);
var result = ssender.sendWithParam("86", phone,
tmplateId, new[] { code.ToString() }, smsSign, "", ""); // 签名参数未提供或者为空时,会使用默认签名发送短信
}
catch (JSONException ex)
{
throw;
}
catch (HTTPException ex)
{
throw;
}
catch (Exception ex)
{
throw;
}
Code = code;
}
}
}
3:创建一张短信发送记录表
//信息记录表
public class SMSInfo
{
public int Id { get; set; }
public int Code { get; set; }
public Int64 TelPhone { get; set; }
//创建时间
public DateTime CreateTime { get; set; }
//短信过期时间
public DateTime ExpireTime { get; set; }
}
4:数据访问层写插入短信信息表方法