0 Comments

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; }
}
}

One Reply to “How to send a free SMS from C#”

  1. Do you need to install a messagebird API in C# or is this the only code you need to be able to send an SMS? I’m interested in sending SMS notifications for one of my projects and was wondering how I could integrate something like this?

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts