Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

This resource has not currently been approved, and is not currently linked to from our directory of resources. It is being displayed here for preview by the author and moderators only.
Rated
Read 4,136 times

Related Categories

Custom Email Control in ASP.NET

intexture

 

Custom Email Control in ASP.NET

 

One of the most powerful features of ASP.NET is its support for custom server controls and components. ASP.NET ships with dozens of built-in controls, and developers can easily extend these controls or write their own controls from scratch. Server controls can be used to encapsulate complex user interface logic or business rules, and can benefit from design-time support like drag-and-drop and toolbox support and property builders. Custom controls pick up where User Controls leave off, providing greater flexibility, reusability, and a better design time experience, but with some added complexity.

 

Sending email from a Web page is the most common functionalities used in web development. A common use of sending email is to send a static pre defined mail message to a designated email address from administrator menu. The .NET Framework makes the task of sending email from a Web page relatively simple. In order to send an email from ASP.NET Web page you need to use the Smtp Mail class found in System.Web.Mail namespace, which contains a static method Send.

 

 

System.Web.Mail namespace needs to be improrted to send an email. We use the SmtpMail and MailMessage classes of this namespace for this purpose. The MailMessage class provides properties and methods for constructing an email message.

 

To create email user control, we need to add a user-control file to a project. First, create a new ASP.NET Web application. Right click on the project in the Project Explorer and click Add - Add New Item and select a Web User Control item. This adds a file with the extension .ascx to the project. This is the file that the user control will use to expose its interface. An .ASCX file cannot be viewed directly in the browser. It will need to be placed within a container (such as another Web form) to be viewed. So, let's add a new Web form to the project, open it, and drag the user-control file onto the page..

    

 

The basic HTML of emailStatic.aspx looks like this

 

<%@ Control Language="VB" EnableViewState="False" Debug="true" Strict="false" %>

<%@ Import Namespace="System.Web.Mail" %>

<SCRIPT language="VB" runat="server">

Sub Page_load(Sender as Object, E as EventArgs)

   

 

    If request.form("EmailAddress") = "" Then

        dim strResponse as string = "<h2>Send Email formatted in HTML</h2>"

        lblMessage.Text = strResponse

    Else

        dim strResponse as string = "You just sent an email message formatted in HTML to:<h2>" & request("EmailAddress") & "</h2>"

        lblMessage.Text = strResponse

    End If

   

End Sub

   

Sub btn_Click(sender as Object, e as System.EventArgs)

 

    If request.form("EmailAddress") <> ""

        Dim mail As New MailMessage

        mail.From = "salman.zafar@xyz.com"

        mail.To = request.form("EmailAddress")

        mail.Subject = "asp.net user controls"

        mail.Body = "mail sent using asp.net user controls"

        mail.BodyFormat = MailFormat.Html

        ' Mail Server Internet Address

        mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver","mail.xyz.com")

 

                                ' MailServerPort Number (Default 25 - which is SMTP 25/TCP)

                                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", "25")

 

 

                                ' There are two levels of Send Usage

                                ' cdoSendUsingPickup = 1 "Send message using the local SMTP service pickup directory."

                                ' cdoSendUsingPort = 2 "Send the message using the network (SMTP over the network)."

                                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", "1")

 

                                ' There are three levels of SMTP Authentication

                                ' cdoAnonymous = 0 "Do not authenticate"

                                ' cdoBasic = 1 "Use basic (clear-text) authentication."

                                ' cdoNTLM = 2 "Use NTLM authentication"

                                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1")

 

                                ' User account and password to authenticate to the server defined above

                                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "YourUserName")

                                mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "YourPassword")

 

    

   SmtpMail.SmtpServer = "mail.xyz.com"

   SmtpMail.Send(mail)

   End If

End Sub

 

</SCRIPT>

<asp:Label id="lblMessage" runat="server" BorderColor="#cccccc" BorderStyle="solid" Width="448px"

                Font-Name="Verdana"></asp:Label>

<FORM name="form1" method="post" runat="server" ID="Form1">

                Email Address:<INPUT style="BACKGROUND-COLOR: #ffffa0" size="30" name="EmailAddress">

                <INPUT id="btnSubmit" type="submit" value="Sending Email " name="b1" runat="server" OnServerClick="btn_Click">

</FORM>

 

 

 

Drag above control in any aspx file and start using emailing with predefined text in ascx file.

 

The HTML code for emailStaticTest.aspx looks like this

 

<%@ Register TagPrefix="uc1" TagName="emailStatic" Src="emailStatic.ascx" %>

<%@ Page language="c#" Codebehind="emailStaticTest.aspx.cs" AutoEventWireup="false" Inherits="CustomControls.emailStatic.emailStaticTest" %>

<uc1:emailStatic id="EmailStatic1" runat="server"></uc1:emailStatic>

 

 

While this control is also quite simple, it lays the foundation for much more complex and innovative user controls. User controls can be very useful for breaking down a large application into smaller, more manageable chunks.

 

 

References

 

http://aspalliance.com/cookbook/

http://www.15seconds.com/Issue/020319.htm

 

 

Sr. Technical Specialist in International Turnkey Systems,Telecom Unit

Comments