一、在阿里云上开通【短信服务】
1.登录阿里云,搜索短信服务
阿里云官网:https://www.aliyun.com/
需要没有阿里云的账号,需要进行注册申请,登录成功后,进去控制台,然后找到短信服务,或者直接在搜索栏搜索短信 服 务。
2.点击当前页面中出现的免费开通进入页面
3.阿里云的便捷性还是很高的,进入页面就有了便利于新手用的 快速学习 按钮
4.在快速学习中,可以看到他让我选择签名和模板
5.我们可以用他自带的样式去测试看效果
手机收到的效果 与 页面中右侧所展示的模拟相同
6.创建自己想要的签名
签名内容尽量不要涉及到XXXX公司 若要用XXXX公司则要升级用户到企业用户
完成新建后需要等待2个小时的审核期,申请说明可以尽可能的详细,可以增加审批的速度。
刚码完字,还没5分钟就完成了审批
7.创建自己想要的模板
可以通过标签页去转移到模板管理页面,添加自定义的文档
同样也是两个小时的审核时间,模板的审核会比签名严格很多,所以尽可能在申请说明中说明用途,及后果分配。
经历了半个小时的等待,审核终于通过。
8.在快速学习页面调取自己创建的签名和模板
9.查看对应API DEMO
10.查看自己的accessKeyId
这边需要你放入的有accessKeyId,accessKeySecret。
查看accessKeyId,accessKeySecret
点击头像选 中“accessKeyId”点击就会看到,签名就是你申请通过的签名。
自己帐号内存放一定定额,5元,10元都可以
发送一条验证码短信价格为0.03元/条,账户内没余额可能会造成发送失败。
二、把API DEMO转换成代码
编程语言:C# 控制台程序
环境:Visual Studio 2010
引用到的文件:阿里云提供的
aliyun-net-sdk-core.dll
aliyun-net-sdk-dysmsapi.dll
下载地址:https://download.csdn.net/download/weixin_40276431/12209034 (需要2积分哈 我混点积分下点学习资源, 没有积分的可以联系我,评论邮箱, 我有空就发)
首先建立一个新的控制台程序
这里右击引用——点击添加引用
找到相应的dll文件 点击添加
在Program.cs文件中
输入代码
代码内容为阿里云提供的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
namespace test
{
class Program
{
static void Main(string[] args)
{
String accessKeyId = "你的accessKeyId ";
String accessSecret = "你的accessSecret ";
IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessSecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.Method = MethodType.POST;
request.Domain = "dysmsapi.aliyuncs.com";
request.Version = "2017-05-25";
request.Action = "SendSms";
/*
* PhoneNumbers 接收验证码的手机号
* SignName 你刚刚注册的签名名称
* TemplateCode 你刚刚注册的模板编码
* code code这边是一个变量 内容可以自定义 我们测试可以先把他写死
* 在运行成功后 尝试更改为随机6位验证码等
*/
request.AddQueryParameters("PhoneNumbers", "接收验证码的手机号");
request.AddQueryParameters("SignName", "签名名称");
request.AddQueryParameters("TemplateCode", "模板编码");
String code = "123456";
request.AddQueryParameters("TemplateParam", "{\"code\":\"" + code + "\"}");
try
{
CommonResponse response = client.GetCommonResponse(request);
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (ServerException e)
{
Console.WriteLine(e);
}
catch (ClientException e)
{
Console.WriteLine(e);
}
Console.ReadLine();
}
}
}
别忘了using
代码:
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
错误修改:
可能会出现许多报错
右键项目—点击属性—调整一下框架版本即可 解决报错
运行结果:
三、模拟验证码发送 验证并登录
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
namespace Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
String code;
private void button2_Click(object sender, EventArgs e)
{
if (textBox2.Text == code)
{
MessageBox.Show("登录成功");
}
else
{
MessageBox.Show("登录失败");
}
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
String accessKeyId = "你的accessKeyId";
String accessSecret = "你的accessSecret";
IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessSecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.Method = MethodType.POST;
request.Domain = "dysmsapi.aliyuncs.com";
request.Version = "2017-05-25";
request.Action = "SendSms";
string phone = textBox1.Text;
request.AddQueryParameters("PhoneNumbers", phone);
request.AddQueryParameters("SignName", "你的签名");
request.AddQueryParameters("TemplateCode", "你的模板");
code = getRandomString();
//"{\"code\":\""+code+"\"}"
request.AddQueryParameters("TemplateParam", "{\"code\":\"" + code + "\"}");
try
{
CommonResponse response = client.GetCommonResponse(request);
Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content));
}
catch (Exception)
{
throw;
}
}
else
{
MessageBox.Show("手机号不能为空");
}
}
//生成一个 6 位数的全数字验证码
public string getRandomString()
{
Random rd = new Random();
int AuthCodeNumber = rd.Next(100000, 1000000);
String AuthCode = AuthCodeNumber.ToString();
return AuthCode;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
因为项目代码不能露出,设计了一个和小项目、实战相关的项目,学会方法,进行套用。
这个是个winform程序 控件2个lable 2个textbox 2个button
很基础但可以很快掌握的项目。
这次的内容就这么多 希望这篇文章对你有用 谢谢观看