C#一般处理程序+html实现短信发送

本文介绍如何使用阿里云短信SDK在.NET环境下实现短信发送功能,包括配置Access Key、设置短信模板及参数,并通过示例代码展示具体实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下载.net sdk中把四个dll文件拿出来放到项目中引用之后

后台代码

using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.Core;
using Aliyun.Acs.Dysmsapi.Model.V20170525;
using Aliyun.Acs.Core.Exceptions;


namespace WebApplication1.net
{
    /// <summary>
    /// Handler2 的摘要说明
    /// </summary>
    public class Handler2 : IHttpHandler
    {


        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";


            if (context.Request["command"] != null)
            {
                string command = context.Request["command"].ToString();
                System.Reflection.MethodInfo method = this.GetType().GetMethod(command);//搜索指定名称的方法






                if (method != null)
                {
                    method.Invoke(this, new object[] { context });
                }
            }
        }
        public void SendMessage(HttpContext context)
        {
            String product = "Dysmsapi";//短信API产品名称
            String domain = "dysmsapi.aliyuncs.com";//短信API产品域名
            String accessKeyId = "";//这里写你的accessKeyId
            String accessKeySecret = "";//你的accessKeySecret
            string NameValue = context.Request ["NameValue"];
            string TimeValue = context.Request ["TimeValue"];
            IClientProfile profile = DefaultProfile.GetProfile("cn-hangzhou", accessKeyId, accessKeySecret);
            //IAcsClient client = new DefaultAcsClient(profile);
            // SingleSendSmsRequest request = new SingleSendSmsRequest();


            DefaultProfile.AddEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
            IAcsClient acsClient = new DefaultAcsClient(profile);
            SendSmsRequest request = new SendSmsRequest();
            try
            {
                //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为20个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式
                request.PhoneNumbers = context.Request ["PhoneNumber"];
                //必填:短信签名-可在短信控制台中找到
                request.SignName = "";//这里要写上你的签名
                //必填:短信模板-可在短信控制台中找到
                request.TemplateCode = "";//这里要写上你的模板code
                //可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
                request.TemplateParam = "{\"name\":\"" + NameValue + "\",\"time\":\"" + TimeValue + "\"}";//这里是你在设置模板是预留的参数
                //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
                request.OutId = "21212121211";//这里随便写
                //请求失败这里会抛ClientException异常
                SendSmsResponse sendSmsResponse = acsClient.GetAcsResponse(request);


                //System.Console.WriteLine(sendSmsResponse.Message);
                context.Response.Write(sendSmsResponse.Message);




            }
            catch (ServerException e)
            {
                System.Console.WriteLine("Hello World!");
            }
            catch (ClientException e)
            {
                System.Console.WriteLine("Hello World!");
            }


        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

}

下面是html代码

  <title></title>
    <script src="/lib/jquery/jquery-1.11.3.js" type="text/javascript"></script>
   <script type="text/javascript" >
       function SendMessage() {
           var PhoneNumber = $("#PhoneNumber").val();
           var NameValue = $("#NameValue").val();
           var TimeValue = $("#TimeValue").val();
           if (PhoneNumber==""||NameValue==""||TimeValue=="") {
               alert("请全部填写完成!");
               return;
           }
           $.get("/net/Handler2.ashx", { command:"SendMessage",PhoneNumber:PhoneNumber,NameValue:NameValue,TimeValue:TimeValue,Random:Math.random()}, function () {




           });
       }
   
   </script>
</head>
<body>
<table style="width:400px;margin:0 auto;text-align:center;border:1px solid Gray;">
    <tr style="height:50px;">
        <td>手机号码:</td>
        <td>
            <input type="text" id="PhoneNumber"/>
        </td>
    </tr>
      <tr style="height:50px;">
        <td>姓名:</td>
        <td>
            <input type="text" id="NameValue"/>
        </td>
    </tr>
      <tr style="height:50px;">
        <td>时间:</td>
        <td>
            <input type="text" id="TimeValue"/>
        </td>
    </tr>    
    <tr style="height:50px;">
        <td colspan="2" style="text-align:center;">
            <input type="button" value="发送" onclick="SendMessage()" />
        </td>       
    </tr>
</table>
</body>


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值