locked
Sending SMS from my web application RRS feed

  • Question

  • User2129316869 posted

    Please i am building a web application that shoul send sms to users on thier birthday automatically. Please how can I accomplish this using ASP.Net Web Pages

    Monday, May 26, 2014 8:05 AM

Answers

  • User281315223 posted

    You'll likely need to use some type of service or SMS Gateway to handle sending SMS messages, which may not always be free (much like sending text messages or phone calls often aren't). The details and implementations may vary for each of them, but you will certainly need an active internet connection (which I am assuming they meant by the modem) to use them (as you would with any sort of web-based technology)

    I'll list a few options for highly recommended SMS Gateways for .NET as well as the code for sending messages for one in particular.

    Overview and Suggestions

    I would recommend checking out the following Stack Overflow discussion on SMS Gateways, which recommended Clickatell. However, there are tons of them out there, so I would suggest doing some research and figuring out what works best for you : 

    Wikipedia also features a fairly comprehensive listing of SMS Gateways as well. 

    An Actual Example

    You will need to use some type of service most likely to perform this, especialy if you are going to be performing it on a large scale.

    SMSified appears to have quite an extensive and friendly API for performing this very task.

    A fully featured example on sending outbound SMS messages using SMSified can be found here and seen below : 

    // SMSified API endpoint.
    string webTarget = "https://api.smsified.com/v1/smsmessaging/outbound/{0}/requests";
                           
    // Parameters to send with API request.
    string webPost = "address={0}&message={1}";
                           
    // SMSified credentials.
    string userName = "";
    string password = "";
    string senderNumber = "";
                           
    // Create new HTTP request.
    string url = String.Format(webTarget, senderNumber);
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "POST";
    req.ContentType =  "application/x-www-form-urlencoded";
    byte[] postData = Encoding.ASCII.GetBytes(String.Format(webPost, "14075551212", "This is a test from C#"));
    req.ContentLength = postData.Length;
                           
    // Set HTTP authorization header.
    string authInfo = userName + ":" + password;
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
    req.Headers["Authorization"] = "Basic " + authInfo;
                           
    // Send HTTP request.
    Stream PostStream = req.GetRequestStream();
    PostStream.Write(postData, 0, postData.Length);
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();

    Keep in mind this is just a specific example using SMSified and others may vary. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 26, 2014 8:16 AM
  • User2103319870 posted

    You can try using Twilio

    Twilio libraries available for .NET: https://github.com/twilio/twilio-csharp

    Sample Code

    static void Main(string[] args)
        {
            TwilioRest.Account account;
            Hashtable h;
    
            // ACCOUNT_SID and ACCOUNT_TOKEN are from your Twilio account
            account = new TwilioRest.Account(ACCOUNT_SID, ACCOUNT_TOKEN);
    
            h = new Hashtable();
            h.Add("From", CALLER_ID);
            h.Add("To", "PHONE NUMBER TO SEND TO");
            h.Add("Body", "The answer is 42");
    
            account.request(String.Format("/{0}/Accounts/{1}/SMS/Messages", API_VERSION, ACCOUNT_SID), "POST", h));
    
        }


    Also Check the below links

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 26, 2014 8:22 AM
  • User2103319870 posted

    Also where would i get the API_VERSION, ACCOUNT_SID and ACCOUNT_TOKEN or what do they stand for. Thanks

    You need to create an account in Twilio for using it and once your register you will get  ACCOUNT_SID and ACCOUNT_TOKEN.

    Check the video available in this link

    http://www.markhagan.me/Samples/Send_SMS_Using_Twilio_ASPNet

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 26, 2014 8:48 AM

All replies

  • User281315223 posted

    You'll likely need to use some type of service or SMS Gateway to handle sending SMS messages, which may not always be free (much like sending text messages or phone calls often aren't). The details and implementations may vary for each of them, but you will certainly need an active internet connection (which I am assuming they meant by the modem) to use them (as you would with any sort of web-based technology)

    I'll list a few options for highly recommended SMS Gateways for .NET as well as the code for sending messages for one in particular.

    Overview and Suggestions

    I would recommend checking out the following Stack Overflow discussion on SMS Gateways, which recommended Clickatell. However, there are tons of them out there, so I would suggest doing some research and figuring out what works best for you : 

    Wikipedia also features a fairly comprehensive listing of SMS Gateways as well. 

    An Actual Example

    You will need to use some type of service most likely to perform this, especialy if you are going to be performing it on a large scale.

    SMSified appears to have quite an extensive and friendly API for performing this very task.

    A fully featured example on sending outbound SMS messages using SMSified can be found here and seen below : 

    // SMSified API endpoint.
    string webTarget = "https://api.smsified.com/v1/smsmessaging/outbound/{0}/requests";
                           
    // Parameters to send with API request.
    string webPost = "address={0}&message={1}";
                           
    // SMSified credentials.
    string userName = "";
    string password = "";
    string senderNumber = "";
                           
    // Create new HTTP request.
    string url = String.Format(webTarget, senderNumber);
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Method = "POST";
    req.ContentType =  "application/x-www-form-urlencoded";
    byte[] postData = Encoding.ASCII.GetBytes(String.Format(webPost, "14075551212", "This is a test from C#"));
    req.ContentLength = postData.Length;
                           
    // Set HTTP authorization header.
    string authInfo = userName + ":" + password;
    authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
    req.Headers["Authorization"] = "Basic " + authInfo;
                           
    // Send HTTP request.
    Stream PostStream = req.GetRequestStream();
    PostStream.Write(postData, 0, postData.Length);
    HttpWebResponse res = (HttpWebResponse)req.GetResponse();

    Keep in mind this is just a specific example using SMSified and others may vary. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 26, 2014 8:16 AM
  • User2103319870 posted

    You can try using Twilio

    Twilio libraries available for .NET: https://github.com/twilio/twilio-csharp

    Sample Code

    static void Main(string[] args)
        {
            TwilioRest.Account account;
            Hashtable h;
    
            // ACCOUNT_SID and ACCOUNT_TOKEN are from your Twilio account
            account = new TwilioRest.Account(ACCOUNT_SID, ACCOUNT_TOKEN);
    
            h = new Hashtable();
            h.Add("From", CALLER_ID);
            h.Add("To", "PHONE NUMBER TO SEND TO");
            h.Add("Body", "The answer is 42");
    
            account.request(String.Format("/{0}/Accounts/{1}/SMS/Messages", API_VERSION, ACCOUNT_SID), "POST", h));
    
        }


    Also Check the below links

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 26, 2014 8:22 AM
  • User2129316869 posted

    Thanks all. I really appreciate

    A2H

    You can try using Twilio

    A2H I hope this will work.

     @{
    TwilioRest.Account account;
            Hashtable h;
    
            // ACCOUNT_SID and ACCOUNT_TOKEN are from your Twilio account
            account = new TwilioRest.Account(ACCOUNT_SID, ACCOUNT_TOKEN);
    
            h = new Hashtable();
            h.Add("From", CALLER_ID);
            h.Add("To", "PHONE NUMBER TO SEND TO");
            h.Add("Body", "The answer is 42");
    
            account.request(String.Format("/{0}/Accounts/{1}/SMS/Messages", API_VERSION, ACCOUNT_SID), "POST", h));
    
        }
    

    That is using in a cshtml tag.

    Also where would i get the API_VERSION, ACCOUNT_SID and ACCOUNT_TOKEN or what do they stand for. Thanks

    Monday, May 26, 2014 8:36 AM
  • User2103319870 posted

    Also where would i get the API_VERSION, ACCOUNT_SID and ACCOUNT_TOKEN or what do they stand for. Thanks

    You need to create an account in Twilio for using it and once your register you will get  ACCOUNT_SID and ACCOUNT_TOKEN.

    Check the video available in this link

    http://www.markhagan.me/Samples/Send_SMS_Using_Twilio_ASPNet

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 26, 2014 8:48 AM
  • User2129316869 posted

    Can I use my own personal phone number for the application? I am just trying to understand how the whole thing works. Thanks a lot

    Monday, May 26, 2014 8:49 AM
  • User379720387 posted

    Yes.

    Once you register with Twilio, you will have at least one phone number (800 555 1212 in the example below).

    That will be the number that the SMS is sent from.

    I  have implemented SMS, voice and transcription with Twilio.

    This is what it takes to send a SMS from Twilio in webPages".

    @using Twilio.WebMatrix;
    Twilio.SendSms("+18005551212", mobileto, body, "");
    


    Monday, May 26, 2014 9:21 AM
  • User2129316869 posted
    @using Twilio;
    @{
        var today = DateTime.Today.Day;
        var tomonth = DateTime.Today.Month;
        var db2 = Database.Open("StarterSite");
        var people = db2.Query("select * from Clients where BirthDay = @0 and BirthMonth = @1", today, tomonth);
        // set our AccountSid and AuthToken
        string AccountSid = "AC45b00a5504e242b8a486ebf4cad405c9";
        string AuthToken = "********************************";
      
        // instantiate a new Twilio Rest Client
        var client = new TwilioRestClient(AccountSid, AuthToken);
      
        // make an associative array of people we know, indexed by phone number
       
        // iterate over all our friends
        foreach (var person in people) {
      
            // Send a new outgoing SMS by POSTing to the Messages resource */
            client.SendMessage(
                "+16308830208", // From number, must be an SMS-enabled Twilio number 
                person.Phone,     // To number, if using Sandbox see note above
                // message content
                string.Format("Hey {0}, Charlesville is wishing u a happy birthday!", person.Name)
            );
     
            Response.Write(string.Format("Sent message to {0}", person.Name));
        }
        client.SendMessage(
                "+16308830208", // From number, must be an SMS-enabled Twilio number 
                "+2347010285254",     // To number, if using Sandbox see note above
            // message content
                string.Format("Hey {0}, Charlesville is wishing u a happy birthday!", "Chris")
            );
       
    }

    The above is what i have been able to prepare but i dont recieve sms

    Monday, May 26, 2014 11:53 AM
  • User379720387 posted

    Log in to Twilio and look at the log. See if it was sent.

    The dashboard may show an error message.

    Be careful Twilio formats their log for readability. The webpage and the export to .CSV all show something different. Only the call to the API will show you what really happened.

    Monday, May 26, 2014 2:30 PM
  • User2129316869 posted

    Another issue I am having is thou I ve not gotten that one fixed is that, I need my clients to see the name of the Company as the sender rather than the phone number. Thanks.

    Tuesday, May 27, 2014 3:45 AM
  • User2129316869 posted

    Even thou my code is not working I am trying to send automatic birthday messages to clients. Please you can help me review the code i posted to at least make sure that the only issue am having is just sending the message and not both message and the code. Thanks

    Tuesday, May 27, 2014 4:19 AM
  • User379720387 posted

    That is functionality on the mobile device.

    Tell your clients to add your service number to their contacts.

    Tuesday, May 27, 2014 7:57 AM
  • User379720387 posted

    Have you looked at the logs?

    Tuesday, May 27, 2014 7:58 AM
  • User2129316869 posted

    Yes I have looked at my logs and the SMS isn't there just the onesTwilio used in testing the numbers. Thanks WAVE

    Tuesday, May 27, 2014 8:26 AM
  • User-419562455 posted

    Please refer below link :

    http://sms-asp-net.blogspot.in/

    Tuesday, May 27, 2014 8:34 AM
  • User379720387 posted

    Why are you not using the Twilio Webmatrix helper?

    Tuesday, May 27, 2014 11:06 AM
  • User2129316869 posted

    Now you are talking Wave. Please I don't know about it. Can you help me with how to use it and how to use other SMS helpers if any. Thanks

    Tuesday, May 27, 2014 11:11 AM
  • User2129316869 posted

    Error 1 The type name 'WebMatrix' does not exist in the type 'Twilio.WebMatrix.Twilio'.

    I get that error at @using Twilio.Webmatrix and i have installed it from nuget package

    Tuesday, May 27, 2014 11:20 AM
  • User2129316869 posted

    ok this is what I have done but no responce still. I am really stuck at what i do not even know. Thanks

    @{
        
    }
    
    @using Twilio.WebMatrix;
    @{
    if (IsPost) {
    Twilio.AccountSid="AC45b00a5504e242b8a486ebf4cad405c9";
    Twilio.AuthToken = "*****************************";
     
    Twilio.SendSms("+16308830208", "+2347010285254", Request.Form["body"], "");
    }
    }
    
    <form method="post">
    <input type="text" name="body" />
        <input type="submit">
    </form>

    Tuesday, May 27, 2014 11:40 AM
  • User379720387 posted

    Ignore that error.

    I have got the same one all over my pages that use Twilio, everything works.

    Tuesday, May 27, 2014 11:40 AM