How Can I Send Sms Using C#?
Ensure that the .NET SDK is installed on your system by downloading and installing .NET from the official website.
To check if it's installed, run this command in your Terminal or Command Prompt:
dotnet --version
If you don’t have a C# project, create one by running:
dotnet new console -n SmsSender cd SmsSender
Since the code uses Newtonsoft.Json, install it via NuGet:
dotnet add package Newtonsoft.Json dotnet run
Copy and paste this code into a C# project, save the file as Program.cs, and replace its contents with the provided C# code. Then, update the Authentication_id and Authentication_key with your credentials. Click Authentication Key ID, and a page will display your 32-character Authentication ID and 32-character Authentication Key, which you can copy and paste into the code.
using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; class Program { static async Task Main() { await SendSmsAsync(); } static async Task SendSmsAsync() { var obj = new { mtype = "N", to = "+91XXXXXXXXXX", message = "This is a test message from CSharp...", auth_id = "Authentication_Id", auth_key = "Authentication_Key" }; string url = "https://msgpserver.com/api/"; using (HttpClient client = new HttpClient()) { string json = JsonConvert.SerializeObject(obj); var content = new StringContent(json, Encoding.UTF8, "application/json"); try{ HttpResponseMessage response = await client.PostAsync(url, content); response.EnsureSuccessStatusCode(); string result = await response.Content.ReadAsStringAsync(); Console.WriteLine("Response from server: " + result); } catch (HttpRequestException e) { Console.WriteLine("Error: " + e.Message); } } } }
Return Success Response.
[ SUCCESS ] {'status': 'success', 'login': 'success', 'recharge': 'success', 'host': 'success', 'msg': 'success'}
Return Error Response.
[ ERROR ] {'status': 'success', 'login': 'success', 'recharge': 'success', 'host': 'error', 'msg': 'error'} {'status': 'success', 'login': 'success', 'recharge': 'expire', 'host': 'error', 'msg': 'error'} {'status': 'online', 'login': 'error', 'recharge': 'error', 'host': 'error', 'msg': 'error'}
Are you satisfied this Answer?