Can I send SMS Messages from a C# Application?
Asked Answered
V

4

22

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

Vallo answered 6/7, 2015 at 12:58 Comment(3)
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 #3525242Athirst
twilio.com is meant to be a good service.Aklog
here found sms sending with c# source code codecanyon.net/item/sms-sending-receiving-via-modem/29530614Wexford
F
25

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
Fourpence answered 6/7, 2015 at 13:20 Comment(7)
Thanks for your post. My carrier is EE though?Vallo
@lloyd - Updated post. See this thread for more information.Fourpence
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?Glyconeogenesis
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.Fewness
These days you can use a provider like AWS SNS to avoid knowledge of carriers: docs.aws.amazon.com/sns/latest/dg/…Fourpence
And what library are you using to do that?Dead
can the body have html tags in it?Wooldridge
M
12

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.

Marelda answered 6/10, 2015 at 14:52 Comment(0)
S
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();
        }
    }
}
Stow answered 6/7, 2015 at 13:3 Comment(10)
Still confused if I'm honest @Stow . The code just doesn't want to work. Do you want me to post all of my code in my question?Vallo
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.Stow
Well spotted @Stow ! Where do I put this code: SMSEngine = new SMSCOMMS("COM1"); SMSEngine.Open(); SMSEngine.SendSMS("919888888888","THIS IS YOUR MESSAGE"); SMSEngine.Close();Vallo
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 #24180970 If you are creating windows application you need to call that class instance on button click event or other control events.Stow
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 contextVallo
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 argumentsVallo
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.Stow
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...?Vallo
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 etcStow
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. LloydVallo
L
-1

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.

Lepsy answered 9/5, 2021 at 21:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.