Sending Email Using ASP.NET Server Control

ASP.NET user controls provide rich functionality and easy reuse of code but limitations come in when you have to reuse that control in several different website applications.

When you have a control that you are going to use in several different applications then it's a wise idea to build server control library and either add reference to the application or add that control library's dll file to bin directory of web application.

In order to get started we first need to understand what our needs are because server control does not provide design time control building environment as user control does. In case of server controls you have to build it from ground up so it usually involves generating raw HTML from your controls. While building server control you have two choices, one inherit from web control class and build your server control using raw HTML and user interface provided by you, other is by using composite control class, this class provide access to all standard asp.net server controls. Inheriting your server control provide additional benefit because you get access to all events of standard server controls.

We will use composite control class to build our server control.

To get started create a new class library (you can use ASP.NET Server Control library but I prefer using core class library if you wish to use other, feel free to do so). Once project is created Visual Studio will create a default class1.cs file. Delete this file and add a new class with name "ContactUsServerControl.cs". After creating this file add reference to following libraries:

System.Web, System.Drawing

Once these references update using section as following:

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Drawing;

using System.ComponentModel;

using System.Net.Mail;

using System.Net;

 

Add a namespace to your project as:

namespace CustomControlLibrary

 

Now we will start our class definition and inherit this class from Composite Control class (because we need this control to be accessible to outside world we will create it as public )

public class ContactUsServerControl : CompositeControl

 

Once added we will define some properties for our server control so that user can set those properties from his property box or using server control markup:

public string Name

{

get

{

TextBox txtName = (TextBox)FindControl("txtboxName");

return txtName.Text;

}

set

{

TextBox txtName = (TextBox)FindControl("txtboxName");

txtName.Text = value;

}

}

public string Email

{

get

{

TextBox txtEmail = (TextBox)FindControl("txtboxEmail");

return txtEmail.Text;

}

set

{

TextBox txtEmail = (TextBox)FindControl("txtboxEmail");

txtEmail.Text = value;

}

}

public string Subject

{

get

{

TextBox txtSubject = (TextBox)FindControl("txtboxSubject");

return txtSubject.Text;

}

set

{

TextBox txtSubject = (TextBox)FindControl("txtboxSubject");

txtSubject.Text = value;

}

}

public string Message

{

get

{

TextBox txtMessage = (TextBox)FindControl("txtboxMessage");

return txtMessage.Text;

}

set

{

TextBox txtMessage = (TextBox)FindControl("txtboxMessage");

txtMessage.Text = value;

}

}

 

public string MailFrom { get; set; }

public string MailTo { get; set; }

public string SMTPServerAddress { get; set; }

public SmtpDeliveryMethod DeliveryUsing { get; set; }

public string SMTPUserName { get; set; }

public string SMTPPassword { get; set; }

public int PortNumber { get; set; }

public bool IsSSLConnectionAvailable { get; set; }

public bool IsEmailHtml { get; set; }

 

After adding these properties we will create one panel control to hold our other controls and then add our asp.net server controls to this panel control:

    Panel pnl = new Panel();

TextBox txtboxName = new TextBox();

TextBox txtboxEmail = new TextBox();

TextBox txtboxSubject = new TextBox();

TextBox txtboxMessage = new TextBox();

Button btnSend = new Button();

Button btnReset = new Button();

 

While creating server control inherited from composite control we will add our functionality and layout in CreateChildControl, so we will override this control with our functionality as follows:

protected override void CreateChildControls()

{

btnSend.Click += new EventHandler(btnSend_Click);

btnReset.Click += new EventHandler(btnReset_Click);

Controls.Add(pnl);

pnl.Controls.Add(new LiteralControl("<table><tr><td>"));

pnl.Controls.Add(new LiteralControl("Your Name: "));

pnl.Controls.Add(new LiteralControl("</td><td>"));

pnl.Controls.Add(txtboxName);

pnl.Controls.Add(new LiteralControl("</td></tr><tr><td>"));

pnl.Controls.Add(new LiteralControl("Your Email: "));

pnl.Controls.Add(new LiteralControl("</td><td>"));

pnl.Controls.Add(txtboxEmail);

pnl.Controls.Add(new LiteralControl("</td></tr><tr><td>"));

pnl.Controls.Add(new LiteralControl("Subject: "));

pnl.Controls.Add(new LiteralControl("</td><td>"));

pnl.Controls.Add(txtboxSubject);

pnl.Controls.Add(new LiteralControl("</td></tr><tr><td>"));

pnl.Controls.Add(new LiteralControl("Your Message: "));

pnl.Controls.Add(new LiteralControl("</td><td>"));

pnl.Controls.Add(txtboxMessage);

pnl.Controls.Add(new LiteralControl("</td></tr><tr><td colspan='2'>"));

pnl.Controls.Add(btnSend);

pnl.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));

pnl.Controls.Add(btnReset);

pnl.Controls.Add(new LiteralControl("</td></tr></table>"));

pnl.Style.Add("background-color","lightgray");

pnl.Style.Add("width","600px");

txtboxName.ID = "txtboxName";

txtboxName.Style.Add("width","350px");

txtboxEmail.ID = "txtboxEmail";

txtboxEmail.Style.Add("width", "350px");

txtboxSubject.ID = "txtboxSubject";

txtboxSubject.Style.Add("width", "350px");

txtboxMessage.ID = "txtboxMessage";

txtboxMessage.TextMode = TextBoxMode.MultiLine;

txtboxMessage.Columns = 42;

txtboxMessage.Rows = 5;

btnSend.ID = "btnSend";

btnSend.Text = "Send Email";

btnReset.ID = "btnReset";

btnReset.Text = "Reset";

}

 

 

As mentioned earlier, with asp.net controls present in our server control we can also take advantage of their standard events then in this example we will use button controls click event and will add send email functionality on these event as shown below:

void btnSend_Click(object sender, EventArgs e)

{

MailMessage message = new MailMessage();

message.From = new MailAddress(MailFrom);

message.To.Add(new MailAddress(MailTo));

message.Subject = string.Format("Message from: {0}, {1}", Name, Email);

SmtpClient smtp = new SmtpClient(SMTPServerAddress);

smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

smtp.Credentials = new NetworkCredential(SMTPUserName, SMTPPassword);

smtp.Port = PortNumber;

smtp.EnableSsl = IsSSLConnectionAvailable;

message.IsBodyHtml = IsEmailHtml;

message.Body = string.Format(@"This message is from <a mailto:'{0}'>{1}</a>. <hr />{2}", Email, Name, Message);

smtp.Send(message);

}

 

Along with this we will create another event for reset button so we can clear all textboxes as user click on this button:

void btnReset_Click(object sender, EventArgs e)

{

txtboxName.Text = "";

txtboxEmail.Text = "";

txtboxSubject.Text = "";

txtboxMessage.Text = "";

}

 

Once all this is complete, your complete class code should look like this:

namespace CustomControlLibrary

{

public class ContactUsServerControl : CompositeControl

{

public string Name

{

get

{

TextBox txtName = (TextBox)FindControl("txtboxName");

return txtName.Text;

}

set

{

TextBox txtName = (TextBox)FindControl("txtboxName");

txtName.Text = value;

}

}

public string Email

{

get

{

TextBox txtEmail = (TextBox)FindControl("txtboxEmail");

return txtEmail.Text;

}

set

{

TextBox txtEmail = (TextBox)FindControl("txtboxEmail");

txtEmail.Text = value;

}

}

public string Subject

{

get

{

TextBox txtSubject = (TextBox)FindControl("txtboxSubject");

return txtSubject.Text;

}

set

{

TextBox txtSubject = (TextBox)FindControl("txtboxSubject");

txtSubject.Text = value;

}

}

public string Message

{

get

{

TextBox txtMessage = (TextBox)FindControl("txtboxMessage");

return txtMessage.Text;

}

set

{

TextBox txtMessage = (TextBox)FindControl("txtboxMessage");

txtMessage.Text = value;

}

}

 

public string MailFrom { get; set; }

public string MailTo { get; set; }

public string SMTPServerAddress { get; set; }

public SmtpDeliveryMethod DeliveryUsing { get; set; }

public string SMTPUserName { get; set; }

public string SMTPPassword { get; set; }

public int PortNumber { get; set; }

public bool IsSSLConnectionAvailable { get; set; }

public bool IsEmailHtml { get; set; }

Panel pnl = new Panel();

TextBox txtboxName = new TextBox();

TextBox txtboxEmail = new TextBox();

TextBox txtboxSubject = new TextBox();

TextBox txtboxMessage = new TextBox();

 

Button btnSend = new Button();

 

 

Button btnReset = new Button();

 

 

protected override void CreateChildControls()

{

btnSend.Click += new EventHandler(btnSend_Click);

btnReset.Click += new EventHandler(btnReset_Click);

Controls.Add(pnl);

pnl.Controls.Add(new LiteralControl("<table><tr><td>"));

pnl.Controls.Add(new LiteralControl("Your Name: "));

pnl.Controls.Add(new LiteralControl("</td><td>"));

pnl.Controls.Add(txtboxName);

pnl.Controls.Add(new LiteralControl("</td></tr><tr><td>"));

pnl.Controls.Add(new LiteralControl("Your Email: "));

pnl.Controls.Add(new LiteralControl("</td><td>"));

pnl.Controls.Add(txtboxEmail);

pnl.Controls.Add(new LiteralControl("</td></tr><tr><td>"));

pnl.Controls.Add(new LiteralControl("Subject: "));

pnl.Controls.Add(new LiteralControl("</td><td>"));

pnl.Controls.Add(txtboxSubject);

pnl.Controls.Add(new LiteralControl("</td></tr><tr><td>"));

pnl.Controls.Add(new LiteralControl("Your Message: "));

pnl.Controls.Add(new LiteralControl("</td><td>"));

pnl.Controls.Add(txtboxMessage);

pnl.Controls.Add(new LiteralControl("</td></tr><tr><td colspan='2'>"));

pnl.Controls.Add(btnSend);

pnl.Controls.Add(new LiteralControl("&nbsp;&nbsp;"));

pnl.Controls.Add(btnReset);

pnl.Controls.Add(new LiteralControl("</td></tr></table>"));

pnl.Style.Add("background-color","lightgray");

pnl.Style.Add("width","600px");

txtboxName.ID = "txtboxName";

txtboxName.Style.Add("width","350px");

txtboxEmail.ID = "txtboxEmail";

txtboxEmail.Style.Add("width", "350px");

txtboxSubject.ID = "txtboxSubject";

txtboxSubject.Style.Add("width", "350px");

txtboxMessage.ID = "txtboxMessage";

txtboxMessage.TextMode = TextBoxMode.MultiLine;

txtboxMessage.Columns = 42;

txtboxMessage.Rows = 5;

btnSend.ID = "btnSend";

btnSend.Text = "Send Email";

btnReset.ID = "btnReset";

btnReset.Text = "Reset";

}

 

void btnReset_Click(object sender, EventArgs e)

{

txtboxName.Text = "";

txtboxEmail.Text = "";

txtboxSubject.Text = "";

txtboxMessage.Text = "";

}

 

void btnSend_Click(object sender, EventArgs e)

{

MailMessage message = new MailMessage();

message.From = new MailAddress(MailFrom);

message.To.Add(new MailAddress(MailTo));

message.Subject = string.Format("Message from: {0}, {1}", Name, Email);

SmtpClient smtp = new SmtpClient(SMTPServerAddress);

smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

smtp.Credentials = new NetworkCredential(SMTPUserName, SMTPPassword);

smtp.Port = PortNumber;

smtp.EnableSsl = IsSSLConnectionAvailable;

message.IsBodyHtml = IsEmailHtml;

message.Body = string.Format(@"This message is from <a mailto:'{0}'>{1}</a>. <hr />{2}", Email, Name, Message);

smtp.Send(message);

}

}

}

 

Now let build your project to check if any error. Once project is build successfully, right click on solution explorer and add new website to test our control. Right click on newly created website and choose add reference and from reference dialog box click on projects tab select class library project which contain your server control.

Once successfully added the reference your website project will have a new folder with name bin with dll file of your server control library project. After this open default.aspx page and register your control to the page:

<%@ Register Assembly="CustomControlLibrary" Namespace="CustomControlLibrary" TagPrefix="cc1" %>

 

Now your server control is ready to use, add smtp mail information to the control and your control markup should look like this:

<cc1:ContactUsServerControl id="ContactUsServerControl1" runat="Server" IsEmailHtml="True" IsSSLConnectionAvailable="False" DeliveryUsing="Network" MailFrom="Example@Example.com" MailTo=" Example@Example.com " PortNumber="25" SMTPPassword="password" SMTPServerAddress="mail.example.com" SMTPUserName="UserName"></cc1:ContactUsServerControl>

 

After setting all properties when you run your control it should look like this:

Thank you.

Tags: , , ,

ASP.NET | Server Control