zoukankan      html  css  js  c++  java
  • C# 使用阿里云发送短信

      最近有个项目,短信服务使用的是阿里云的,由于时间问题,没有手动去构造sign去发送,而是直接使用阿里云的SDK发送,所以这里算是做个笔记,或许以后还能用得到

      首先,我们需要安装阿里云的SDK,推荐使用nuget安装,搜索aliyun-net-sdk-core,直接按照即可

      

       安装完成之后就可以使用了,代码如下:  

    using Aliyun.Acs.Core;
    using Aliyun.Acs.Core.Http;
    using Aliyun.Acs.Core.Profile;
    using System;
    
    namespace DemoConsole2
    {
        class Program
        {
            static void Main(string[] args)
            {
                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";
                // request.Protocol = ProtocolType.HTTP;
    
                request.AddQueryParameters("PhoneNumbers", "185XXXXXXXX");
                request.AddQueryParameters("SignName", "签名");
                request.AddQueryParameters("TemplateCode", "SMS_176375688");
                request.AddQueryParameters("TemplateParam", "{\"code\":\"074281\"}");
                request.AddQueryParameters("OutId", "");
    
                try
                {
                    CommonResponse response = client.GetCommonResponse(request);
                    var content = System.Text.Encoding.Default.GetString(response.HttpResponse.Content);
                    Console.WriteLine(content);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                }
            }
        }
    }
    View Code

      注意,其中参数要用AddQueryParameters方法,因为参数是放在QueryString中携带过去的,具体可以使用哪些参数可以参考阿里云的开发文档:https://help.aliyun.com/document_detail/101414.html?spm=a2c4g.11186623.6.616.74665f30I95HSl

      其中accessKeyId和accessSecret可以在阿里云上查看,如下图

      

       签名和模板Code需要申请和审核,如下图

       

  • 相关阅读:
    python web 2
    python web1(解析url)
    webstrom 今天突然要激活
    数组排序 记录一下
    浏览器添加随机数去除缓存
    vue-cli 安装报错
    vue 初始化项目报错
    深拷贝和浅拷贝
    css3 属性 clip-path
    js数组去重
  • 原文地址:https://www.cnblogs.com/shanfeng1000/p/11751315.html
Copyright © 2011-2022 走看看