Mobile Mail sending Application using ASP.NET

0 comments
This program will allow the user to send mail from his mobile device. Here we use a mail server and make use of the classes provided in the System.Web.Mail namespace. We also make elaborate use of validation controls. 


Listing 1.1: Mail Sending Application 


<%
@ Page Inherits="System.Web.UI.MobileControls.MobilePage"Language="C#" %>
<%
@ Register TagPrefix="mobile"Namespace="System.Web.UI.MobileControls"Assembly="System.Web.Mobile" %>
<%
@ Import Namespace="System.Web.Mail" %>
<
script language="c#" runat="server">
    protected void Submit_OnClick(Object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            /// Make an object of MailMessage Class            MailMessage message = new MailMessage();

            ///Set the Fields            message.To = to.Text;
            message.From = from.Text;
            message.Subject = subject.Text;

            ///Set u r Mail Server here            SmtpMail.SmtpServer = "c-sharpcorner.com";

            /// This will send the Message            SmtpMail.Send(message);

            /// Give the Confirmation as the Message has been sent            confirm.Text = "Mail has been sent to " + to.Text;

            //Activate Second Form            this.ActiveForm = Form2;
        }
    }

</
script>

Listing 1.1 is the final action in the example. When the user submits the form, the Submit_OnClick method is called, and once the message is e-mailed, Form2 is activated. But first we have to get input from the user for both the To and From fields. We will limit the data we collect, as it is difficult to type on a phone (see Listing 1.2). 

Listing 1.2 : Mail Sending: User Interface 


<
mobile:form id="Form1" runat="server">
<
mobile:Label runat="server">FROM :
</
mobile:Label>
<
mobile:TextBox id="from" runat="server"/>
<
mobile:RequiredFieldValidator
id
="FromValidator1"
ControlToValidate
="from" runat="server">Enter from address
</
mobile:ReuiredFieldValidator>
<
mobile:RegularExpressionValidator
id
="FromValidator2"
ControlToValidate
="from"
ValidationExpression
="\w+([-+.]\w+)*@\w+([-.]\w+)
*\.\w+([-.]\w+)*"

runat
="server">
Enter a valid Email Id

</
mobile:RegularExpressionValidator>
<
mobile:Label runat="server">TO:
</
mobile:Label>
<
mobile:TextBox id="to" runat="server"/>
<
mobile:RequiredFieldValidator
id
="toValidator1"
ControlToValidate
="to" runat="server">Enter to address
</
mobile:RequiredFieldValidator>
<
mobile:RegularExpressionValidator
ControlToValidate
="to"
ValidationExpression
="\w+([-+.]\w+)*@\w+
([-.]\w+)*\.\w+([-.]\w+)*"

runat
="server">
Enter a valid Email Id

</
mobile:RegularExpressionValidator>
<
mobile:Label runat="server">MESSAGE:
</
mobile:Label>
<
mobile:TextBox id="subject" runat="server"/>
<
mobile:Command runat="server" OnClick="Submit_OnClick">Submit
</
mobile:Command>
</
mobile:form>
<
mobile:form id="Form2" runat="server">
<
mobile:Label id ="confirm" runat="server">
</
mobile:Label>
</
mobile:form>

The validators will allow the Submit function to be called only after the page has been validated. Do not forget the IsValid property, as was mentioned earlier. 

0 comments: