首页 短信 短信示例代码 发送短信[C#]

发送短信示例代码C#

作者: 阅读数:1164 上传时间:2025-04-01

发送短信

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
 
//await SMS.SendAsync('13488888888', '用户您好。【极速数据】');
class SMS
{
    // 替换成你的AppKey
    private const string AppKey = "YOUR_APPKEY_HERE";
 
    public static async Task SendAsync(string mobile, string content)
    {
        try
        {
            using (HttpClient client = new HttpClient())
            {
                // 构造请求URL
                string url = $"https://api.jisuapi.com/sms/send?appkey={AppKey}&"+;
                            $"mobile={mobile}&" +
                            $"content={Uri.EscapeDataString(content)}";
 
                // 发送GET请求
                HttpResponseMessage response = await client.GetAsync(url);
                 
                // 确保成功响应
                response.EnsureSuccessStatusCode();
 
                // 读取响应内容
                string responseBody = await response.Content.ReadAsStringAsync();
                JObject result = JObject.Parse(responseBody);
 
                // 处理API返回状态
                if ((int)result["status"] == 0)
                {
                    // 解析信息
                    JToken data = result["result"];
                    Console.WriteLine($"发送数量:{data["count"]} 账户ID:{data["accountid"]}");                   
                }
                else
                {
                    Console.WriteLine($"查询失败:{result["status"]} {result["msg"]}");
                }
            }
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine($"请求异常:{ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误:{ex.Message}");
        }
    }
     
    static async Task Main(string[] args)
    {
        //await SendAsync();
    }
}