Widgetized Section

Go to Admin » Appearance » Widgets » and move Gabfire Widget: Social into that MastheadOverlay zone

Sending E-mails with ASP.NET 2.0 using C#

Nowadays, web masters prefer to receive information from their users or customers through a contact or feedback form placed on the website.

ASP.NET 2.0 provides a simplified approach to achieve the task as compared to ASP.NET 1.1. In this part of the series, we will examine how to send e-mails in plain text and HTML formats using C#.

Let us start our discussion by creating a simple user interface as shown below

aspnet_2_email_UI

The code to create the above WebForm is given below.

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #0000ff">&lt;</span><span style="color: #800000">div</span> <span style="color: #ff0000">align</span>=<span style="color: #0000ff">&quot;left&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">table</span> <span style="color: #ff0000">border</span>=<span style="color: #0000ff">&quot;0&quot;</span> <span style="color: #ff0000">width</span>=<span style="color: #0000ff">&quot;544&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">tr</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">td</span> <span style="color: #ff0000">valign</span>=<span style="color: #0000ff">&quot;top&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">font</span> <span style="color: #ff0000">face</span>=<span style="color: #0000ff">&quot;Verdana&quot;</span> <span style="color: #ff0000">size</span>=<span style="color: #0000ff">&quot;2&quot;</span><span style="color: #0000ff">&gt;</span>Name<span style="color: #0000ff">&lt;/</span><span style="color: #800000">font</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">td</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">td</span> <span style="color: #ff0000">height</span>=<span style="color: #0000ff">&quot;24</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">form</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;Form1&quot;</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&quot;</span> <span style="color: #ff0000">valign</span>=<span style="color: #0000ff">&quot;top&quot;</span><span style="color: #0000ff">&gt;</span> <span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">Height</span>=<span style="color: #0000ff">&quot;25px&quot;</span> 
<span style="color: #ff0000">Width</span>=<span style="color: #0000ff">&quot;370px&quot;</span> <span style="color: #ff0000">ID</span>=<span style="color: #0000ff">&quot;txtName&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span> <span style="color: #ff0000">ID</span> = <span style="color: #0000ff">&quot;reqName&quot;</span> <span style="color: #ff0000">ControlToValidate</span> = 
<span style="color: #0000ff">&quot;txtName&quot;</span> <span style="color: #ff0000">Runat</span> = <span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">ErrorMessage</span> = <span style="color: #0000ff">&quot;*&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">td</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">tr</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">tr</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">td</span> <span style="color: #ff0000">valign</span>=<span style="color: #0000ff">&quot;top&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">font</span> <span style="color: #ff0000">size</span>=<span style="color: #0000ff">&quot;2&quot;</span> <span style="color: #ff0000">face</span>=<span style="color: #0000ff">&quot;Verdana&quot;</span><span style="color: #0000ff">&gt;</span>Address<span style="color: #0000ff">&lt;/</span><span style="color: #800000">font</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">td</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">td</span> <span style="color: #ff0000">height</span>=<span style="color: #0000ff">&quot;24&quot;</span> <span style="color: #ff0000">valign</span>=<span style="color: #0000ff">&quot;top&quot;</span><span style="color: #0000ff">&gt;</span> 
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">Height</span>=<span style="color: #0000ff">&quot;25px&quot;</span> <span style="color: #ff0000">Width</span>=<span style="color: #0000ff">&quot;370px&quot;</span> 
<span style="color: #ff0000">ID</span>=<span style="color: #0000ff">&quot;txtAddress&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span><span style="color: #0000ff">&gt;</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">td</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">tr</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">tr</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">td</span> <span style="color: #ff0000">valign</span>=<span style="color: #0000ff">&quot;top&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">font</span> <span style="color: #ff0000">face</span>=<span style="color: #0000ff">&quot;Verdana&quot;</span> <span style="color: #ff0000">size</span>=<span style="color: #0000ff">&quot;2&quot;</span><span style="color: #0000ff">&gt;</span>From<span style="color: #0000ff">&lt;/</span><span style="color: #800000">font</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">td</span><span style="color: #0000ff">&gt;</span>
<p><span style="color: #0000ff">&lt;</span><span style="color: #800000">td</span> <span style="color: #ff0000">height</span>=<span style="color: #0000ff">&quot;24&quot;</span> <span style="color: #ff0000">valign</span>=<span style="color: #0000ff">&quot;top&quot;</span><span style="color: #0000ff">&gt;</span> <span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span> </p><p><span style="color: #ff0000">Height</span>=<span style="color: #0000ff">&quot;22px&quot;</span> <span style="color: #ff0000">Width</span>=<span style="color: #0000ff">&quot;368px&quot;</span> 
</p>
<span style="color: #ff0000">ID</span>=<span style="color: #0000ff">&quot;txtFrom&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span><span style="color: #0000ff">&gt;</span>
<p><span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span> <span style="color: #ff0000">ID</span> = <span style="color: #0000ff">&quot;reqFrom1&quot;</span> </p><p><span style="color: #ff0000">ControlToValidate</span> = <span style="color: #0000ff">&quot;txtFrom&quot;</span> 
</p>
<span style="color: #ff0000">runat</span> = <span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">ErrorMessage</span> = <span style="color: #0000ff">&quot;*&quot;</span> <span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span><span style="color: #0000ff">&gt;</span>
<p><span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RegularExpressionValidator</span> <span style="color: #ff0000">ID</span> = <span style="color: #0000ff">&quot;regFrom2&quot;</span> <span style="color: #ff0000"></span></p><p><span style="color: #ff0000">ControlToValidate</span> = <span style="color: #0000ff">&quot;txtFrom&quot;</span> 
</p>
<span style="color: #ff0000">runat</span> = <span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">ErrorMessage</span> = <span style="color: #0000ff">&quot;Invalid Email&quot;</span> 
<span style="color: #ff0000">ValidationExpression</span>=<span style="color: #0000ff">&quot;\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RegularExpressionValidator</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">br</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">td</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">tr</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">tr</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">td</span> <span style="color: #ff0000">valign</span>=<span style="color: #0000ff">&quot;top&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">font</span> <span style="color: #ff0000">face</span>=<span style="color: #0000ff">&quot;Verdana&quot;</span> <span style="color: #ff0000">size</span>=<span style="color: #0000ff">&quot;2&quot;</span><span style="color: #0000ff">&gt;</span>To<span style="color: #0000ff">&lt;/</span><span style="color: #800000">font</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">td</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">td</span> <span style="color: #ff0000">height</span>=<span style="color: #0000ff">&quot;24&quot;</span> <span style="color: #ff0000">valign</span>=<span style="color: #0000ff">&quot;top&quot;</span><span style="color: #0000ff">&gt;</span> 
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">Height</span>=<span style="color: #0000ff">&quot;22px&quot;</span> <span style="color: #ff0000">Width</span>=<span style="color: #0000ff">&quot;368px&quot;</span> <span style="color: #ff0000">ID</span>=<span style="color: #0000ff">&quot;txtTo&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span> <span style="color: #ff0000">ID</span> = <span style="color: #0000ff">&quot;reqTo1&quot;</span> <span style="color: #ff0000">ControlToValidate</span> = <span style="color: #0000ff">&quot;txtTo&quot;</span> 
<span style="color: #ff0000">runat</span> = <span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">ErrorMessage</span> = <span style="color: #0000ff">&quot;*&quot;</span> <span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RegularExpressionValidator</span> <span style="color: #ff0000">ID</span> = <span style="color: #0000ff">&quot;reqTo2&quot;</span> <span style="color: #ff0000">ControlToValidate</span> = <span style="color: #0000ff">&quot;txtTo&quot;</span> 
<span style="color: #ff0000">runat</span> = <span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">ErrorMessage</span> = <span style="color: #0000ff">&quot;Invalid Email&quot;</span> 
<span style="color: #ff0000">ValidationExpression</span>=<span style="color: #0000ff">&quot;\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RegularExpressionValidator</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">td</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">tr</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">tr</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">td</span> <span style="color: #ff0000">valign</span>=<span style="color: #0000ff">&quot;top&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">font</span> <span style="color: #ff0000">face</span>=<span style="color: #0000ff">&quot;Verdana&quot;</span> <span style="color: #ff0000">size</span>=<span style="color: #0000ff">&quot;2&quot;</span><span style="color: #0000ff">&gt;</span>Comments<span style="color: #0000ff">&lt;/</span><span style="color: #800000">font</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">td</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">td</span> <span style="color: #ff0000">height</span>=<span style="color: #0000ff">&quot;112&quot;</span> <span style="color: #ff0000">valign</span>=<span style="color: #0000ff">&quot;top&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span> <span style="color: #ff0000">runat</span>=<span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">Height</span>=<span style="color: #0000ff">&quot;110px&quot;</span> 
<span style="color: #ff0000">TextMode</span>=<span style="color: #0000ff">&quot;MultiLine&quot;</span> <span style="color: #ff0000">Width</span>=<span style="color: #0000ff">&quot;368px&quot;</span> <span style="color: #ff0000">ID</span>=<span style="color: #0000ff">&quot;txtComments&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">TextBox</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span> <span style="color: #ff0000">ID</span> = <span style="color: #0000ff">&quot;reqComments&quot;</span> <span style="color: #ff0000">ControlToValidate</span> = <span style="color: #0000ff">&quot;txtComments&quot;</span> 
<span style="color: #ff0000">Runat</span> = <span style="color: #0000ff">&quot;server&quot;</span> <span style="color: #ff0000">ErrorMessage</span> = <span style="color: #0000ff">&quot;*&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">RequiredFieldValidator</span><span style="color: #0000ff">&gt;</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">td</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">tr</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">tr</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">td</span> <span style="color: #ff0000">colspan</span>=<span style="color: #0000ff">&quot;2&quot;</span> <span style="color: #ff0000">valign</span>=<span style="color: #0000ff">&quot;top&quot;</span> <span style="color: #ff0000">height</span>=<span style="color: #0000ff">&quot;30&quot;</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span> <span style="color: #ff0000">align</span>=<span style="color: #0000ff">&quot;center&quot;</span><span style="color: #0000ff">&gt;</span>
<p><span style="color: #0000ff">&lt;</span><span style="color: #c71585">asp</span>:<span style="color: #800000">Button</span> <span style="color: #ff0000">Runat</span> = <span style="color: #0000ff">server</span> <span style="color: #ff0000">ID</span> = <span style="color: #0000ff">btnSubmit</span> </p><p><span style="color: #ff0000">OnClick</span> = <span style="color: #0000ff">btnSubmit_Click</span> 
</p>
<span style="color: #ff0000">Text</span> = <span style="color: #0000ff">&quot;Submit&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #c71585">asp</span>:<span style="color: #800000">Button</span><span style="color: #0000ff">&gt;</span>
<p>&amp;nbsp;<span style="color: #0000ff">&lt;</span><span style="color: #800000">input</span> <span style="color: #ff0000">id</span>=<span style="color: #0000ff">&quot;Reset1&quot;</span> <span style="color: #ff0000">type</span> = <span style="color: #0000ff">&quot;reset&quot;</span> <span style="color: #ff0000">runat</span> = <span style="color: #0000ff">&quot;server&quot;</span> </p><p><span style="color: #ff0000">value</span> = <span style="color: #0000ff">&quot;Clear&quot;</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">td</span><span style="color: #0000ff">&gt;</span>
</p>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">tr</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">table</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">div</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>&amp;nbsp;<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">br</span><span style="color: #0000ff">&gt;</span>
<span style="color: #008000">&lt;!-- Insert content here --&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>
<span style="color: #0000ff">&lt;/</span><span style="color: #800000">form</span><span style="color: #0000ff">&gt;</span>

As you can see we have applied validation controls to prevent input errors. The next process is to provide the required code for sending e-mails upon submission of the form. We will analyze each process in a series of steps for better understanding.

Step 1: Importing Namespace

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="background-color: #ffff00; color: black">&lt;%@ Page Language=&quot;C#&quot; %&gt;</span>
<span style="background-color: #ffff00; color: black">&lt;%@ Import Namespace=&quot;System.Net.Mail&quot; %&gt;</span>

 

Step 2: Creating objects of SmtpClient and MailMessage classes

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">SmtpClient smtpClient = new SmtpClient();
MailMessage objMail = new MailMessage();

 

Step 3: Creating and Specifying From, To Addresses

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><p>//From Address will be assigned from the e-mail </p><p>specified in the From TextField
</p>
MailAddress objMail_fromaddress = new MailAddress(txtFrom.Text);
You can also give the code given below.
<p>MailAddress objMail_fromaddress = new </p><p>MailAddress(txtFrom.Text, &quot;Learnxpress Support&quot;);</p>

 

The above code inserts the text Learnxpress Support instead of the From e-mail address.

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><p>//To Address will be assigned from the e-mail </p><p>specified in the To TextField
</p>
MailAddress objMail_toaddress = new MailAddress(txtTo.Text);
//Assigning From address to the MailMessage class
objMail.From = objMail_fromaddress;
//Assigning To address to the MailMessage class as a collection
objMail.To.Add(objMail_toaddress);

Step 4: Creating the Subject and Message Body Text Format

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">objMail.Subject = &quot;E-mail Example - CSharp - Text&quot;;
<p>objMail.Body = &quot;Name: &quot; +txtName.Text + &quot;\r\n&quot; +&quot;Address: &quot; </p><p>+ txtAddress.Text + &quot;\r\n&quot; + &quot;Comments: &quot; + txtComments.Text</p>

Step 5: Specifying the HTML Header

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">objMail.IsBodyHtml = true;

Step 6: Creating the Subject and Message Body HTML Format

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">objMail.Subject = &quot;E-mail Example - CSharp - HTML&quot;;
<p>objMail.Body = &quot;<span style="color: #0000ff">&lt;</span><span style="color: #800000">html</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">head</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">title</span><span style="color: #0000ff">&gt;</span>Sending E-mails with </p><p>ASP.NET 2.0 using CSharp<span style="color: #0000ff">&lt;/</span><span style="color: #800000">title</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">head</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">body</span><span style="color: #0000ff">&gt;</span>&quot; +&quot;<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>Name : &quot; </p><p>+txtName.Text + &quot;<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>&quot; +&quot;<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>Address : &quot; +txtAddress.Text + </p><p>&quot;<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>&quot; +&quot;<span style="color: #0000ff">&lt;</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>Comments : &quot; +txtComments.Text + &quot;<span style="color: #0000ff">&lt;/</span><span style="color: #800000">p</span><span style="color: #0000ff">&gt;</span>&quot; </p><p>+&quot;<span style="color: #0000ff">&lt;/</span><span style="color: #800000">body</span><span style="color: #0000ff">&gt;</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">html</span><span style="color: #0000ff">&gt;</span>&quot;;</p>

Step 7: Specifying Mail Priority

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">objMail.Priority = MailPriority.High;

Step 8: Enabling SMTP Authentication

Sometimes, you will need to provide the code for SMTP authentication to send e-mails. The required code snippet is given below.

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">smtpClient.Host = &quot;mail.domain.com&quot;;
<p>smtpClient.Credentials = new </p><p>System.Net.NetworkCredential(&quot;username@domain.com&quot;,&quot;Password&quot;);</p>

Note: domain should be substituted with your real domain name such as c-sharpcorner, learnxpress.

Step 9: Handling Exceptions and Delivering E-mail

<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">try
{
smtpClient.Send(objMail);
Response.Redirect(&quot;http://www.c-sharpcorner.com&quot;); 
}
catch (Exception exc)
{
Response.Write(&quot;Send failure: &quot; + exc.ToString());
}

In the above code, we have specified that the page should be redirected to c-sharpcorner website after the successful submission of the form. You can also create a separate page called thankyou.aspx and redirect users to the page so that users will know that the mail has been successfully delivered. Once you understood the basics of System.Net.Mail namespace then it is very easy to write the code. Moreover, we now have a simplified coding mechanism as compared to ASP.NET 1.1.

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS
Posted by on February 9, 2009. Filed under .NET,Development. You can follow any responses to this entry through the RSS 2.0. You can leave a response or trackback to this entry

13 Responses to Sending E-mails with ASP.NET 2.0 using C#

  1. Joao Reply

    June 8, 2009 at 4:37 pm

    …pretty clean solution. I wonder where in the .config file, you placed the user login and password for the e-mail. account?

  2. madalina Reply

    July 2, 2009 at 8:01 am

    very nice and helpfull! Thnx!

  3. Bijal Reply

    August 3, 2009 at 10:21 pm

    code is very easy and understandable.Thanks

  4. Bijal Reply

    August 10, 2009 at 9:11 am

    Thanks for posting,

    Very useful and easily understandable code.

  5. karthik Reply

    November 10, 2009 at 7:47 pm

    Thanks a tonne. It was very helpful.

    Also, I get a feeling some times the mail gets stuck at the server. Is there a way to trigger or kick it out?

  6. tlkit Reply

    December 21, 2009 at 4:13 am

    The SMTP server requires a secure connection or the client was not authenticated.. Help me!

  7. tu Reply

    January 21, 2010 at 12:53 am

    you can say

  8. Rachel Eiple Reply

    February 2, 2010 at 6:17 pm

    Hey neat site, just have something to ask you, what antispam system you use for filtering out junk websites because I am getting so many spammers on my website.

  9. patil Reply

    September 20, 2010 at 11:48 am

    i dint get any o/p man

  10. patil Reply

    September 20, 2010 at 11:50 am

    sent failSystem.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.3 Client was not authenticated at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.Button1_Click(Object sender, EventArgs e) in c:\Documents and Settings\ram\My Documents\Visual Studio 2005\WebSites\send mail\Default.aspx.cs:line 42

    this is my o/p

  11. Nilesh Kumar Reply

    November 28, 2010 at 4:16 am

    Hi, I am getting request time out error.

    Send failure: System.Net.Mail.SmtpException: The operation has timed out. at System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.btnsendmail_Click(Object sender, EventArgs e) in d:\MailDemo2\Default.aspx.cs:line 36

    Please help.

  12. kartik Reply

    August 2, 2011 at 9:24 am

    best coding bro….
    thank u very much…..

  13. kartik Reply

    August 2, 2011 at 9:29 am

    sent failSystem.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.3 Client was not authenticated at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.Button1_Click(Object sender, EventArgs e) in c:\Documents and Settings\ram\My Documents\Visual Studio 2005\WebSites\send mail\Default.aspx.cs:line 42

    this error can b resolved by just replacing “mail.domain.com” mail hosing ip
    smtpClient.Host = “mail.domain.com”;
    instate
    smtpClient.Host = “mail hosting ip”;

    from,
    tech4em Computers, Nagpur
    (Projects development on .net platform)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>