You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用C# WinForms结合Twilio API发送批量短信

使用Twilio API和C#发送批量短信(含Passthrough API详解)

嘿,我来帮你理清用Twilio API和C#发送批量短信的方法,尤其是你搞不懂的Passthrough API用法~首先你现在写的代码是单条短信的发送逻辑,批量发送的话我们分两种场景来聊:

一、基础批量:循环发送单条短信

如果是小规模批量(比如几十上百条),直接基于你现有的代码做循环就行,注意要避开Twilio的速率限制(不同地区的号码限制不同,比如美国号码一般是每秒1条)。

改进后的代码示例:

using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

// 你的Twilio账户凭证
const string accountSid = "xxxxx";
const string authToken = "xxxxx";
TwilioClient.Init(accountSid, authToken);

// 把需要发送的收件人号码整理成列表
var recipientNumbers = new List<string> 
{ 
    "+27" + txtTo1.Text, 
    "+27" + txtTo2.Text, 
    // 可以继续添加更多号码
};
var messageContent = txtMessage.Text;
var yourTwilioNumber = new PhoneNumber("xxxxx"); // 你的Twilio发信号码

foreach (var number in recipientNumbers)
{
    try
    {
        var sentMessage = MessageResource.Create(
            to: new PhoneNumber(number),
            from: yourTwilioNumber,
            body: messageContent,
            provideFeedback: true,
            statusCallback: new Uri("http://requestb.in/...") // 你的状态回调地址
        );
        Console.WriteLine($"短信已发送,SID: {sentMessage.Sid}");
        // 加个1秒延迟,避免触发速率限制
        System.Threading.Thread.Sleep(1000);
    }
    catch (Exception ex)
    {
        Console.WriteLine($"发送到 {number} 失败:{ex.Message}");
    }
}

这种方式简单直接,适合小批量场景,但如果是上千条的大规模发送,更推荐用下面的Messaging Service结合Passthrough API的方式。

二、高级批量:结合Passthrough API发送

你提到的Passthrough API,其实是Twilio用来让你传递自定义业务参数的能力——比如你需要给每条短信关联用户ID、订单号,或者给运营商传递特定参数,都可以用它。配合Twilio的Messaging Service,还能自动处理号码池和速率限制,效率更高。

首先你需要先在Twilio控制台创建一个Messaging Service,拿到它的SID,然后用下面的代码:

using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
using Newtonsoft.Json; // 需要引用Newtonsoft.Json NuGet包

const string accountSid = "xxxxx";
const string authToken = "xxxxx";
const string messagingServiceSid = "你的Messaging Service SID"; // 控制台获取
TwilioClient.Init(accountSid, authToken);

// 每条短信可以带自定义业务数据,后续在状态回调里能拿到
var batchTasks = new List<dynamic>
{
    new { To = "+27xxxxxxxxx", UserInfo = new { UserId = 123, OrderNo = "ORD-001" } },
    new { To = "+27yyyyyyyyy", UserInfo = new { UserId = 456, OrderNo = "ORD-002" } }
};

foreach (var task in batchTasks)
{
    try
    {
        var message = MessageResource.Create(
            to: new PhoneNumber(task.To),
            messagingServiceSid: messagingServiceSid, // 用Messaging Service代替固定发信号码
            body: txtMessage.Text,
            provideFeedback: true,
            statusCallback: new Uri("http://requestb.in/..."),
            // 这里就是Passthrough参数的用法:把自定义数据序列化后传递
            parameters: new Dictionary<string, string>
            {
                { "Passthrough", JsonConvert.SerializeObject(task.UserInfo) }
            }
        );
        Console.WriteLine($"短信已发送,SID: {message.Sid}");
    }
    catch (Exception ex)
    {
        Console.WriteLine($"发送到 {task.To} 失败:{ex.Message}");
    }
}

关于Passthrough API的关键点:

  • 自定义参数传递:通过parameters字典里的Passthrough键,把你的业务数据(比如用户ID、订单号)序列化后传递,Twilio会把这个参数带回你的状态回调接口,方便你跟踪每条短信对应的业务信息。
  • Messaging Service优势:它会自动从你的号码池里选合适的号码发送,避免单一号码发送过多被运营商限制,还能自动适配速率限制,大规模发送更稳定。

三、小提醒

  • 速率限制:Twilio的短信发送速率限制和你的账户类型、号码地区有关,手动循环发送一定要加延迟,用Messaging Service就不用自己操心这个。
  • 状态回调:你设置的statusCallback地址会收到Twilio推送的短信状态(已发送、已送达、失败),结合Passthrough参数就能精准对应到每条业务记录。

内容的提问来源于stack exchange,提问作者Marcus

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:04:05