Can I send SMS Messages from a C# Application?

59,135

Solution 1

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("[email protected]");

message.To.Add(new MailAddress("[email protected]"));//See carrier destinations below
//message.To.Add(new MailAddress("[email protected]"));

//message.CC.Add(new MailAddress("[email protected]"));
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, [email protected].
  • 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

Solution 2

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.

Solution 3

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();
        }
    }
}
Share:
59,135
Lloyd
Author by

Lloyd

Updated on July 05, 2022

Comments

  • Lloyd
    Lloyd almost 2 years

    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?)

  • Lloyd
    Lloyd almost 9 years
    Thanks for your post. My carrier is EE though?
  • yairr
    yairr almost 9 years
    @lloyd - Updated post. See this thread for more information.
  • Lloyd
    Lloyd almost 9 years
    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?
  • BSG
    BSG almost 9 years
    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.
  • Lloyd
    Lloyd almost 9 years
    Well spotted @BSG ! Where do I put this code: SMSEngine = new SMSCOMMS("COM1"); SMSEngine.Open(); SMSEngine.SendSMS("919888888888","THIS IS YOUR MESSAGE"); SMSEngine.Close();
  • BSG
    BSG almost 9 years
    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 stackoverflow.com/questions/24180470/… If you are creating windows application you need to call that class instance on button click event or other control events.
  • Lloyd
    Lloyd almost 9 years
    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
    Lloyd almost 9 years
    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
  • BSG
    BSG almost 9 years
    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.
  • Lloyd
    Lloyd almost 9 years
    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...?
  • BSG
    BSG almost 9 years
    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
  • Lloyd
    Lloyd almost 9 years
    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
  • B. Clay Shannon-B. Crow Raven
    B. Clay Shannon-B. Crow Raven over 6 years
    So you have to know the text recipient's carrier in each instance? Is there a way to figure that out based on their email address?
  • David Smith
    David Smith over 5 years
    Works without any additional dependencies. I can vouch it works for Verizon phones. Yes, the downside is you have to know the recipient's carrier.
  • yairr
    yairr almost 5 years
    These days you can use a provider like AWS SNS to avoid knowledge of carriers: docs.aws.amazon.com/sns/latest/dg/…
  • Fayeure
    Fayeure about 3 years
    And what library are you using to do that?