connectseekers.com

Thursday, 16 February 2017

Send SMS Using API Using C# In Asp.Net By HttpWebRequest

2/16/2017 - By Pranav Singh 0


This article will show you how you can send SMS using SMS API using c#.net in asp.net. In this I have used HttpWebRequest for communicating and receiving response from the SMS server.

So first we will create a new asp.net application and a page. In this page add the below code control for mobile no and sms message.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SendOTP.aspx.cs" Inherits="Shoping_Cart.SendOTP" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Send SMS Using API Using C# In Asp.Net By HttpWebRequest</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            Mobile No:
        <asp:TextBox ID="txtMobileNo" runat="server" Width="241px"></asp:TextBox><br />
            Message:
              <asp:TextBox ID="txtMessage" runat="server" Width="241px" Height="112px" TextMode="MultiLine"></asp:TextBox><br />
             <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send Message" />

            <br />
            <asp:Label ID="lblMessage" runat="server" Style="color: #FF0000" Text=""></asp:Label>

        </div>
    </form>
</body>
</html>

Now check the code to send the SMS.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Shoping_Cart
{
    public partial class SendOTP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                //Send message
                string Username = "youremail@domain.com";
                string APIKey = "YourHash";//This may vary api to api. like ite may be password, secrate key, hash etc
                string SenderName = "MyName";
                string Number = "9876543210";
                string Message = txtMessage.Text;
                string URL = "http://api.urlname.in/send/?username=" + Username + "&hash=" + APIKey + "&sender=" + SenderName + "&numbers=" + Number + "&message=" + Message;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);
                HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
                StreamReader sr = new StreamReader(resp.GetResponseStream());
                string results = sr.ReadToEnd();
                sr.Close();
                lblMessage.Text = "Message send successfully.";
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }
        }

    }
}

Here in above code I have user a dummy API key. In this you need to use your own API detail for successful execution.





About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view




Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top