首先从控制台获取编写程序必须提供的参数
secretId,secretKey
SmsSdkAppId
SignName
TemplateId
step 1:获取secretId,secretKey
https://console.cloud.tencent.com/cam/capi
step 2:TencentCloud.Sms正文模板管理参数获取
https://console.cloud.tencent.com/smsv2/csms-template
ID | 模板类型 | 模板名称 | 内容 | 状态/原因 | 申请时间 | 操作 |
---|
813494 | 普通短信 | 发货短信提醒模板 | 尊敬的客户,您的订单{1} 已发出,发货单号为:{2},请注意查收,谢谢合作!**公司 | 已通过 | 2020-12-16 23:52:44 |
step 3:获取签名
https://console.cloud.tencent.com/smsv2/csms-sign
step 4:获取SDKAppID
https://console.cloud.tencent.com/smsv2/app-manage
How to use
G:\tencentcloud-sdk-dotnet\src\5.x\TencentCloudExamples\Program.cs
/*
* Copyright (c) 2018 THL A29 Limited, a Tencent company. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
using System;
using System.Threading.Tasks;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Sms;
using TencentCloud.Sms.Models;
namespace TencentCloudExamples
{
/// <summary>
/// run ok
/// detail:https://www.iaspnetcore.com/Blog/BlogPost/60bbfba559018b01fdfb177c/abptencentcloudsms-with-net-50
/// </summary>
class Program
{
/*
* -------------------------------------------------------------------------------
* 过程名: genTC3Auth
作者: 张旭州
日期: 2021.02.15
参数: SecretKey, SecretId, sDomain, bodyJSON, Service: string
参数说明: AccessKeyID,AccessKeySecret,域名, 待发送的数据主体JSON, 服务ocr, cvm
返回值: string
参数参考如下:
SecretKey := '您的腾讯云SecretKey';
SecretId := '您的腾讯云SecretId';
Service := 'sms' //发送短信
sDomain := 'sms.tencentcloudapi.com' //短信发送的域名
*
*/
/// <summary>
/// wjl sms:test send sms ok
/// </summary>
/// <param name="args"></param>
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
try
{
/* 必要步骤:
* 实例化一个认证对象,入参需要传入腾讯云账户密钥对secretId,secretKey。
* 这里采用的是从环境变量读取的方式,需要在环境变量中先设置这两个值。
* 你也可以直接在代码中写死密钥对,但是小心不要将代码复制、上传或者分享给他人,
* 以免泄露密钥对危及你的财产安全。
* CAM密匙查询: https://console.cloud.tencent.com/cam/capi
*/
Credential cred = new Credential
{
SecretId = "AKIDXtzp",
SecretKey = "wby4uSj"
};
/*
Credential cred = new Credential {
SecretId = Environment.GetEnvironmentVariable("TENCENTCLOUD_SECRET_ID"),
SecretKey = Environment.GetEnvironmentVariable("TENCENTCLOUD_SECRET_KEY")
};*/
/* 非必要步骤:
* 实例化一个客户端配置对象,可以指定超时时间等配置 */
ClientProfile clientProfile = new ClientProfile();
/* SDK默认用TC3-HMAC-SHA256进行签名
* 非必要请不要修改这个字段 */
clientProfile.SignMethod = ClientProfile.SIGN_TC3SHA256;
/* 非必要步骤
* 实例化一个客户端配置对象,可以指定超时时间等配置 */
HttpProfile httpProfile = new HttpProfile();
/* SDK默认使用POST方法。
* 如果你一定要使用GET方法,可以在这里设置。GET方法无法处理一些较大的请求 */
// httpProfile.ReqMethod = "GET";
/* SDK有默认的超时时间,非必要请不要进行调整
* 如有需要请在代码中查阅以获取最新的默认值 */
httpProfile.Timeout = 10; // 请求连接超时时间,单位为秒(默认60秒)
/* SDK会自动指定域名。通常是不需要特地指定域名的,但是如果你访问的是金融区的服务
* 则必须手动指定域名,例如sms的上海金融区域名: sms.ap-shanghai-fsi.tencentcloudapi.com */
httpProfile.Endpoint = "sms.tencentcloudapi.com";
// 代理服务器,当你的环境下有代理服务器时设定
// httpProfile.WebProxy = Environment.GetEnvironmentVariable("HTTPS_PROXY");
clientProfile.HttpProfile = httpProfile;
/* 实例化要请求产品(以sms为例)的client对象
* 第二个参数是地域信息,可以直接填写字符串ap-guangzhou,或者引用预设的常量 */
SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
/* 实例化一个请求对象,根据调用的接口和实际情况,可以进一步设置请求参数
* 你可以直接查询SDK源码确定SendSmsRequest有哪些属性可以设置
* 属性可能是基本类型,也可能引用了另一个数据结构
* 推荐使用IDE进行开发,可以方便的跳转查阅各个接口和数据结构的文档说明 */
SendSmsRequest req = new SendSmsRequest();
/* 基本类型的设置:
* SDK采用的是指针风格指定参数,即使对于基本类型你也需要用指针来对参数赋值。
* SDK提供对基本类型的指针引用封装函数
* 帮助链接:
* 短信控制台: https://console.cloud.tencent.com/sms/smslist
* sms helper: https://cloud.tencent.com/document/product/382/3773 */
//req.SmsSdkAppId = "1258259871";
req.SmsSdkAppId = "1400**";
/* 短信签名内容: 使用 UTF-8 编码,必须填写已审核通过的签名,签名信息可登录 [短信控制台] 查看 */
req.SignName = "**公司";
/* 短信码号扩展号: 默认未开通,如需开通请联系 [sms helper] */
// req.ExtendCode = "x";
/* 国际/港澳台短信 senderid: 国内短信填空,默认未开通,如需开通请联系 [sms helper] */
// req.SenderId = "";
/* 用户的 session 内容: 可以携带用户侧 ID 等上下文信息,server 会原样返回 */
// req.SessionContext = "";
/* 下发手机号码,采用 e.164 标准,+[国家或地区码][手机号]
* 示例如:+8613711112222, 其中前面有一个+号 ,86为国家码,13711112222为手机号,最多不要超过200个手机号*/
req.PhoneNumberSet = new String[] { "+8613711112222" };
/* 模板 ID: 必须填写已审核通过的模板 ID。模板ID可登录 [短信控制台] 查看 */
req.TemplateId = "813494";
/* 模板参数: 若无模板参数,则设置为空*/
req.TemplateParamSet = new String[] { "666", "777" };
// 通过client对象调用DescribeInstances方法发起请求。注意请求方法名与请求对象是对应的
// 返回的resp是一个DescribeInstancesResponse类的实例,与请求对象对应
SendSmsResponse resp = client.SendSmsSync(req);
// 输出json格式的字符串回包
Console.WriteLine(AbstractModel.ToJsonString(resp));
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Console.Read();
}
}
}
EasyAbp.Abp.TencentCloud.Sms.Tests