如何使用Azure通信服务的C#发送多个接收方短信?

huangapple go评论76阅读模式
英文:

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:

如何使用Azure通信服务的C#发送多个接收方短信?

The ACS method parameter is shown in this image:

如何使用Azure通信服务的C#发送多个接收方短信?

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&lt;SmsSendResult&gt; sendResult = (List&lt;SmsSendResult&gt;) smsClient.Send(
            from: sms.From,
            to: sms.To,
            message: sms.Body,
            options: new SmsSendOptions(enableDeliveryReport: true) { }
        ) ;

the error is 如何使用Azure通信服务的C#发送多个接收方短信?

the ACS method parameter is 如何使用Azure通信服务的C#发送多个接收方短信?

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&lt;IReadOnlyList&lt;SmsSendResult&gt;&gt; response = smsClient.Send(
    from: &quot;&lt;from-phone-number&gt;&quot;,
    to: new string[] { &quot;&lt;to-phone-number-1&gt;&quot;, &quot;&lt;to-phone-number-2&gt;&quot; },
    message: &quot;Weekly Promotion!&quot;,
    options: new SmsSendOptions(enableDeliveryReport: true) // OPTIONAL
    {
        Tag = &quot;marketing&quot;, // custom tags
    });

IEnumerable&lt;SmsSendResult&gt; results = response.Value;
foreach (SmsSendResult result in results)
{
    Console.WriteLine($&quot;Sms id: {result.MessageId}&quot;);
    Console.WriteLine($&quot;Send Result Successful: {result.Successful}&quot;);
}

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.

huangapple
  • 本文由 发表于 2023年4月19日 22:11:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76055544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定