0、前提
注册 微信公众号
https://mp.weixin.qq.com/cgi-bin/registermidpage?action=index&lang=zh_CN
使用 微信公众号 创建 腾讯云 短信签名
https://cloud.tencent.com/search/%E7%9F%AD%E4%BF%A1/1_1
1、下载 TencentCloud 文件
迅雷下载 提取码:wmpq
百度网盘下载 提取码:lyio
2、创建MVC项目
3、添加现有项目
4、添加引用
5、写代码
前端代码自行发挥
后端如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
//导入命名空间
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Sms.V20190711;
using TencentCloud.Sms.V20190711.Models;
//跳转到页面
public ActionResult dd()
{
return View();
}
//输入手机号,post提交
[HttpPost]
public ActionResult dd(string sjh)
{
//yzm 随机生成的验证码
Random rd = new Random();
string yzm = rd.Next(100000, 1000000).ToString();
//time 验证码过期时间 单位:分钟
int time = 5;
SendDL(new string[] { "+86"+sjh},yzm,time);
return Content("true");
}
/// <summary>
/// 发送短信的方法
/// </summary>
/// <param name="PhoneNumber">发送手机</param>
/// <param name="code">验证码</param>
/// <param name="Time">有效时间</param>
public static void SendDL(string[] PhoneNumber, string code, int Time)
{
try
{
Credential cred = new Credential
{
SecretId = "", //在腾讯云官网中的“云产品”中找到“访问秘钥”,点击打开,就看得到相关ID和Key,复制填写即可
SecretKey = ""
};
ClientProfile clientProfile = new ClientProfile();
HttpProfile httpProfile = new HttpProfile();
httpProfile.Endpoint = ("sms.tencentcloudapi.com");
clientProfile.HttpProfile = httpProfile;
SmsClient client = new SmsClient(cred, "", clientProfile);
SendSmsRequest req = new SendSmsRequest();
req.PhoneNumberSet = PhoneNumber;
req.TemplateID = "";//创建正文模板ID
req.SmsSdkAppid = "";//在腾讯云官网中的“云产品”中找到“访问秘钥”,点击打开,就会有一个APPID,复制填写即可
req.Sign = "";//您的公众号名字
req.TemplateParamSet = new String[] { code, Time.ToString() };
SendSmsResponse resp = client.SendSmsSync(req);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return;
}