C# 如何从asp.net mvc应用程序发送批量短信

C# 如何从asp.net mvc应用程序发送批量短信,c#,asp.net,asp.net-mvc,C#,Asp.net,Asp.net Mvc,我正在创建asp,NETMVC应用程序,其中一个要求是从存储在数据库中的手机号码发送批量短信。我让Twilio通过REST API在C#和.NET中发送短信,但我不知道如何在MVC中使用它,下面是代码: using System; using System.Collections.Generic; using Twilio; using Twilio.Rest.Api.V2010.Account; using Twilio.Types; namespace Quickstart { class

我正在创建asp,NETMVC应用程序,其中一个要求是从存储在数据库中的手机号码发送批量短信。我让Twilio通过REST API在C#和.NET中发送短信,但我不知道如何在MVC中使用它,下面是代码:

using System;
using System.Collections.Generic;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;

namespace Quickstart
{
class SmsSender
{
    static void Main(string[] args)
    {
        const string accountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        const string authToken = "your_auth_token";

        TwilioClient.Init(accountSid, authToken);

        var people = new Dictionary<string, string>() {
            {"+14158675309", "Curious George"},
            {"+14158675310", "Boots"},
            {"+14158675311", "Virgil"}
        };

        // Iterate over all our friends
        foreach (var person in people)
        {
            // Send a new outgoing SMS by POSTing to the Messages resource
            MessageResource.Create(
                from: new PhoneNumber("555-555-5555"), // From number, must be an SMS-enabled Twilio number
                to: new PhoneNumber(person.Key), // To number, if using Sandbox see note above
                // Message content
                body: $"Hey {person.Value} Monkey Party at 6PM. Bring Bananas!");

            Console.WriteLine($"Sent message to {person.Value}");
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用斜纹布;
使用Twilio.Rest.Api.V2010.Account;
使用斜纹字体;
命名空间快速启动
{
SmsSender类
{
静态void Main(字符串[]参数)
{
常量字符串accountSid=“ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX”;
const string authToken=“您的授权令牌”;
TwilioClient.Init(accountSid,authToken);
var people=新字典(){
{“+14158675309”,“好奇的乔治”},
{14158675310,“靴子”},
{“+14158675311”,“维吉尔”}
};
//反复访问我们所有的朋友
foreach(人与人之间的变量)
{
//通过发送到消息资源发送新的传出SMS
MessageResource.Create(
from:新电话号码(“555-555-5555”),//from号码必须是支持SMS的Twilio号码
to:newphonenumber(person.Key),//to number,如果使用沙盒,请参见上面的注释
//消息内容
身体:$“嘿{person.Value}晚上6点的猴子派对。带香蕉来!”);
WriteLine($“已将消息发送到{person.Value}”);
}
}
}
}


非常感谢您的帮助。

从数据库中收集用户,这样您就可以收集与给定用户相关联的电话号码,现有用户是一个包含您正在发送的信息的对象。您可以根据自己的需要进行自定义

而不是像这样的静态字典:

var people = new Dictionary<string, string>() {
            {"+14158675309", "Curious George"},
            {"+14158675310", "Boots"},
            {"+14158675311", "Virgil"}
        };
在这里,您可以通过“批量”用户进行循环:

            foreach (AspNetUser dr in doctorsToNotify)
            {
                await notifier.SendWaitingPatientNotification(existingUser, dr.Phone);
            }
通知程序类(编辑“xxxxxxx”以提供电话号码):


我将API sid和令牌存储在web配置文件中,并使用。如果您需要更多帮助,请告诉我。

您有任何错误吗?请使用线程。
            foreach (AspNetUser dr in doctorsToNotify)
            {
                await notifier.SendWaitingPatientNotification(existingUser, dr.Phone);
            }
 public class SMSNotifier
    {
        public SMSNotifier()
        {
            var accountSid = ConfigurationManager.AppSettings["TwilioSid"];
            // Your Auth Token from twilio.com/console
            var authToken = ConfigurationManager.AppSettings["TwilioToken"]; ;

            TwilioClient.Init(accountSid, authToken);
        }

        public async Task SendWaitingPatientNotification(AspNetUser user, string phonenumber)
        {
            var message = await MessageResource.CreateAsync(
             to: new PhoneNumber("+1" + phonenumber),
             from: new PhoneNumber("+xxxxxxxxxx"),
             body: string.Format("Patient {0} {1} has entered the waiting room at {2}. Please visit the patient queue to see patient.", user.FirstName, user.LastName, DateTime.Now.ToString()));

        }
    }