Sunday, January 27, 2013

How to Send SMS With ASP.NET


1. Select the 'File' menu then 'New Project.' This displays the New Project dialog.
2. Select .Net 2.0 or above in the framework selection drop-down menu at the top of the New Project dialog.
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. Name the project with any name you desire. A new project is created in the Visual Studio development environment.
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.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. 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. In the new class, enter the following code:using System.Net.Mail;namespace Communication{public class SMSManager{///
/// Sends an SMS message using the designated SMTP mail server.///
/// The subject of the message being sent.
/// The message body.
/// The SMS e-mail address
///
The SMTP server must be set prior to executing this method.
public void SendMessage(string subject, string messageBody,string smsEmailAddress){MailMessage message = new MailMessage();SmtpClient client = new SmtpClient();//TODO: Add REGEX validation for email addressmessage.To.Add(smsEmailAddress);message.IsBodyHtml = false;// Set the subject and message body textmessage.Subject = subject;message.Body = messageBody;// Send the e-mail messageclient.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.

No comments:

Post a Comment