Breaking News

Editors Picks

Monday, July 19, 2010

how to send sms from asp.net


Code For Default.aspx Page


<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="smssend.aspx.cs" Inherits="_Default" validateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>sunilgurjar.blogspot SMS Gateway Message Sending Example</title>
</head>
<body>
    <center>
    <form id="smsdata" runat="server">
        <asp:Table id="smstable" runat="server" style="text-align:left; border-width:thin; border-color:Silver;" BorderStyle="Solid">
            <asp:TableRow>
                <asp:TableCell ColumnSpan="2">
                    <b>Compose a message:</b>
                    <br />
                    <br />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell HorizontalAlign="Left" VerticalAlign="Top">
                    <asp:Label ID="labelRecipient" runat="server" Text="Recipient: "></asp:Label>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:TextBox ID="textboxRecipient" runat="server"></asp:TextBox>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell HorizontalAlign="Left" VerticalAlign="Top">
                    <asp:Label ID="labelMessage" runat="server" Text="Message Text: "></asp:Label>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:TextBox ID="textboxMessage" runat="server" TextMode="MultiLine"></asp:TextBox>
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell ColumnSpan="2" HorizontalAlign="Center">
                    <asp:Button ID="buttonSend" runat="server" Text="Send Message" OnClick="buttonSendOnClick" />
                </asp:TableCell>
            </asp:TableRow>
            <asp:TableRow>
                <asp:TableCell ColumnSpan="2" HorizontalAlign="Center">
                    <asp:TextBox ID="textboxError" runat="server" BorderStyle="None" TextMode="MultiLine"></asp:TextBox>
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </form>
    </center>
</body>
</html>

Code For Default.aspx.cs Page

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Net.Mail;
using System.Text.RegularExpressions;

public partial class _Default : System.Web.UI.Page
{
   

    protected void Page_Load(object sender, EventArgs e)
    {
        textboxRecipient.Width = 400;
        textboxMessage.Width = 450;
        textboxMessage.Rows = 10;
        textboxError.Width = 400;
        textboxError.Rows = 5;

        textboxError.ForeColor = System.Drawing.Color.Red;
        textboxError.Visible = false;
        textboxError.Text = "";

        if (!Page.IsPostBack)
        {
            textboxRecipient.Text = "8010771150";
            textboxMessage.Text = "Hello World!";
        }
    }

    protected void buttonSendOnClick(object sender, EventArgs e)
    {
        //are required fields filled in:
        if (textboxRecipient.Text == "")
        {
            textboxError.Text += "Recipient(s) field must not be empty!\n";
            textboxError.Visible = true;
            return;
        }

        //we creating the necessary URL string:
        string SunilSURL = "http://127.0.0.1"; //where sunilgurjar.blogspot SMS Gateway is running
        string SunilSPort = "9501"; //port number where sunilgurjar.blogspot SMS Gateway is listening
        string SunilUser = HttpUtility.UrlEncode("admin"); //username for successful login
        string SunilPassw = HttpUtility.UrlEncode("admin"); //user's password
        string SunilMessageType = "SMS:TEXT"; //type of message
        string SunilRecipients = HttpUtility.UrlEncode(textboxRecipient.Text); //who will get the message
        string SunilMessageData = HttpUtility.UrlEncode(textboxMessage.Text); //body of message

        string createdURL = SunilSURL + ":" + SunilSPort + "/httpapi" +
            "?action=sendMessage" +
            "&username=" + SunilUser +
            "&password=" + SunilPassw +
            "&messageType=" + SunilMessageType +
            "&recipient=" + SunilRecipients +
            "&messageData=" + SunilMessageData;

        try
        {
            //Create the request and send data to sunilgurjar.blogspot SMS Gateway Server by HTTP connection
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(createdURL);

            //Get response from sunilgurjar.blogspot SMS Gateway Server and read the answer
            HttpWebResponse myResp = (HttpWebResponse)myReq.GetResponse();
            System.IO.StreamReader respStreamReader = new System.IO.StreamReader(myResp.GetResponseStream());
            string responseString = respStreamReader.ReadToEnd();
            respStreamReader.Close();
            myResp.Close();

            //inform the user
            textboxError.Text = responseString;
            textboxError.Visible = true;
        }
        catch (Exception)
        {
            //if sending request or getting response is not successful sunilgurjar.blogspot SMS Gateway Server may do not run
            textboxError.Text = "sunilgurjar.blogspot  SMS Gateway Server is not running!";
            textboxError.Visible = true;
        }

    }
}


Read more ...

Sending Email with ASP.NET



Code For Default.aspx Page


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table width="100%" cellpadding="2" cellspacing="2" class="tdtable">


<tr>
<td class="tdbigblock">Your Name*:</td>
<td class="headerleft">
    <asp:TextBox ID="txtName" runat="server" Width="250px"></asp:TextBox>   
    <asp:RequiredFieldValidator ID="rfvName" runat="server"
        ControlToValidate="txtName" ErrorMessage="name can not be empty"
        Display="None" ValidationGroup="submit"></asp:RequiredFieldValidator>
    <cc1:ValidatorCalloutExtender ID="vceName" runat="server"
        TargetControlID="rfvName">
    </cc1:ValidatorCalloutExtender>
    </td>
</tr>
<tr>
  <td colspan="2" class="tdtextwhite">&nbsp;</td>
  </tr>
<tr>
<td class="tdbigblock">Your Email*:</td>
<td class="headerleft">
    <asp:TextBox ID="txtEmail" runat="server" Width="250px"></asp:TextBox>   
    <asp:RegularExpressionValidator ID="revEmail" runat="server"
        ControlToValidate="txtEmail" Display="None"
        ErrorMessage="Enter correct email address"
        ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ValidationGroup="submit"></asp:RegularExpressionValidator>
    <asp:RequiredFieldValidator ID="rfvEmail" runat="server"
        ControlToValidate="txtEmail" ErrorMessage="email can not be empty"
        Display="None" ValidationGroup="submit"></asp:RequiredFieldValidator>
    <cc1:ValidatorCalloutExtender ID="vceEmail" runat="server"
        TargetControlID="rfvEmail">
    </cc1:ValidatorCalloutExtender>
    <cc1:ValidatorCalloutExtender ID="vceyouremail" runat="server"
        TargetControlID="revEmail">
    </cc1:ValidatorCalloutExtender>
    </td>
</tr>
<tr>
  <td colspan="2" class="tdtextwhite">&nbsp;</td>
  </tr>
<tr>
<td class="tdbigblock">Contact No.*:</td>
<td class="headerleft">
    <asp:TextBox ID="txtContactNo" runat="server" Width="250px"></asp:TextBox>   
    <asp:RequiredFieldValidator ID="rfvMobile" runat="server"
        ControlToValidate="txtContactNo" Display="None"
        ErrorMessage="Mobile number can not empty" ValidationGroup="\d{10}"></asp:RequiredFieldValidator>
    <asp:RequiredFieldValidator ID="rfvContactNo" runat="server"
        ControlToValidate="txtContactNo"
        ErrorMessage="contact no can not be empty" Display="None" ValidationGroup="submit"></asp:RequiredFieldValidator>
    <cc1:ValidatorCalloutExtender ID="vceMobileno" runat="server"
        TargetControlID="rfvContactNo">
    </cc1:ValidatorCalloutExtender>
    <cc1:ValidatorCalloutExtender ID="vceContactno" runat="server"
        TargetControlID="rfvMobile">
    </cc1:ValidatorCalloutExtender>
    </td>
</tr>
<tr>
  <td colspan="2" class="tdtextwhite">&nbsp;</td>
  </tr>
<tr>
<td class="tdbigblock">Subject*:</td>
<td class="headerleft">
    <asp:TextBox ID="txtSubject" runat="server" Width="250px"></asp:TextBox>   
    <asp:RequiredFieldValidator ID="rfvSubject" runat="server"
        ControlToValidate="txtSubject" ErrorMessage="Subject can not be empty"
        Display="None" ValidationGroup="submit"></asp:RequiredFieldValidator>
    <cc1:ValidatorCalloutExtender ID="vceSubject" runat="server"
        TargetControlID="rfvSubject">
    </cc1:ValidatorCalloutExtender>
    </td>
</tr>
<tr>
  <td colspan="2" class="tdtextwhite">&nbsp;</td>
  </tr>
<tr>
<td class="tdbigblock">Message*:</td>
<td class="headerleft">
    <asp:TextBox ID="txtMessage" runat="server" TextMode="MultiLine" Width="300px"></asp:TextBox>   
    <cc1:ValidatorCalloutExtender ID="vceMessage" runat="server"
        TargetControlID="rfvMessage">
    </cc1:ValidatorCalloutExtender>
    <asp:RequiredFieldValidator ID="rfvMessage" runat="server"
        ErrorMessage="message can not be empty" ControlToValidate="txtMessage"
        Display="None" ValidationGroup="submit"></asp:RequiredFieldValidator>
    </td>
</tr>
<tr>
  <td colspan="2" class="tdtextwhite">&nbsp;</td>
  </tr>
<tr>
  <td colspan="2"></td>
  </tr>
<tr>
<td></td>
<td>
    <asp:Button ID="btnSubmit" runat="server" Text="Submit"
        onclick="btnSubmit_Click" ValidationGroup="submit" />    </td>
</tr>
<tr>
<td></td>
<td>
    <asp:Label ID="lblMessage" runat="server"></asp:Label>
    </td>
</tr>
</table>
    </div>
    </form>
</body>
</html>


 Code For Default.aspx.cs page


using System;
using System.Collections;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net.Mail;

public partial class Contactus : System.Web.UI.Page
{
    #region "Variables"

    #endregion
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    /// <summary>
    /// Method to clear vales of all control of the form
    /// </summary>
    public void ClearAllControl()
    {
        txtName.Text = "";
        txtEmail.Text = "";
        txtContactNo.Text = "";
        txtSubject.Text = "";
        txtMessage.Text = "";
    }
    /// <summary>
    /// Method to send vales of all control of the form in email
    /// </summary>
    public void SendMailContactusDetail()
    {
        if (txtEmail.Text != "")
        {
            MailMessage msg = new MailMessage();
            SmtpClient smtp = new SmtpClient("mail.yourSMTPSERVER.com ");// SmtpClient smtp = new SmtpClient("mail.gmail.com");
            msg.To.Add("sunilgurjr@gmail.com");
            msg.To.Add("sunilgurjr@gmail.com ");
            msg.From = new MailAddress(this.txtEmail.Text);
            msg.Subject = this.txtSubject.Text;
            msg.IsBodyHtml = true;
            System.Net.NetworkCredential Netcred = new System.Net.NetworkCredential();
            Netcred.UserName = "your UserName Email ID";
            Netcred.Password = "Your Password";

            smtp.UseDefaultCredentials = true;
            smtp.Credentials = Netcred;

        
            msg.Priority = MailPriority.High;
            msg.Body = "<html><body><table border=0 cellpadding=4 cellspacing=4 width=100%>" +
            "<tr><td><b><font size=5>Requirement Send User</font></b></td></tr> " +
            "<tr><td><b>Name</b></td><td>----</td><td>" + this.txtName.Text + " </td></tr> " +
            "<tr><td><b>Email Address</b></td><td>----</td><td>" + this.txtEmail.Text + "</td></tr> " +
            "<tr><td><b>Phone Number</b></td><td>---</td><td>" + this.txtContactNo.Text + " </td></tr> " +
            "<tr><td><b>Subject</b></td><td>---</td><td>" + this.txtSubject.Text + " </td></tr> " +
            "<tr><td><b>Message</b></td><td>---</td><td><p align=justify>" + this.txtMessage.Text + "</td></tr></table></body></html>";
            try
            {

                smtp.Send(msg);
                Page.RegisterStartupScript("Reminder1", "<script> alert(' Thanks for Sending your Enquiry ')</script>");
                txtName.Text = "";
                txtEmail.Text = "";
                txtContactNo.Text = "";
                txtSubject.Text = "";
                txtMessage.Text = "";
            }
            catch (Exception ex)
            {

            }
        }
        else
        {
            Response.Write("<script>alert('Please Enter Email Address')</script>");
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
      
            SendMailContactusDetail();
            ClearAllControl();
                
    }
}


Read more ...

Contact Us

Name

Email *

Message *