gpt4 book ai didi

c# - 如何在 C# 中使用 Azure 通信服务 SMS 发送多个收件人 SMS

转载 作者:行者123 更新时间:2023-12-03 06:18:26 26 4
gpt4 key购买 nike

我正在开发小型 Web 应用程序,以使用 Azure 通信服务为多个收件人发送短信。我从微软官方页面获得了以下代码,它适用于一位收件人。

SmsClient smsClient = new SmsClient(connectionString);

SmsSendResult sendResult = smsClient.Send(
from: sms.From.ToString(),
to: sms.To.ToString(),
message: sms.Body,
options: new SmsSendOptions(enableDeliveryReport: true) { }
) ;

如果我传递收件人列表,“to”会显示语法错误。

SMS.cs

`public class SMS 
{
public SMS() { }

public SMS(SMS sms)
{
if (sms == null)
throw new ArgumentNullException(nameof(sms));

From = sms.From;
To = sms.To;
Body = sms.Body;
}

[DataType(DataType.PhoneNumber)]
public string From { get; set; }

public string Body { get; set; }

[DataType(DataType.PhoneNumber)]
public string[] To { get; set; }
}`

我修改了如下代码

 List<SmsSendResult> sendResult = (List<SmsSendResult>) smsClient.Send(
from: sms.From,
to: sms.To,
message: sms.Body,
options: new SmsSendOptions(enableDeliveryReport: true) { }
) ;

错误是enter image description here

ACS 方法参数为 enter image description here

有人可以帮忙吗?

最佳答案

您可以引用sample example here了解如何使用 Azure 通信服务使用 SmsClient 中的 Send 或 SendAsync 函数以及收件人电话号码列表发送短信 1:N 收件人列表。

以下是示例:

Response<IReadOnlyList<SmsSendResult>> response = smsClient.Send(
from: "<from-phone-number>",
to: new string[] { "<to-phone-number-1>", "<to-phone-number-2>" },
message: "Weekly Promotion!",
options: new SmsSendOptions(enableDeliveryReport: true) // OPTIONAL
{
Tag = "marketing", // custom tags
});

IEnumerable<SmsSendResult> results = response.Value;
foreach (SmsSendResult result in results)
{
Console.WriteLine($"Sms id: {result.MessageId}");
Console.WriteLine($"Send Result Successful: {result.Successful}");
}

您需要在上面的代码中进行以下替换:

  • 替换为与您的通信服务资源关联的支持短信的电话号码。
  • 将 和 替换为您想要向其发送消息的电话号码。

关于c# - 如何在 C# 中使用 Azure 通信服务 SMS 发送多个收件人 SMS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76055544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com