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.
0 comments:
Please let me know your view