How to send a free SMS from C#

I used the following code to send a free SMS from C#, by using the messagebird REST service.

 

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace DocumentenService.Client
{
public class Program
{
public static void Main(string[] args)
{
SendSms().Wait();
// Wait for the user to close this application.
Console.WriteLine("Press enter to close this application.");
string result = Console.ReadLine();
}
/// <summary>
/// Send a sms
/// </summary>
/// <returns></returns>
public static async Task SendSms()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("https://rest.messagebird.com/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("AccessKey", "live_...");
var message = new SmsMessage
{
body = "SMS send from C#, greetings Roel.",
originator = 31611111111, // Sender
recipients = new long[] { 31622222222 } // Receivers
};
HttpResponseMessage response = await client.PostAsJsonAsync("messages", message);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("succes!");
}
}
}
}
public class SmsMessage
{
/// <summary>
/// Example: This is a test message.
/// </summary>
public string body { get; set; }
/// <summary>
/// Example: 31611111111
/// </summary>
public long originator { get; set; }
/// <summary>
/// Example: new long[] { 31622222222 } 
/// </summary>
public long[] recipients { get; set; }
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *