Library tutorials & articles
Custom Email Control in ASP.NET
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")
'
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
Related articles
Related discussion
-
Gizmox Announces release of Visual WebGui SDK version 6.2.2
by Visual WebGui (0 replies)
-
Error 4 Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
by lbargers (3 replies)
-
How to receive data in web server sending from GPRS modem
by AshokSingh (1 replies)
-
Working with frames in c# .net
by pulak2008 (0 replies)
-
Time Out Problem
by lorrainepetty (1 replies)
Related podcasts
-
CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly Media
CodeCast Episode 4: State of .NET, IE8, ASP.NET MVC, and O'Reilly MediaHosts Ken Levy and Markus Egger discuss the new State of .NET events, IE8, ASP.NET MVC, followed by an interview from PDC with two editors from O'Reilly Media. More on ASP.NET MVC can be found at http://asp.net/mvc. Interview...
Related jobs
-
Microsoft .Net Architect
in AMSTERDAM (€50K-€90K per annum) -
Microsoft Dynamics CRM Technical Consultant
in Netherlands (€50K-€90K per annum) -
.net developer
in Rijswijk (€2K-€4K per annum)
Events coming up
-
Dec
9
Internet Information Services 7.0 for ASP.Net Developers
Glasgow, United Kingdom
One of the biggest and best new features of Windows Server 2008 and Windows Vista is Internet Information Server 7.0. IIS 7.0 is the latest and most significant release of Microsoft's Web Server. With this release comes a new extensibility model which gives developers more options than ever before, more diagnostic tools with which developers can debug and locate issues. During the session Andrew will investigate the new architecture, look at extending, configuring and developing for IIS7, ta...
This thread is for discussions of Custom Email Control in ASP.NET.