ASP.NET Email Control

Introduction

Well the purpose of the user control is quite simple. I have seen a series of publications all over the net for sending Emails using ASP.Net. I would have loved to find a user control that I could just drop on my page, enter minimum required fields and have the control do the rest of it for me.
 
That's what I finally feel I achieved with this user control. Now you can judge it and if you like it, you are free to use it, modify it.

Background

I tried to keep this as simple as possible but if you are trying to recreate what I did, you will need some basic experience in AJAX Extensions. You will obviously need to know what SMTP servers and ports mean.
 
The user control assumes that you will be adding this control on your website and the sole purpose is to receive emails from visitors. So the control will be using your credentials (email ID and password) to authenticate the sender of the email. For this reason, I removed the capability to attach files to the email.
 
Note: I do not recommend allowing others to send emails from your site using this user control. The change that need to be done to allow this is quite simple but the implications are numerous and generally not advisable.

Using the code

To use this user control you will have to perform the following simple steps.

  1. Set up your project to use the control
  2. Register the user control on your web form
  3. Add the user control to your web form and configure it
  1. Set up your project to use the control
     
    First step is to create a folder called 'Controls' in your website. Copy the included ".ascx" control to this folder. Also add the "AjaxControlToolkit.dll" file to the 'bin' directory to be able to use the validators.
     
  2. Register the user control on your web form
     
    Then you need to add the reference to the User control on your page using the following piece of code:
     
    <%@Register src="Controls/EmailControl.ascx"tagname="EmailControl"tagprefix="uc1"%>
     
  3. Add the use Control to your web form and configure it.

Option 1

Use the web.config file to provide the information needed for the email control. Add the control to your aspx page with the following code:
 
<uc1:EmailControl ID="EmailControl1" runat="server" />
 
Setup the control in the web.config file with the following information:

<appSettings>
         <add key="mailServerSSLEnabled" value=""/>
         <add key ="mailServerName" value=" "/>
         <add key="mailServerPort" value=""/>
         <add key="mailID" value=" "/>
         <add key="mailIDPassword" value=" "/>
</appSettings>
 
Option 2

Set up the mail control in the aspx page itself.

<uc1:EmailControl ID="EmailControl1" runat="server"
         mailServerSSLEnabled =" "
         mailServerName =" "
         mailServerPort =" "/>


Setup the control in the web.config file with the following information:

<appSettings> 
         <add key="mailID" value=" "/>
         <add key="mailIDPassword" value=" "/>
</appSettings>
 
My personal preference is to include the control information in the web.config file and encrypting the information. But then you would have to modify the web control code to handle the encryption and decryption (not included in this control).

Points of Interest

The control as provided does not include functionality to encrypt and decrypt the configuration information. As long as you are using this control on your website and you have complete control of the data provided for the control, its ok to use the control as is but when you are placing this file on a web server, I strongly recommend using the encryption and decryption logic using the following examples

http://msdn.microsoft.com/en-us/library/zhhddkxy.aspx
http://msdn.microsoft.com/en-us/library/zhhddkxy.aspx

Up Next
    Ebook Download
    View all
    Learn
    View all