Where Developers Build for Developers

C#

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace MessagingExample
{
    class SMSPoh
    {
        private static readonly string ApiKey = "YOUR_API_KEY";
        private static readonly string ApiSecret = "YOUR_API_SECRET";
        private static readonly string SenderId = "YourSenderID";
        private static readonly string Recipient = "+959********";
        private static readonly string Message = "Hello, this is a test message from SMSPoh!";

        static async Task Main(string[] args)
        {
            await SendSMS(Recipient, Message);
        }

        public static async Task SendSMS(string recipient, string message)
        {
            var apiUrl = "https://v3.smspoh.com/api/rest/send";

            // Prepare the payload
            var payload = new
            {
                to = recipient,
                message = message,
                from = SenderId
            };

            // Encode the API key and secret
            var credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{ApiKey}:{ApiSecret}"));

            // Set up the request
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", credentials);
                client.DefaultRequestHeaders.Add("Content-Type", "application/json");

                var jsonPayload = JsonConvert.SerializeObject(payload);
                var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");

                // Send the request
                var response = await client.PostAsync(apiUrl, content);

                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("Message sent successfully!");
                }
                else
                {
                    Console.WriteLine($"Failed to send message. Status code: {response.StatusCode}");
                }
            }
        }
    }
}