如何使用C#和twilio API发送短信 [英] How to send sms using C# and twilio API

查看:208
本文介绍了如何使用C#和twilio API发送短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个用户界面,用户可以在其中输入她的凭据和电话号码,并使用 Twilio API

I'm trying to create a User interface where the user can enter her credential and phone number and send messages to some recipient using Twilio API

因此,正如我所解释的,我创建了我的帐户并初始化了身份验证密钥和令牌

So as explained I created my account and initialized the Authentication key and token

    private void button1_Click(object sender, EventArgs e)
    {
         string ssid = twilioSSIDBox.Text;
         string token = twilioTokenBox.Text;
         string number = twilioNumberBox.Text;


        var client = new TwilioRestClient(Environment.GetEnvironmentVariable(ssid), Environment.GetEnvironmentVariable(token));
        client.SendMessage(number, "+158965220", "Teting API message!");

    }

经过多次测试(对ssid和令牌以及编号进行硬编码)和文档咨询之后,该消息仍未在Visual Studio平台上发送,并且我没有收到任何错误消息或任何东西

After multiple test (hard coding the ssid and token and number ) and documentation consulting , the message is still not being sent on the visual studio platform and I'm not receiving any error message or anything

所以我的问题是我想念的是什么?我需要一个允许Visual Studio能够发送短信的库吗?

So my question is what I am missing? Do I need a certain library that allow visual studio to be able to send sms messages?

我正在使用Visual Studio 2015和Windows 10平台

I'm using Visual studio 2015 and windows 10 platform

谢谢

推荐答案

查看是否可以从以下代码中获得任何帮助:

See if you can get any help from the below code:

/// <summary>
    /// Sends alert as SMS 
    /// </summary>
    /// <param name="details"></param>
    /// <returns></returns>
    public static Message SendSms(DeliveryDetails details)
    {
        var messageResult = new Message();
        try
        {
            if (details?.ToNumber != null)
            {
                var toNumberList = details.ToNumber.ToList();
                if (toNumberList.Count > 0)
                {
                    foreach (var toNumber in toNumberList)
                    {
                        messageResult = Twilio.SendMessage(FromNumber, toNumber, $"{details.Subject}\n\n{details.Message}");

                        if (messageResult == null)
                        {
                            logger.Error(string.Format(
                                "Error connecting to Twilio, message sending failed to {0}",
                                toNumber));
                        }
                        else if (messageResult.RestException != null)
                        {
                            logger.Error(string.Format("Twilio Error Message Description - {0}",
                                messageResult.RestException.Message));
                        }
                        else
                        {
                            logger.Info(String.Format("SMS {0} deliverd to {1}", messageResult.Body, messageResult.To));
                        }
                    }
                }
                else
                {
                    logger.Error("ToNumber List Empty");
                }
            }
            else
            {
                logger.Error("ToNumber List Null");
            }
        }
        catch (Exception e)
        {
            logger.Error(String.Format("An error occurred while sending the message\n{0}", e.Message));
        }

        return messageResult;
    }

这篇关于如何使用C#和twilio API发送短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆