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
-
Export Datagrid to Excel with same formatting
by BarbaMariolino (1 replies)
-
how to design a template platform which can be used to create many different pages?
by polytheme (1 replies)
-
sending sms from pc
by sriraj20074 (0 replies)
-
Capture Video
by ess-image (23 replies)
-
DataGrid - How to display the content of a hidden TemplateField on mouseover
by DonCarnage (0 replies)
Related podcasts
-
StackOverflow uses ASP.NET MVC - Jeff Atwood and his technical team
Scott chats with Jeff Atwood of CodingHorror.com and most recently, StackOverflow.com. Jeff and Joel Spolsky and their technical team have created a new class of application using ASP.NET MVC. What works, what doesn't, and how did it all go down?
Events coming up
-
Jul
7
DTC 70-528 Session 7: Chapter 12
Greenwood Village, United States
Due to lack of interest of the 5th Monday meetup, we will continue as originally scheduled. The topic of the night will be "Chapter 12 - Creating ASP.NET Mobile Web Applications", taught by RJ Hatch. It is a fairly small chapter, so we can discuss other topics as well. Pizza and beverages will be provided on a donation basis.
This thread is for discussions of Custom Email Control in ASP.NET.