How to Send SMS With ASP.NET

Written by jay rigsby
  • Share
  • Tweet
  • Share
  • Pin
  • Email
How to Send SMS With ASP.NET
(Jupiterimages/Photos.com/Getty Images)

SMS, Short Message Service, allows messages of up to 160 characters to be sent and received on mobile devices. If messages contain non-Latin characters, such as the Arabic alphabet, the limit is 70 characters. Some mobile providers support sending a longer SMS by splitting the long content into multiple messages containing fewer than the maximum number of characters. In ASP.Net, an SMS can be sent by communicating with a GPRS modem connected to the machine, using an SMS gateway or Web service, and sending an e-mail to an e-mail-to-SMS gateway. There is typically a cost associated with the first two approaches, although a few free SMS gateways exist. The last approach, e-mail to SMS, uses native .Net functionality can be done for free.

Skill level:
Moderate

Other People Are Reading

Things you need

  • Visual Studio 2005 or above
  • .Net 2.0
  • SMTP server

Show MoreHide

Instructions

  1. 1

    Select the "File" menu then "New Project." This displays the New Project dialogue.

  2. 2

    Select .Net 2.0 or above in the framework selection drop-down menu at the top of the New Project dialogue.

  3. 3

    Select "Visual C#" then "Web" in the Project Types selection area. This will display a list of installed Web templates for Visual C#. Select "ASP.NET Empty Web Application."

  4. 4

    Name the project with any name you desire. A new project is created in the Visual Studio development environment.

  5. 5

    Create a "system.net" section in the Web application configuration file, web.config, if one does not exist. Enter the following code:

    <system.net>

    &lt;mailSettings&gt;
    

    <smtp>

             &lt;network host="stmp server address or ip" port="port number" username="username" password="secret" defaultCredentials="true" /&gt;
    
      &lt;/smtp&gt;
    
    &lt;/mailSettings&gt;
    

    </system.net>

    System.Net.Mail reads SMTP configuration data out of the standard .NET configuration system. Replace the attribute values on the network element with the values appropriate for the SMTP server that will be used to send the message.

  6. 6

    Right-click the project name and select "Add A Class." Name the class SMSManager or a similar name that adheres to your development standards.

  7. 7

    In the new class, enter the following code:

    using System.Net.Mail;

    namespace Communication

    {

    public class SMSManager
    
    {
    
        /// &lt;summary&gt;
    
        /// Sends an SMS message using the designated SMTP mail server.
    
        /// &lt;/summary&gt;
    
        /// &lt;param name="subject"&gt;The subject of the message being sent.&lt;/param&gt;
    
        /// &lt;param name="messageBody"&gt;The message body.&lt;/param&gt;
    
        /// &lt;param name="smsEmailAddress"&gt;The SMS e-mail address &lt;/param&gt;
    
        /// &lt;remarks&gt;The SMTP server must be set prior to executing this method.&lt;/remarks&gt;
    
    
    
        public  void SendMessage(string subject, string message Body,string smsEmailAddress)
    
        {
    
            MailMessage message = new MailMessage();
    
            SmtpClient client = new SmtpClient();
    
    
    
            //TODO: Add REGEX validation for e-mail address
    
            message.To.Add(smsEmailAddress);
    
    
    
            message.IsBodyHtml = false;
    
    
    
            // Set the subject and message body text
    
            message.Subject = subject;
    
            message.Body = message Body;
    
    
    
    
    
            // Send the e-mail message
    
            client.Send(message);
    
        }
    
    }
    

    }

    This code connects to the SMTP server specified in configuration and sends a message. The email address the message is sent to, smsEmailAddress parameter, is the email address for the SMTP to SMS gateway provided by the wireless carrier. Contact the wireless carrier for the address or use the email address list provided in the reference of this article.

Tips and warnings

  • Carriers periodically change the email address for their SMTP to SMS gateways. You should validate that the email address used periodically.

Don't Miss

Filter:
  • All types
  • Articles
  • Slideshows
  • Videos
Sort:
  • Most relevant
  • Most popular
  • Most recent

No articles available

No slideshows available

No videos available

By using the eHow.co.uk site, you consent to the use of cookies. For more information, please see our Cookie policy.