问题
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!");
}
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
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?
I'm using Visual studio 2015 and windows 10 platform
Thank you
回答1:
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;
}
回答2:
I end up resolving my problems
so this was not working on VS2015 Platform :
var client = new TwilioRestClient(Environment.GetEnvironmentVariable(ssid), Environment.GetEnvironmentVariable(token));
client.SendMessage(number, "+158965220", "Teting API message!");
Even thought I already installed the Twilio api version 4.7.2 using nuget console
I did again the following :
Install-Package Twilio
than Visual studio suggest me to add reference to the API inside the code
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
Thanks to the following Example
this has worked :
TwilioClient.Init(accountSid, authToken);
var message = MessageResource.Create(
to: new PhoneNumber(toNumber),
from: new PhoneNumber(FromNumber),
body: "Hello from C#");
So my conclusion is probably an outdated library because when I used NUGET to install:
Install-Package Twilio -Version 4.7.2
I have no idea why it wasn't working
P.S: I'm not sure if it was relevant but I also installed this using NUGET console it mighed helped or it might not but I felt like I had to mention it :
Install-Package RestSharp
来源:https://stackoverflow.com/questions/42618361/how-to-send-sms-using-c-sharp-and-twilio-api