21

I'm looking to build a program that would allow me to send SMS messages directly from the C# Application. I intend to build an 'Automatic Appointment Reminder' system that would automatically send SMS messages to recipients' mobile phones notifying them of their upcoming appointment.

Could anyone advise on how I would implement this type of feature as I have no experience in 'Mobile Communications' and mobile connectivity with desktop applications.

My carrier is EE (If that helps?)

Web Developer
  • 333
  • 4
  • 17
Lloyd
  • 435
  • 3
  • 12
  • 29
  • 2
    There are numerous SMS Gateway services on Internet that offer to send SMS for you, The usually have a API to interact with their web services. Otherwise you need to build/purchase a communication library. Look at this answer http://stackoverflow.com/questions/3524742/sms-gateway-for-windows-c-sharp?rq=1 – Steve Jul 06 '15 at 13:00
  • 1
    https://www.twilio.com/ is meant to be a good service. – user1666620 Jul 06 '15 at 13:05
  • 1
    here found sms sending with c# source code https://codecanyon.net/item/sms-sending-receiving-via-modem/29530614 – Maq Nov 27 '20 at 16:39

5 Answers5

22

Most major carriers offer an email to text service. The program can use email to send an SMS message. For example:

Send an email

var message = new MailMessage();
message.From = new MailAddress("sender@foo.bar.com");

message.To.Add(new MailAddress("5551234567@txt.att.net"));//See carrier destinations below
//message.To.Add(new MailAddress("5551234568@txt.att.net"));

//message.CC.Add(new MailAddress("carboncopy@foo.bar.com"));
message.Subject = "This is my subject";
message.Body = "This is the content";

var client = new SmtpClient();
client.Send(message);

Carrier destinations

  • ATT: Compose a new email and use the recipient's 10-digit wireless phone number, followed by @txt.att.net. For example, 5551234567@txt.att.net.
  • Verizon: Similarly, ##@vtext.com
  • Sprint: ##@messaging.sprintpcs.com
  • TMobile: ##@tmomail.net
  • Virgin Mobile: ##@vmobl.com
  • Nextel: ##@messaging.nextel.com
  • Boost: ##@myboostmobile.com
  • Alltel: ##@message.alltel.com
  • EE: ##@mms.ee.co.uk (might support send without reply-to)

Alternatives

  • There are vendors that provide SMS messaging service via an API
P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
11

Twilio has a C# helper library that will let you do this.

Here's the code you'd need to send a text message with the library:

using System;
using Twilio;
class Example
{
  static void Main(string[] args)
  {
    // Find your Account Sid and Auth Token at twilio.com/user/account
    string AccountSid = "{{ account_sid }}";
    string AuthToken = "{{ auth_token }}";

    var twilio = new TwilioRestClient(AccountSid, AuthToken);
    var message = twilio.SendMessage("+14158141829", "+14159352345", "This text message was sent with code!");

    Console.WriteLine(message.Sid);
  }
}

Disclaimer: I work for Twilio.

rickyrobinett
  • 1,224
  • 10
  • 13
6

You can send sms through variety of ways

  • Using a GSM modem
  • Using web service
  • Using endpoints given by service the provider

You can understand the basic logic for each of the above points through the link provided below and try to achieve that in your code.

http://www.codeproject.com/Articles/19023/Sending-SMS-using-NET

You need to create an instance of the sms engine in your form constructor like this.

  public partial class Form1 : Form
    {
        SMSCOMMS SMSEngine;

        public Form1()
        {

                    SMSEngine = new SMSCOMMS("COM1");



            InitializeComponent();
            SMSEngine.Open();
        }

        private void button1_Click(object sender, EventArgs e)
        {
          SMSEngine.SendSMS("919888888888","THIS IS YOUR MESSAGE");
          SMSEngine.Close();
        }
    }
}
BSG
  • 2,084
  • 14
  • 19
  • Still confused if I'm honest @BSG . The code just doesn't want to work. Do you want me to post all of my code in my question? – Lloyd Jul 06 '15 at 13:37
  • Please remove one SendSMS() function which was written twice in that code. Problem is author of that code wrongly created two functions with same name. – BSG Jul 06 '15 at 13:44
  • Well spotted @BSG ! Where do I put this code: SMSEngine = new SMSCOMMS("COM1"); SMSEngine.Open(); SMSEngine.SendSMS("919888888888","THIS IS YOUR MESSAGE"); SMSEngine.Close(); – Lloyd Jul 06 '15 at 13:50
  • If you are creating console application you need to create an instance of the class provided inside the static main method and call the non static class member. Please visit this link for example http://stackoverflow.com/questions/24180470/call-a-non-static-class-with-a-console-application If you are creating windows application you need to call that class instance on button click event or other control events. – BSG Jul 06 '15 at 13:56
  • I'm creating a windows application. I've put the code in Form1_Load but I get an error on SMSENGINE that it does not exist in the current context – Lloyd Jul 06 '15 at 14:01
  • thanks, have just tried that but receiving this error on SMSEngine = newSMSCOMMS("COMM1"): The best overloaded method match for 'SMSCOMMS.SMSCOMMS.SMSCOMMS(ref string)' has some invalid arguments – Lloyd Jul 06 '15 at 14:14
  • While creating the instance of the class, provide the port to which your modem is connected. COMM1 is not correct port string. Replace it as COM1 and check which port is your modem connected. – BSG Jul 06 '15 at 14:22
  • sorry was my typo, it is COM1 in my code. I'm not sure why I need to find the port my modem is connected from? I'm just using my PC with the C# application on it and just accessing the internet via my router...? – Lloyd Jul 06 '15 at 14:24
  • You need to use GSM modem as mentioned in that article. If GSM modem is not available please try other options like Using web service etc – BSG Jul 06 '15 at 14:42
  • I haven't got a GSM Modem unfortunately. So I should use a web service such a Twilio, TextLocal or Plivo etc? Can you recommend one that is totally free (I live in hope!) or the cheapest? or indeed the most reliable/trusted. Many thanks. Lloyd – Lloyd Jul 06 '15 at 14:49
0

Ozeki's C# sms api provides feedback in the form of events. This is great because other SMS api-s don't offer delivered to handset reports or any other real feedback about what happened to your SMS. Here is the code.

using System; using OZX;

namespace OzekiConsoleClient { class Program { static OzxClient Client;

    static void Main(string[] args)
    {
        Client = new OzxClient();
        Client.AutoReconnect = true;

        Client.OnMessageAcceptedForDelivery += Client_OnMessageAcceptedForDelivery;
        Client.OnMessageNotAcceptedForDelivery += Client_OnMessageNotAcceptedForDelivery;
        Client.OnMessageSubmitSuccess += Client_OnMessageSubmitSuccess;
        Client.OnMessageSubmitFailed += Client_OnMessageSubmitFailed;
        Client.OnMessageDeliverySuccess += Client_OnMessageDeliverySuccess;
        Client.OnMessageDeliveryFailed += Client_OnMessageDeliveryFailed;
        Client.OnMessageViewed += Client_OnMessageViewed;
        Client.OnConnected += Client_OnConnected;
        Client.OnDisconnected += Client_OnDisconnected;
     
        Client.Connect("127.0.0.1",9580,"testuser","testpass");
    }
    
    static void Client_OnConnected(object sender, EventArgs e)
    {
        Console.WriteLine("Successfully connected.");
         
        var msg = new OzxMessage();
        msg.ToAddress = "+447958448798";
        msg.Text = "Hello world";

        Console.WriteLine("Sending message. ID: "+msg.ID);
        Client.Send(msg);
    }
}

}

You can also use this code to send an SMS through an Android Mobile instead of subscribing for an On-line SMS service.

Disclaimer: I work for Ozeki.

0

I am using https://d7networks.com/ and the integration is simple

using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json; // Install-Package Newtonsoft.Json

class Program
{
    static async Task Main(string[] args)
    {
        // Set the URL to send the POST request to
        string url = "https://api.d7networks.com/messages/v1/send";

        // Create a new HttpClient object
        HttpClient client = new HttpClient();

        // Set the bearer token authentication header
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "YOUR_API_TOKE"); // Replace YOUR_API_TOKEN with your API token

        // Create a new message object with channel, recipients, and content attributes
        var message = new
        {
            originator = "smsinfo", // Replace smsinfo with your sender ID
            recipients = new string[] { "+97150900XXXX"}, // Replace +97150900XXXX with your recipient's number
            content = "Testing multi lined message. \n This is the second line. \n This is the third line."
        };

        // Nest the message object inside a messages object
        var messages = new
        {
            messages = new[] { message }
        };

        // Convert the messages object to a JSON string
        string json = JsonConvert.SerializeObject(messages);

        // Create a new StringContent object with the JSON string
        var content = new StringContent(json, Encoding.UTF8, "application/json");

        // Send the POST request and get the response
        HttpResponseMessage response = await client.PostAsync(url, content);

        // Print the response status code and message
        Console.WriteLine($"Response status code: {response.StatusCode}");
        Console.WriteLine($"Response message: {await response.Content.ReadAsStringAsync()}");
    }
}