英文:
How do I send multiple recipient SMS using the Azure communication service SMS in c#
问题
I am developing small web application to send SMS using Azure communication service for multiple recipients. I got a following code from official microsoft page and it is working for one recipient.
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) { }
);
If I pass a list of recipients, the "to" field is showing a syntax error.
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; }
}
I modified the code like this:
List<SmsSendResult> sendResult = (List<SmsSendResult>) smsClient.Send(
from: sms.From,
to: sms.To,
message: sms.Body,
options: new SmsSendOptions(enableDeliveryReport: true) { }
);
The error is shown in the images you provided:
The ACS method parameter is shown in this image:
Can anyone please help with this?
英文:
I am developing small web application to send SMS using Azure communication service for multiple recipients. I got a following code from official microsoft page and it is working for one recipient.
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) { }
) ;
if I pass a list of recipients the "to" showing a syntax error.
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; }
}`
I modified the code like as below
List<SmsSendResult> sendResult = (List<SmsSendResult>) smsClient.Send(
from: sms.From,
to: sms.To,
message: sms.Body,
options: new SmsSendOptions(enableDeliveryReport: true) { }
) ;
Can anyone please help in this?
答案1
得分: 1
以下是您要翻译的内容:
您可以参考此处的示例示例,了解如何使用 Azure 通信服务中的 SmsClient 的 Send 或 SendAsync 函数发送给一组接收者的 SMS 消息,使用收件人电话号码列表。
以下是示例示例:
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", // 自定义标签
});
IEnumerable<SmsSendResult> results = response.Value;
foreach (SmsSendResult result in results)
{
Console.WriteLine($"Sms id: {result.MessageId}");
Console.WriteLine($"Send Result Successful: {result.Successful}");
}
您需要在上述代码中进行以下替换:
- 将
<from-phone-number>
替换为与您的 Communication Services 资源关联的支持 SMS 的电话号码。 - 将
<to-phone-number-1>
和<to-phone-number-2>
替换为您想发送消息的电话号码。
英文:
You can refer to the sample example here on how to send sms 1:N list of recipients using azure communication service using Send or SendAsync function from the SmsClient with a list of recipient phone numbers.
Here is the sample example:
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}");
}
You need to Make these replacements in the above code:
- Replace <from-phone-number> with an SMS-enabled phone number that's associated with your Communication Services resource.
- Replace <to-phone-number-1> and <to-phone-number-2> with phone numbers that you'd like to send a message to.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论